Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Unity向Arduino发送许多变量_C#_String_Unity3d_Arduino_Serial Port - Fatal编程技术网

C# 从Unity向Arduino发送许多变量

C# 从Unity向Arduino发送许多变量,c#,string,unity3d,arduino,serial-port,C#,String,Unity3d,Arduino,Serial Port,我正试着给我的“大师阿杜伊诺”发很多信息。 因为SerialPort对象只发送字符串。我尝试过很多事情,包括: 从ints创建字符串(由于string.length的大小是动态的,因此无效) 然后我尝试将这些整数转换为字符,这是因为所有的值都在0-255之间,然后将字符转换成字符串并发送 这类工作。然而,我认为在char世界中没有0的值。所以数据是不正确的。但一定有更好的办法吗 void sendInfo() { for (var i = 0; i < peltiers.Len

我正试着给我的“大师阿杜伊诺”发很多信息。 因为SerialPort对象只发送字符串。我尝试过很多事情,包括:

  • 从ints创建字符串(由于string.length的大小是动态的,因此无效)
  • 然后我尝试将这些整数转换为字符,这是因为所有的值都在0-255之间,然后将字符转换成字符串并发送
这类工作。然而,我认为在char世界中没有0的值。所以数据是不正确的。但一定有更好的办法吗

void sendInfo() {
    for (var i = 0; i < peltiers.Length; i++) {
        char tempHot = (char) peltiers[i].GetComponent<Peltier>().hot;
        char charTemp = (char) peltiers[i].GetComponent<Peltier>().temp;
        peltierInfo += tempHot.ToString();           
        peltierInfo += charTemp.ToString();                  
    } 
    sp.WriteLine(peltierInfo);
    Debug.Log(peltierInfo);
    sp.BaseStream.Flush();
    peltierInfo = "";         
}
void sendInfo(){
对于(var i=0;i
任何帮助都将不胜感激!谢谢

Arduino代码:

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0; 
    while (0 < Serial.available()) { // loop through all the received bytes 
    char bufferByte = 0;     
    bufferByte = Serial.read();
    serialBuffer[serialIndex] = (byte) bufferByte;  // put current index byte in array      
    serialIndex ++;                    // add index. 
    if (serialIndex%12==0 && serialIndex != 0) {
          sendBytes(0);      
       }
    } 
     //sendBytes(0);  
     delay(50);
}

void sendBytes(int slave) {
    byte i2cBuffer[12];
    int bufferIndex = slave * 12;
    for (int i = 0; i < 12; i++) {
        i2cBuffer[i] = serialBuffer[i + bufferIndex];
    }
    Wire.beginTransmission(slave+1);
    Wire.write(i2cBuffer, 12);
    Wire.endTransmission();  
}
void循环(){
int serialIndex=0;
int i2cIndex=0;
而(0
要能够发送任何整数,首先将它们编码成字符串,用某种东西(例如
'\0'
字符)将它们分开,然后对字符串进行解码

void sendInfo() {
    ...
    peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
    peltierInfo += '\0';
    peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString();
    peltierInfo += '\0';
    ...
}

void loop() {
    int serialIndex = 0;  
    int i2cIndex = 0;
    // set to how many digits there can be in an incoming number plus 1
    int maxNumberLen = 20; 
    char buffer[20];
    // position at which we now put a char that makes up our number
    char* currentCharPtr = buffer;
    while (0 < Serial.available()) { // loop through all the received bytes 
       char bufferByte = 0;     
       bufferByte = Serial.read();
       *currentCharPtr = bufferByte;
       // move pointer forward
       currentCharPtr ++;
       // end of a number in string
       if (bufferByte == '\0') {
           printf("Got number %s\n", buffer);
           // atoi parses string to int
           serialBuffer[serialIndex] = atoi(buffer); 
           serialIndex ++;                    
           if(serialIndex%12==0 && serialIndex != 0){
              sendBytes(0);
           }
           // fill buffer with zeros after we found a number
           memset(buffer, 0, 20);
           currentCharPtr = buffer;
       }
    } 
     //sendBytes(0);  
     delay(50);

 }
void sendInfo(){
...
peltierInfo+=peltiers[i].GetComponent().hot.ToString();
peltierInfo+='\0';
peltierInfo+=peltiers[i].GetComponent().temp.ToString();
peltierInfo+='\0';
...
}
void循环(){
int serialIndex=0;
int i2cIndex=0;
//设置传入数字中可以有多少位数加1
int maxNumberLen=20;
字符缓冲区[20];
//我们现在放置组成数字的字符的位置
char*currentCharPtr=缓冲区;
而(0
谢谢你的回复,你的回答正是我让它工作的方式。我只是做了一些不同的事情,没有时间上传这篇文章。哪种方法更好,还是只是口味的问题

统一

void sendInfo()
{
for(int i=0;i
阿杜伊诺

void loop() {
   int serialIndex = 0;
   if(Serial.available() > 0){     
     while (0 < Serial.available()) {            // loop through all the received bytes 
        String bufferString;
        uint8_t bufferInt;
        bufferString = Serial.readStringUntil(','); 
        bufferInt = bufferString.toInt();      
        serialBuffer[serialIndex] = bufferInt;  // put current index byte in array      
        serialIndex ++;                          // add index. 
     }     
     sendBytes(0); 
   }
   delay(50);
}
void循环(){
int serialIndex=0;
如果(Serial.available()>0){
而(0

谢谢你的帮助

“字符世界”中有
0
,即
'\0'
。这是一个不可打印的空字符,如果您试图打印它,您只会得到一个空白。发送零有问题吗?哦,对不起,当然你的版本更好-我假设你在Arduino代码中使用纯C。如果你使用C++,那么就要使用<代码>字符串< /C> >而不是char数组。我选择了
'\0'
作为分隔符,因为在纯C中,每个字符串都必须以
'\0'
结尾,所以我的代码更简单。
void loop() {
   int serialIndex = 0;
   if(Serial.available() > 0){     
     while (0 < Serial.available()) {            // loop through all the received bytes 
        String bufferString;
        uint8_t bufferInt;
        bufferString = Serial.readStringUntil(','); 
        bufferInt = bufferString.toInt();      
        serialBuffer[serialIndex] = bufferInt;  // put current index byte in array      
        serialIndex ++;                          // add index. 
     }     
     sendBytes(0); 
   }
   delay(50);
}