Serial buffer array - What's wrong

I have a Mega 2650, IDE 1.0, Reading serial data from a 4d Systems LCD unit in GFX mode. My packet is 00000 (5 seperate values. I have setup a basic code to read this packet into an Array and then just send it to the terminal app. For some reason I can not get this to work and not sure why. Using Serial.write(Serial1.read()); , I get the data as I expect to see it from the com channel. Yet using the following array code I only get the debug message saying that serial data detected. Here is my code. Afer is results of code. NOTE: I commented out the different Array defines as I tried each one with same result. Trying to learn what each does. Most confusing is the uint8_t. But uncommenitng each seperatly does not work.

// int LcdRcvPacket[5]; // No data detected diplayed
// char LcdRcvPacket[5]; //No data detected displayed
// uint8_t LcdRcvPacket[] = { 0, 0, 0, 0, 0}; // no data detected
// uint8_t LcdRcvPacket[] = {'0','0','0','0','0'}; // No data detected

// Serial.write(Serial1.read());  // would produce 00000 if used 
// Payload recived overover serial port looks like this 00000. See below for possible data ranges of each par tof array.

int Data1, Data2, Data3, Data4, Data5;

void setup()  
{
 // Open serial communications and wait for port to open:
  Serial1.begin(9600); // Mega 2560
  Serial.begin(9600); //  Serial Com port on PC
}

void loop() // Repeat over and over
{
  
    if(Serial1.available()) // If serial Data detected then process otherwise bypass
    {       // If LCD data avaiable get it
	Serial.println("reading Serial String: ");     //Debug code and works to detect the serial data avaiable
	  while (Serial1.available()< 5) 
            { 
	      for(int n=0; n < 5; n++) 
		LcdRcvPacket[n] = Serial1.read(); // Assign LCD data into a array for processing
	    }
	// Serial.write(Serial1.read());  // would produce 00000 if used as I think the whole While/for loop is not working and not sure why

	// Parse Array into indavidual variables
	Data1 = LcdRcvPacket[0]; 
	Data2 = LcdRcvPacket[1];
	Data3 = LcdRcvPacket[2];
	Data4 = LcdRcvPacket[3];
	Data5 = LcdRcvPacket[4];

        Serial.println(Data1); //Data range 0-1
        Serial.println(Data2); //Data range 0-100
        Serial.println(Data3); //Data range 1-3
        Serial.println(Data4); //Data range 1-3
        Serial.println(Data5); //Data range 0-1
    }
   // If no serial data then do somthing else, or process data detected that was captured.
   
}

When run, serial port sends to Arduino 00000 but it never gets sent to Terminal. Like I said earlier using Serial.write(Serial1.read()); produces 000000

Zero is less than five.
Just saying.

Ok, then I guess I'm missing the fundamentals of For loops. n=o as long as n less than 5 then n = n +1. Do this until n=5 Right?

Based on this ref. ZThe cod eshould be good. http://arduino.cc/en/Reference/For

Maddawg

Ok, then I guess I'm missing the fundamentals of For loops.

No, you are executing the for loop when there are 0, 1, 2, 3, or 4 bytes of serial data to read. If you ever get 5 characters in the buffer, you'll never read again.

You need to turn the less than in the while statement to a greater than or equal. Don't read anything until there are at least 5 bytes to read.

Ok, reviewing and will absorbe then test and comment back in a bit.

Thanks,

Maddawg

Ok so I tried a > 5 and the results worked. Though I'm not sure I get the logic behind it and will break that down for further study. In effort to move on with the test, the results are unexpected but do show. I expected the following to happen:

reading Serial String:
00000

but get the following results which was unexpected:
reading Serial String:
.....
reading Serial String:
.....
reading Serial String:
00000

I had to add Serial1.flush(); to end of loop otherwise it would continuously loop with same serial data. I thought once you read the serial buffer it is cleared. So the flush seemed to handle that. Though I can not explain the tripple posting each time new serial data is detected. I have attached the code with current revisions.

uint8_t LcdRcvPacket[] = { 0, 0, 0, 0, 0}; // payload from Serial1

// Payload sent over serial port like this 00000 (broken out would be 0,000,0,0,0)see below for posisbel data ranges

int Data1, Data2, Data3, Data4, Data5;

void setup()  
{
 // Open serial communications and wait for port to open:
  Serial1.begin(9600); // Mega 2560
  Serial.begin(9600); //  Serial Com port on PC
}

void loop() // Repeat over and over
{
  
    if(Serial1.available()) // If serial Data detected then process otherwise bypass
    {       // If LCD data avaiable get it
	Serial.println("reading Serial String: ");     //Debug code
	  while (Serial1.available() > 5) 
            { 
	      for(int n=0; n < 5; n++) 		
LcdRcvPacket[n] = Serial1.read(); // Assign LCD data into a array for processing
                delay(500);
	    }
	
	// Parse Array into indavidual variables
	Data1 = LcdRcvPacket[0]; 
	Data2 = LcdRcvPacket[1];
	Data3 = LcdRcvPacket[2];
	Data4 = LcdRcvPacket[3];
	Data5 = LcdRcvPacket[4];

        Serial.write(Data1); //Data range 0-1
        Serial.write(Data2); //Data range 0-100
        Serial.write(Data3); //Data range 1-3
        Serial.write(Data4); //Data range 1-3
        Serial.write(Data5); //Data range 0-1
        Serial.println(); 
    }
    Serial1.flush();
   // If no serial data then do somthing else, or process data detected that was captured.
}

Though I'm not sure I get the logic behind it

In your original code, if there is no data in the receive buffer, Serial.available returns zero.
Zero, as I pointed out, is less than five, so you go ahead and read the five bytes of data that aren't in the buffer.

Second issue is I dont know how to handle the 2nd byte in the array, it can be a value of 0-100. If serial data is set for #2 to any value from 10 through 100 then the program loops indeffinently as well. I know that I need to parse it into single bytes and then re-assemble unless there is another way but not sure I know how that works.

Thanks for assisting by the way...

Maddawg

AWOL:
In your original code, if there is no data in the receive buffer, Serial.available returns zero.
Zero, as I pointed out, is less than five, so you go ahead and read the five bytes of data that aren't in the buffer.

I get that if Serial.available is 0 when no data is present. Which means that the While statment and all below it would not even run cause the If statement would be false for no data.

So, the following is what I had interpeted the while statement was doing.

If serial data available (true or greater than 0), and while (serial.available) is less than 5 (true), keep receiving and putting in data into arrray via the for loop. When Serial data is 5, the while statement is finished and we reached end of packet. The If statement has to be true before it will even bother executing the while statement right? I guess I am over anaylizing it. Seems While x < y do something in the while loop when x = y, the while loop terminates.

Sorry, just want to be sure I fully understand this and why it works as you have shown.
I would still like to know why the working result returns three lines of data when I only expected 1 and shown in three posts up.

Shawn

I get that if Serial.available is 0 when no data is present. Which means that the While statment and all below it would not even run cause the If statement would be false for no data.

OK, you get one byte, you read it, and four bytes that aren't.

Don't forget, serial is slooooow

Which means that the While statment and all below it would not even run cause the If statement would be false for no data.

No, it doesn't. It means that the body of the while statement WILL be executed, because 0 is less than 5. What if statement, from your original code, are you talking about?

If you are referring to your new code, AWOL's response is correct.

By the way, if you are sending 5 characters, then you want:

if(Serial1.available() >= 5)
{
    for(int n=0; n < 5; n++)
    {
       LcdRcvPacket[n] = Serial1.read(); // Assign LCD data into a array for processing
    }
}

No while loop and NO delay()! Notice also the >= rather than the >.

By the way, if you are sending 5 characters, then you want:

if(Serial1.available() >= 5)

{
    for(int n=0; n < 5; n++)
    {
       LcdRcvPacket[n] = Serial1.read(); // Assign LCD data into a array for processing
    }
}



No while loop and NO delay()! Notice also the >= rather than the >.

Thanks for the comments in helping understand what I apparently don't.

I was under the impression that the Serial.Available was in sorts a boolean type as that is how it appears when used in code (if serial.available = True or False then process x). So if there is no serial data in the buffer what does the Serial.avaiable() return? If not 0, which is apparently the first byte of the serial packet. is it "" or just empty? I think this is where I have confusion at least in the end and need to better understand that. Cause this is the root of what confused me onthe while statement which I do understand. I was processing what I had assumed was a boolean response.

Again thanks for the lessons in basic coding oo which I thought I had down.

Maddawg

I was under the impression that the Serial.Available was in sorts a boolean type as that is how it appears when used in code (if serial.available = True or False then process x).

Poor examples, then.

Serial.available() returns the number of bytes available to read. This works as a boolean test, since 0 is false and everything else is true, but I don't like code written this way.

So if there is no serial data in the buffer what does the Serial.avaiable() return?

  1. Zero. Zip. Zilch.

If not 0, which is apparently the first byte of the serial packet. is it "" or just empty?

It is zero, but, if you call Serial.read() anyway, it returns -1.

By the way, you have the source code for all the Arduino functions/classes, so you could look at the code or the documentation, instead of making assumptions.