Hey guys, Yet another serial coms question. Im feeding the string LF100 to my arduino via the serial monitor. I want to take that string and split it into two chars, cmd = LF and param = 100, and then echo one of them back. so in comes LF100, that gets chopped into command (first two bytes) and params (last 3 bytes) and then I want to echo back cmd. When I run this program though, I end up getting something like LF100LF100Ah
sent back to me. It looks like the data is not split and is double of what I sent via the serial monitor! The code is below, any thoughts?
/*
- Robots hate me
- Control a robot's movement via a serial connection
- Assumes DC motors controlled with PWM
*/
#include <string.h>//Setup
void setup()
{
Serial.begin(9600); //setup the serial port for 96008N1
}void GetString(char *buf, int bufsize) //Get the command from the serial port
{int i;
Serial.flush();
for (i=0; i<bufsize - 1; ++i) //start feeding the buf variable
{
while (Serial.available() == 0); // wait for character to arrive
buf = Serial.read(); //read data into buf
_ if (buf == '\0' ) // is it the terminator byte?_
* break;*
* }*
}
void loop()
{
* char cmd[2];*
* char param[3];*
* char buffer[6]; //serial command buffer*
* GetString(buffer, sizeof(buffer)); //capture the command from the serial port*
* if (strncmp(buffer, "LF", 2) == 0) //If the first two bytes are LF*
* {*
* strncpy (cmd, buffer, 2); //copy the first two bytes into cmd*
* strncpy( param, buffer+2, 3 ); //copy the last 3 bytes into param*
* Serial.println(cmd); //and print it*
* }*
}
[/quote]