2

I'm trying to send many ints to my "masterArduino". Because the SerialPort object only send strings. I've tried many things including:

  • Creating a string from ints (didn't work because the size of the string.length is dynamic).
  • Then I tried to convert these ints to chars, this because all values are between 0-255, then put the char into a string and send it.

This sort of works. However, I think there is no value for 0 in char world. So the data is not right. But there must be a better way?

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 = "";         
}

Any help would be greatly appreciated! Thanks!

Arduino Code:

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();  
}
1
  • there is 0 in the "char world", namely, '\0'. It's an unprintable empty character, if you try to print it you'll simply get an empty space. Do you have a problem with sending zeroes? Commented Mar 22, 2017 at 14:37

2 Answers 2

1

To be able to send any integers, first encode them into a string, separate them by something (e.g the '\0' char) and then decode the string.

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);

 }
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the reply, your answer was exactly how i got it working. I just did it slightly different and didn't had the time to upload this post. What way is better, or is it just a matter of taste?

Unity

 void sendInfo()
{
    for (int i = 0; i < peltiers.Length; i++)
    {
        peltierInfo += ",";
        peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
        peltierInfo += ",";
        peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString("D3");
    }
    //Debug.Log(peltierInfo);
    sp.WriteLine(peltierInfo);
    sp.BaseStream.Flush();
    peltierInfo = "";
}

Arduino

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);
}

thanks for the help!

1 Comment

oh, sorry, of course your version is better - I assumed you were using pure C for Arduino code. If you're using C++, then by all means use String instead of char arrays. I chose '\0' as a separator, simply because in pure C every string has to end with '\0' so it made my code simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.