Pages: [1]   Go Down
Author Topic: Help with converting Byte to Int  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok so I have two arduino's setup with Xbees. One is sending to the other like this:
Code:
char buffer[128]
char buffer1[128]
char buffer2[128] //The function requires that they are char[]

if(Canbus.ecu_req(ENGINE_COOLANT_TEMP,buffer1) == 1)
if(Canbus.ecu_req(ENGINE_RPM,buffer2) == 1) //this stores the values in buffer1 and buffer2

sprintf(buffer,"%d%,d%,d%,%d;) // when i print
serial.println(buffer);

On my receiving arduino i figured out how to convert this into the integer value, however i can only manage to do this with the first number and then once it detects a comma it ignores the rest because of the atoi function:


int serReadInt()
{
 int i, serAva;                           // i is a counter, serAva hold number of serial available
 char inputBytes [7];                 // Array hold input bytes
 char * inputBytesPtr = &inputBytes[0];  // Pointer to the first element of the array
    
 if (Serial.available()>0)            // Check to see if there are any serial input
 {
   delay(5);                              // Delay for terminal to finish transmitted
                                              // 5mS work great for 9600 baud (increase this number for slower baud)
   serAva = Serial.available();  // Read number of input bytes
   for (i=0; i<serAva; i++)       // Load input bytes into array
     inputBytes[i] = Serial.read();
   inputBytes[i] =  '\0';             // Put NULL character at the end
   return atoi(inputBytesPtr);    // Call atoi function and return result
 }
 else
   return -1;                           // Return -1 if there is no input
}

Is there any way that I can take the buffer string and take each number separated by comma out individually?


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
« Last Edit: April 01, 2012, 06:55:32 pm by Nick Gammon » Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8919
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there any way that I can take the buffer string and take each number separated by comma out individually?

One simple idea when capturing from the serial port:

Code:
//zoomkat 3-5-12 simple delimited ',' string parce
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
 
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input     
     } 
    else {     
      readString += c; //makes the string readString
    }
  }
}
« Last Edit: April 01, 2012, 08:56:41 pm by zoomkat » Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to: