Knowledgebase
emscom > emscom Help Desk > Knowledgebase

Search help:


Read fixed length serial packet

Solution

/* MSSSLTD Demonstration only

software serial library is only included

to allow the code to compile in the IDE

*/

[pre]

include <SoftwareSerial.h>

SoftwareSerial serial_pm(1,2);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
    const uint8_t PACKET_START = 66;
    const uint8_t PACKET_TERM = 255;
    const uint8_t PACKET_LENGTH = 24;
   
    boolean collectPacket = false;
    boolean packetAvailable = false;
  
    uint8_t packetLength = 0;
    uint8_t packet[PACKET_LENGTH +1];

    //read the serial buffer
    while( serial_pm.available() ) {
        char ch = serial_pm.read();
        if(!collectPacket) {                    //start of packet
            collectPacket = (ch == PACKET_START);
            packetAvailable = false;
            packetLength = 0;
        }

        if(collectPacket) {                     //collect packet data
            packet[packetLength++] = ch;
            packet[packetLength] = PACKET_TERM;
        }
       
        if(packetLength == PACKET_LENGTH) {     //finalise
            collectPacket = false;
            packetAvailable = true;
        }
    }

    if(packetAvailable) {
        Serial.println(F("Packet Available"));
    }
}

[/pre]

 
Was this article helpful? yes / no
Related articles Arduino WiFiClient (TCP)
Article details
Article ID: 78
Category: Arduino
Date added: 07-01-2016 07:50:26
Views: 807
Rating (Votes): Article rated 5.0/5.0 (1)

 
« Go back

 
Powered by Help Desk Software HESK, in partnership with SysAid Technologies