Re: Serial Input Basics

Great Code, thank you very much! :slight_smile:

I have made some changes to my needs and have put the if(Serial.available() >0 part in the ISR:

//### RS232 ISR ###############################################################
void serialEvent(){                         // Serial rx-ISR

  while(Serial.available() > 0) {           // read in RS232 
    char x = Serial.read();

      // the order of these IF clauses is significant
      
    if (x == endMarker) {
      readInProgress = false;
      newDataFromPC = true;
      inputBuffer[bytesRecvd] = 0;
      parseData();                          // Parse Command and Values
      updateData_reply();                   // Process according to Command
    }

    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;
      bytesRecvd ++;
      if (bytesRecvd == buffSize) {
        bytesRecvd = buffSize - 1;
      }
    }

    if (x == startMarker) { 
      bytesRecvd = 0; 
      readInProgress = true;
    }
  }//if(Serial.available() > 0) {
}//void serialEvent()

parsedata() is pritty much the same, except: I have added to parse 3x int and 1x float:

//### ParseData() #############################################################
void parseData() {   // split the data into its parts: 3x int, 1x float
    
  char * strtokIndx;                    // this is used by strtok() as an index
  
  strtokIndx = strtok(inputBuffer,","); // get the first part - the string
  strcpy(comandFromPC, strtokIndx);    // copy it to comandFromPC
  
  strtokIndx = strtok(NULL, ",");       // this continues where the previous call left off
  val1 = atoi(strtokIndx);              // convert this part to an integer
  
  strtokIndx = strtok(NULL, ",");       // this continues where the previous call left off
  val2 = atoi(strtokIndx);              // convert this part to an integer
  
  strtokIndx = strtok(NULL, ",");       // this continues where the previous call left off
  val3 = atoi(strtokIndx);              // convert this part to an integer
  
  strtokIndx = strtok(NULL, ","); 
  val4 = atof(strtokIndx);              // convert this part to a float
}//void parseData() {

the update Data and reply is changed to:

//### UpdateData and Reply() ##################################################
void updateData_reply() {                   // Process according to Command 

  if (newDataFromPC) {
    newDataFromPC = false;
    // this illustrates using different inputs to call different functions
    
    // LED12 Command
    if ((strcmp(comandFromPC, "LED12") == 0)||(strcmp(comandFromPC, "led12") == 0)){
      if(val1>0) { Don(12); SPLF("LED12 switched on.");}
      else { Doff(12); SPLF("LED12 switched off.");}
    }//if ((strcmp(comandFromPC, "LED12") == 0)||(strcmp(comandFromPC, "led12") == 0)){
    
    // FCN Command
    else if((strcmp(comandFromPC, "FCN") == 0)||(strcmp(comandFromPC, "fcn") == 0)){
      // ...do something with FCN/fcn
      
      reply2pc();                           // Send standard answer to RS232
    }//else if((strcmp(comandFromPC, "FCN") == 0)||(strcmp(comandFromPC, "fcn") == 0)){

    // any other unknown Command  
    else{                                               
      reply2pc();                           // Send standard answer to RS232
      SPLF(" Command unnkown!\n try: \"<COMMAND, Int1, Int2, Int3, Float>\"");
      SPLF(" e.g.: <LED12,1> to switch LED on Pin 12 on ('0' = off)");
    }//else
    
  }//if (newDataFromPC)
}//void updateData_reply() {

so that the loop() is pretty empty:

//### Loop() ##################################################################
void loop() {
      
  //...add your Code here

  LED13tog();                               // toggle LED 13 to show its alive
  delay(500);                               // just a delay to simmulate load
  
}//loop()

and setup, just for the sake of completeness:

#include "myHelpers.h"                      // my defines for SP, LED13, D i/o

//### Serial Parse global Variables  ##########################################
const byte    buffSize          = 20;       // Serial recive Buffer size [Byte]
const byte    cmdSize           = 10;       // Serial recive Command size [Byte]
char          inputBuffer[buffSize];        // Serial Buffer
const char    startMarker       = '<';      // Start Marker
const char    endMarker         = '\n';     // End Marker
byte          bytesRecvd        = 0;        // Received bytes
boolean       readInProgress    = false;    // still reading in 
boolean       newDataFromPC     = false;    // finished reding, new Data received
char          comandFromPC[cmdSize] = {0}; // Command buffer
int           val1              = 0;        // 1.st integer from PC
int           val2              = 0;        // 2.nd integer from PC
int           val3              = 0;        // 3.rd integer from PC       
float         val4              = 0.0;      // 4.th Float from PC


//### Setup() #################################################################
void setup() {
  SPsetup();                                // Serial Setup and print Start-Info
  LED13Setup();                             // LED13 Setup
  DO(12);                                   // Set Digital Output Pin 12 (LED12)
  SPL();
  SPLF(" Beispiel 4x Serial-Parse\n Type: \"<COMAND, INTEGER_1, INTEGER_2, INTEGER_3, FLOAT_x.yz>\"");

}//setup()

myHelpers.h is just defining SP / SPL / SPLF as Serial.print/ln(F(... and LED13on/off/toggle

Thanks again, for the great, short and efficient code!!

_150627_Test_Serial_Parse_V003.ino (5.25 KB)

myHelpers.h (4.28 KB)

EDIT ... for the benefit of other readers this Thread refers to the serial input basics tutorial Thread

jim_beam:
Great Code, thank you very much! :slight_smile:

Thank you for your kind words. However as you seem to have made major changes I would have preferred if you had put your code in a new Thread rather than confuse my Tutorial. I will ask the moderator to move it.

Note that serialEvent() is NOT an ISR - it is just a function that is called at the end of loop() ( from the function main() ) if there is data in the serial input buffer. I have deliberately NOT used it in my examples to avoid the confusion that it gives rise to.

The Serial functions should not be used in an ISR.

...R

Hello Robin2,

sorry for that code mess and confusing. It is probably better to move it.

Thank you for the feedback on the serialEvent()-Function.
What do I have to search for to learn more abut the "build in Functions" of the Arduino (like serialEvent())?

cheers

jim_beam:
What do I have to search for to learn more abut the "build in Functions" of the Arduino (like serialEvent())?

The Reference section has the basic information. Unfortunately it does not have much info on serialEvent(). Documentation is generally the weak point of OpenSource software.

If you are enthusiastic, all the Arduino source code is included with the Arduino IDE so you can study it.

...R