Go Down

Topic: Arduino Mega Serial 3 Issue (Read 1 time) previous topic - next topic

viscousflow

Hi I'm not sure exactly where this post would go but I'll try this subforum. I initially thought that this was a project specific issue, but further testing revealed otherwise.

I am using an arduino mega to read serial data off of several IMUs (one at a time). I am able to read the serial data perfectly with a simple loop such as:

Code: [Select]

void loop() {
  if (Serial3.available()>0){
    //for (int i=0; i<3; i++){
      sensorValue[0]=Serial3.read();
    //} 
    //for (int i=0; i<3; i++){
      Serial.println(sensorValue[0],DEC);
    //}
  }
}


Everything comes out squeaky clean. The next step is to assemble the data right? So I write a code like this

Code: [Select]


union u_tag {
    byte b[4];
    float fval;
} b_read;

void loop() {
if(Serial3.read()&&33){ //look for break point (ascii for !)
      for (int j=0; j<3; j++){
        //read serial data
        for (int i=0; i<4; i++){
          b_read.b[i] = Serial3.read();
        }//end read bytes
        angle=b_read.fval*57.3/1000;
        Serial.println(angle);
      }
}


Again, data prints to screen flawlessly.

As soon as I place any form of logic in there, all my serial data seems to disappear. Everything shows up as -1. I am not sure if my serial data is being lost or something but this was extremely strange. Here is the code I am using:

Code: [Select]
void loop() {
 
  flag=0;
  while (flag<1){
    h1=(Serial3.read()&&13);//line feed header
    h2=(Serial3.read()&&10);//carriage return header
    if(h1&&h2){
      for (int j=0; j<3; j++){
        val=Serial3.read();//skip ! marker
        //read serial data
        for (int i=0; i<4; i++){
          b_read.b[i] = Serial3.read();
        }//end read bytes
        Euler[j]=b_read.fval*57.3/1000;
        Serial.println(Euler[j]);
      }//end read 3 points
      //found header now leave
      flag=1;
    }//end found header
  }//end wait on header

}


I've double checked my logic multiple times and the logic is perfectly fine because it does indeed enter the for loop and does print values! I figured there was some buffer issue because I even put in a Serial.available() in there and it keeps returning zero. Strange!! How does it enter the loop and then return zero on the next reading?? Obviously data must be available for it to enter the loop, then print something. I am not sure what is going on here, if this is some buffer issue, is there some way I can fix it?

viscousflow

correction: that was "Serial3.available()"

Bainesbunch

Hi I am playing with the serial comms as well in a project to read an RFID send the data onto the net through a TCP/IP transport stream \nd update an LCD tough screen.

Perhaps your issue is that you are trying to read the port too quickly I.E.

when doing the following

    h1=(Serial3.read()&&13);//line feed header
    h2=(Serial3.read()&&10);//carriage return header

You need to be sure that there is a byte in the buffer to read the first time but you don't check to see if there is a byte to read before trying to check for the CR normally the CR'/LF pair come in in that order too CR then LF

For what it is worth I tend to check if there is a byte in the buffer then go into a loop reading the data stream into another buffer until either my line terminator is reached (CR) or their are no more bytes in the RX buffer

once my line terminator is received i then jump into a sub routine where i process what i have received before re-entering the loop if there are more bytes in the rx buffer.


here is a bit of code that might help you, dont forget to change the serial device to use the device you are using.



Code: [Select]



String CommandString = "";

void loop()
{

if (Serial.available() > 0) { buildCommandString() }

// rest of your main loop code in here

}



void buildCommandString()
{
int NextByte;

while ((NextByte != 13) && (Serial.available() > 0) )
{
NextByte = Serial.read();

if (NextByte == 13) // pick your own line terminator
{
// your code in here, dont forget to clear down CommandString before returning to here if you have processed the //command
}
else
{
CommandString += char(NextByte); // add the byte to the command string
delay(10);   // wait to see if we have any more bytes comming in
}

}

}



Cheers Pete.

EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Go Up