So I was sending a number of integers over wifi and the easiest thing to do at the time was just to use Serial.println(myInt). However when I was receiving these integers on my laptop it was desirable to know how many to receive so the rest of the program on my laptop could proceed. eg i expect 1000 integers, if i get 1000 integers continue program. However when you use println it prints integers as characters so integers can vary in size meaning that you dont know how many bytes to expect. eg the number 432 is
3 characters long but the number 78 is 2 characters long.
So I then started sending the integers as bytes. So for any int I split it into 2 bytes so I know exactly the number of bytes to expect. eg a 1000 ints is 2000 bytes. The problem with this is however when you send ints as bytes the bytes can be dedicated commands in the wifi module. eg byte 0xd is a flush command. So it means eventually some integer will have a undesirable wifi command byte. So how do I work around this issue.
Note: using while(Serial.available() > 0) on my laptop to just wait till buffer finishes filling wont work in this case because the source that i m sending from varies with time.
Also, would you need to take into account the serial buffer size for outgoing data. eg if the buffer size of the wifi module flushes when it gets to 1460 bytes does this mean that the serial buffer on the arduino has to be flushed every 64 bytes or can the serial buffer just keep writing/printing for outgoing data?
Generally some type of data delimiter is used to separate the data parts, and an end of data packet marker is used to indicate when all the data parts have been received.
yea, I think this is what I ll do. I ll revert back to using println and use delimiters to indicate end of bytes. I m actually doing this in matlab and a solution to this can be found here,
The accepted answer by Guillaume seems to be the best way of doing this.
Below is a way I receive servo command positions for several servos. Putting the number value first makes parsing the value easy. Easiest to figure out how to receive and parse the data first, then develop a way to send that format.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}