Go Down

Topic: Help with converting Byte to Int (Read 691 times) previous topic - next topic

filip

Apr 02, 2012, 01:48 am Last Edit: Apr 02, 2012, 01:55 am by Nick Gammon Reason: 1
Ok so I have two arduino's setup with Xbees. One is sending to the other like this:
Code: [Select]

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)

zoomkat

#1
Apr 02, 2012, 02:41 am Last Edit: Apr 02, 2012, 03:56 am by zoomkat Reason: 1
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: [Select]
//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
    }
  }
}
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy