Go to Serial input basics: example 5;
Simple untested modification for 2 ints...
SENDER ARDUINO
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
delay(1000);
Serial.print("<123,456>");
}
RECEIVER ARDUINO
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int integerONE = 0;
int integerTWO = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
integerONE = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
integerTWO = atoi(strtokIndx);
}
//============
void showParsedData() {
Serial.print("Integer ONE");
Serial.println(integerONE);
Serial.print("Integer TWO");
Serial.println(integerTWO);
}