serial data format

Afternoon,

I'm having a little trouble with the formatting of data from the serial port (USB).

I have a small TextStar LCD screen that i can send text data too and it displays it correctly on the screen, this only works if the data comes directly from the arduino. I have been using the LCD Smartie app to send data to it using the testdisplay.dll output driver, i have uised this in the past to pass cpu usage data etc to the arduino and drive a servo.

The problem im having is that im not sure on the format of the data that is received from the serial port. I think it is read one byte at once with Serial.read() and to read more than one character at once i need to add them up into a buffer.

I have created a little program to first detect a start character, keep on looping until the character is the end character and then print this our the serial port to the lcd screen.

This doesnt work, it doesnt seem to detect the start character.

LCD Screnie is set to just output the following

"#CPU%$CPU_USE$@"

=the start character

CPU% is plain text $CPU_USE$ is what it replaces with the number 0-100 for the percentage cpu usage @=end character

the code is below that is on the arduino

char buffer[7];
int pointer=0;
byte inByte=0;
void setup()
{
  Serial.begin(9600);

}

void loop()
{  

if (Serial.available() >0) {
  
  //Read the data
  inByte=Serial.read();
  
  // Check for marker
  
  if (inByte == '#') {
  
    // Loop and fill the buffer
    while (buffer[pointer] != '@') {
      buffer[pointer]=Serial.read();
      pointer++;
    }
  pointer=0; // when stop character is detected reset pointer and output buffer
  Serial.println(buffer);
  }
}
}

I have put in a Serial.print("test") in the IF statement block and it never shows it, as if it doesnt detect the # symbol.

Help!!!

I have been struggling with this for a while now.

You are only checking for Serial.available() for the first byte after that you blithely read the buffer if anything is in or not. This won't address the initial problem of not seeing the #

Try putting a print command before the if to see what you are getting. You could try if (inByte == (byte)'#')

:-[ oh yeah, makes sense now.

I will correct the Serial.available() bit later this evening.

im confused by the (byte)'#' bit should it be byte('#')?

Thanks again, i knew it would be something simple.

(byte)'#' performs an explicit cast, in the usual C/C++ way. The byte function converts the input value to a byte. The result is the same, but the explicit cast does not have the overhead of a function call.

Neither way should be necessary. What is important is to echo the input, to make sure that the input is correct. When you know the input is correct, you can deal with the issue of why the code is not operating correctly on correct input. It may be that input is not correct, and that the program is working correctly.

Blind guess, perhaps its sending with parity?