im after some code that will read an entire string (starting and ending with the * character) from a serial port and store it as a string, from there i would like to use a case or if statements to do other things based on the input string.
i've googled it but not had much luck, please help someone! i did find some code where people are reading single characters or integers.
someone please help or steer me in the right direction!
Some simple test code that you can substitute your "*" for the ",".
// zoomkat 10-29-10 simple delimited ',' string parce
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial-delimit-21"); // 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!,
while (Serial.available()) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
break;
} //breaks out of capture loop to print readstring
readString += c;
} //makes the string readString
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
readString=""; //clears variable for new input
}
}
// assumes a format like <???>
// NOTES:
// no checking for buffer overflow
// will hang if ">" never received
// only designed for an app that has nothing else to do
// no null termination of packet as not specifically designed for strings
//
boolean in_packet = false;
char buffer[30];
int bptr = 0;
void loop()
{
while(Serial.peek() == -1) {}; // wait for a character
char c = Serial.read(); //reading from the serial
if (c == '<') { // check for start of packet char, if not then ignore
bptr = 0;
in_packet = true;
while (in_packet) {
if (Serial.available()) {
incomingData = Serial.read();
if (c == '>') {
handleData();
in_packet = false; // end of packet
} else {
buffer[bptr++] = c;
}
}
}
}
}
void handleData () {
// deal with the data here
}