I recently received a set of Xbee modules. Im trying to write a simple program for them where the arduino reads a command from the computer and prints the command to the serial command port for the other Xbee to read from CoolTerm. However, I can only get the Xbee to print a single character at a time so if I send "start", the arduino prints s, t, a, r, t. How do I make it output all the input?
This code does not wait until a byte is available but even when one is available it is always put into the second position in the str1 array then printed.
This page explains how to receive multiple bytes and put them into a null terminated array of chars (ie a C string). Once the string has been constructed you can act upon what it holds to take whatever actions you want.
Some test code I've used to send character strings from one arduino to another arduino using the serial pins.
//zoomkat 3-5-12 simple delimited ',' string tx/rx
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin.
//Connect the arduino grounds together.
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test
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 == ',') {
if (readString.length() >1) {
Serial.print(readString); //prints string to serial port out
Serial.println(','); //prints delimiting ","
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}