I am sending my Arduino text messages. The message is formated beginning with a ‘?’ followed by the message contents.
For some reason, I cannot get the incoming serial data to load into my variable “load” properly. Once the function Update_schedule is called all it outputs is a “!”
I see, so what's in my if statement is only called when incoming_char is '?'. That looks obvious now, how can I basically iterate through the next 20 characters coming through the serial port and load them into load? A while loop?
For instance:
if(incoming_char == '?')
{
while(incoming_char != '!') //Message contents are terminated in a '!'
{
load += incoming_char;
}
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Change the values of SOP and EOP to match your start and end of packet markers. Where it says “Process the packet” put your code for using the data in the packet.
When I sent a text message, I get all the heading up until the '?' then nothing on the serial terminal.
I will be parsing out various values from inData and sending it over a bluetooth or zigbee to another board, just haven't implemented that part yet.
I was trying to print out inData to see what it's contents are.
I tried a few different terminating characters and also a simple test statement in lieu of any code to see if it's even entering the if(started && ended) statement.
No dice..it won't even print that it's entered the if statement.
If I comment out the parse code we've been discussing the full message contents still won't display past the '?'. After messing around, only the first character of the message contents will display on the serial monitor..
Below is simple code that collects data until a , is received, at which time the data is acted upon.
//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
}
}
}
So I got this code working. Thanks very much to PaulS for that code, it works great.
I figured it out by building a program that exclusively parsed and incoming text message, the code worked no problem.
I then selectively commented out the various functions that were running in loop to see which was effecting the parse. Looks like my RFID functions were interfering so a simple modification telling them specifically when to run fixed it.