Go Down

Topic: Basic Serial (Read 358 times) previous topic - next topic

MrBlister

Hi Everyone,

I'm new to C and the Arduino and really struggling with something (I'm sure) very simple. I've found a number of serial-related examples etc, but nothing that helps. Apologies if my searching is pathetic, but I'm sure help with this will set me flying.

There should be no need to bore you with the whys and wherefores:

Read serial
If byteOne == 176, grab the next byte and call it byteTwo
If byteTwo == 21, grab byteThree and go to a subroutine to use byteTwo and byteThree
Go round again


I can read a byte and blink if it == whatever, I just can't see how to capture successive bytes like this.

A simple example will be very gratefully Rxd! Steve.

HazardsMind

A byte ranges from 0 - 255 or 00000000 to 11111111. There are examples provided in the IDE that may help you. Look at some simple serial communication examples, and play around with them.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

johnyboy

First, you need to save all the byte you see on the serial port (for 1 transmisson) just like this:

Code: [Select]

void serialEvent() {

unsigned char RxBuffer[14];

 if(Serial.available()>=14) {

   for(int n=0; n <= 13; n++){
      RxBuffer[n] = Serial.read();
   }
 }
}


After this, make a function name something like Rx_check();

Code: [Select]

void check_rx(){

 switch(RxSerialState)
 {  
 case 0:                     //  Check for Start Delimiter
   if(RxBuffer[0]== 0x7E){
     RxSerialState=1;
     RxXsum=0x00;
     ValidMsgRxied=0;
     RxError=0;              
   }
   else
     RxSerialState=0;        
   break;  

 case 1:                     // Read Byte Count High              
   RxSerialState=2;
   break;

 case 2:                     // Read Byte Count Low  
   RxByteCount = RxBuffer[2];        
   if ( (RxByteCount < 30) || (RxByteCount > 0) )
     RxSerialState=3;
   else
     RxSerialState=0;
   break;

 case 3:                     // Read API ID
   RxXsum = RxXsum + RxBuffer[3];
   p=4;  
   RxByteCount--;
   if ( RxBuffer[3] == 0x81){           // API Type=0x81 sur XBEE;
     RxSerialState=4;
   }
   else
     RxSerialState=0;
   break;

 case 4:                     // Read Data
   RxXsum=RxXsum+RxBuffer[p];
   p++;
   if (RxByteCount == 0)
   {
     RxSerialState=5;
     RxXsum=255-RxXsum;
   }
   RxByteCount--;
   break;

 case 5:                     // Read XSUM
   if ( (RxXsum==RxBuffer[p]) && (RxByteCount == 0) )
   {
   ValidMsgRxied=1;
   RxSerialState=0;
   }
   else
      RxError=1;
   break;
 }   // end switch

}


This will do the job just fine

PeterH


This will do the job just fine


Assuming you changed 14 to 3, it would do the job but would not be very robust - if there were any errors on the serial port it would get out of sync and never recover.

With those requirements I'd use a state machine to record which bytes in the message header had been seen, implemented as a switch statement, and use that to process each incoming character as it is received (I wouldn't bother buffering the received data).
I only provide help via the forum - please do not contact me for private consultancy.

MrBlister

Thanks everyone, I'll have a play later and let you know how I get on.

Go Up