Skip to main content
2 of 2
Added C++ language formatting tag, other formatting improvements.
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Arduino reading serial data non-blocking

I have an Arduino reading serial data and responding to other inputs as well, I'm trying to read incoming serial data without using the while, if at all possible. the incoming serial data will be formatted in a specific manner each time and will be CSV; It will look like such:

19,1400700083, 1-0,1-10,and other values delimited with a new line character "/n".

The first value will tell how many bytes are coming in so the serial receive function wont parse data until it gets all the bytes,second value is a Unix time-stamp, the third value (1-0) is the "get" or set" value; which will tell the Arduino to return a value or to set a value. The fourth (1-10) value will correspond to what variable to get or set. and the other values will be integers as well but are dependent on the other values.

My code is as follows:

void CheckSerialData() { // begin function
  
  if(Serial.available() >= 2) { // begin one, first line will be how many bytes incoming.
  int NumberOfIncomingBytes = Serial.parseInt();
    if(Serial.available() >= NumberOfIncomingBytes) { //begin two
    int IncomingTime = Serial.parseInt();
    setTime(IncomingTime); //time is always incoming with serial values
    int GetOrSetValue = Serial.parseInt(); //this value should be 1 or 0 for "getting values(1)" or "setting values(0)"
      if(GetOrSetValue == 1) { //BEGIN THREE
        int ValueToGet = Serial.parseInt(); 
          switch(ValueToGet) {// BEGIN SWITCH
            case 0:
              Serial.print(GetTemp());
              Serial.print("/n");
              break;
            case 1:
              //serialwrite yaw pitch roll
              break;
            case 2:
              Serial.print(GetVoltage());
              Serial.print("/n");
              break;
            case 3:
              //append all RGB final values together and serial print it
              break;
            case 4:
              //print out accel values
              break;
            case 5:
              //print alarm alarmed status
              break;
            case 6:
              //print out last time alarm set off
              break;
            case 7:
              //print out time allowed lights to turn on
              break;
            case 8:
              //print out arduino time 
              break;
            case 9:
              //print out system summary, (time,voltage,temp,yaw,pitch,roll,accel
              break;
           
          }//END SWITCH
         }//END THREE 
      else { //BEGIN TWO B //SET VALUES
      int ValueToGet = Serial.parseInt(); 
          switch(ValueToGet) {// BEGIN SWITCH
            case 0:
              //set global rgb value set LEFT
              break;
            case 1:
              //set global rgb value set RIGHT
              break;
            case 2:
              //set global alarm value
              break;
            case 3:
              //set global angle to start flashing emergency led's
              break;
            case 4:
              //set turn signal RGB value
              break;
            case 5:
              //set time that lights are ok to turn on
              break;
            case 6:
              //set light override value
              break;
            case 7:
              //set pattern enable
              break;
              
          
          } //END SWITCH
         
      }//END TWO B
      
          }
        }//end two 
      }//end one
    }//end function
    //**********************END SERIAL FUNCTION**************************

I would like to know if this looks like it can work, or am I going about this all wrong?

(I don't have access to my Arduino currently to test the code)