Pages: [1]   Go Down
Author Topic: Basic Serial  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Hythe, Hampshire, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 108
Posts: 3778
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Hythe, Hampshire, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: