newbie advice:serial.println

Hi all,

I have got the following code that is needed to read 5chrs from the serial port, the incoming code is always in the format:
*S0123, where the *S never changes so im using them as the start chrs, then next 3 digits is the data i need, well its the last 2 digits.
So when i enter *S0123 from the serial monitor i want to see 23 decimal as the output. I know the code is probably very 'rough' so be kind, but it appears to work, my only issue is the following line:

Serial.println(code*); //echo it back to see if it's in!*
If i remark this line, then the code just jumps to 'the data is invalid', i cannot understand why remark a serial.println would cause this!!!
Any ideas and also any issues with the code would be welcomed.
cheers
nick
int val = 0;
char code[5];
int bytesread = 0;
int i;
int speedunit;
int speedten;
int speedval;
void setup() {
Serial.begin(9600); // set serialport to 9600baud
*} *
void loop() {

  • if(Serial.available() > 0) { // if data available from radar*
  • i=0;*
  • while (i<5)*
  • {*
    _ code*=Serial.read();_
    _ Serial.println(code); //echo it back to see if it's in!
    i++;
    }
    //Serial.println();
    // now we have all the data in the 'code' array*_

_ //check the first two elements of the array are '*' and 'S'_

_ if ((code[0]=='')&(code[1]=='S'))
{
Serial.println("yipee, we have valid data !");
speedunit=code[4]-48; // the value '48' comes from the fact that ascii '0' is decimal '48' and ascii '1' is decimal '49'
speedten=code[3]-48; // Just a simple way to convert from ascii to decimal!!!!!!
Serial.print("your current speed is ");
speedval=(speedten10)+speedunit;

* Serial.println(speedval,DEC); // speedval is now a decimal value!
}
else*
* Serial.println("bugger, the data is invald ");*_

* Serial.flush(); //clean out the serial data buffer and get ready for new data*
}
}

if(Serial.available() > 0) {          // if data available from radar
  i=0;
  while (i<5)
  {
    code=Serial.read();
    Serial.println(code); //echo it back to see if it's in!
    i++;
  }
  //Serial.println();
  // now we have all the data in the 'code' array

If there is at least one byte to read, read all 5 of them. Do you see a problem with this approach?

The delay caused by printing the character read allows additional characters to arrive. But, you really should not rely on this behavior. You should have an end-of-packet marker. Read and store all characters that have already arrived, until the end of packet marker arrives. When that happens, you can parse the received data, and reset for the next packet.

speedunit=code[4]-48;
speedunit=code[4]-'0';

Is much easier to read, don't you think?

Hi Guys,
thanks for the reply, PaulS i think i understand what you are saying, the only thing i could use as a 'end of line' could be a cr as the data dosnt have an chrs as such :~.
The data arrives as follows:
*S035
*S023
*S034
*S035

That is the only info i have recieved from the manufacturer as to the data structure and there is no hardware/software handshaking available, so i could check for a cr and then store the data, is that what you are saying??.

AWOL, thanks for the tip will use '0' instead.
t
hanks again for your help.
cheers
Nick

If there are never more than 5 characters, and the first is always a *, then an end-of-packet marker is not necessary. As each character is read, add it to the end of the array, unless it IS the *, in which case re-initialize the array and index variables.

When 5 characters have been read, then process the array.