Home Arduino Code Passing your own data packets over Serial Port (Receive side)
Passing your own data packets over Serial Port (Receive side)

Passing your own data packets over Serial Port (Receive side)


I recently had to tackle a problem that required sending multiple messages from one Versalino/Arduino to another through a Serial port.  In order to solve the problem, I came up with my own modifiable communication protocol that may help if you have a similar problem to solve.

Above is the declaration of the structure that is used to store and access the data packet that was last received by the micro-controller, and the subsequent creation of an instance of the structure we will call myPacket.

struct VirtuabotixPacket {
boolean received;
char target;
char msg1;
char msg2;
char msg3;

VirtuabotixPacket myPacket; //this is where we declare an instance of the structure.

The structure above has a boolean state variable to determine whether or not the message is new, a target byte, and 3 message bytes (you can add more message bytes, or increase the target name size very simply if you need to).

boolean listenforcommand()
if(Serial.available() >=6)
if(Serial.read()=='|' && Serial.read()=='|')
else myPacket.received=false;
} else myPacket.received=false;
return myPacket.received;

Of course we have to add a function to actually parse the packets from the Serial datastream, and the way we do this is by first checking to see that we have at least 6 characters (the number of bytes in one packet), and then we look for the proper header of “||” if we do not see the double pipe we skip reading further until at least 6 bytes are available again, because we have jumped in on the middle of a packet, or on a poorly formatted packet.

Once the double pipe ( || ) is received we can process the message further by parsing out the individual components of the message and assigning them to our structure.

Those components are the Target, the Message byte 1, byte 2, and byte 3. Finally we return a true if our message was parsed successfully, and a false otherwise.

Same power and half the cost of other name brand Unos!

void setup()
//Set up our serial port at the baud rate we wish to pass messages at
//Additional setup items below
//........we will set up an led to control
pinMode(13, OUTPUT);
digitalWrite(13, LOW);//let's turn off the LED on pin 13 for now.

Above we set up our serial port, and any other devices we need to control with our packets.

void loop()
while(listenforcommand())//loop while messages are still in the queue
if (myPacket.received==true && myPacket.target=='L') //if our target "L"
if(myPacket.msg1 == "H") digitalWrite(13, HIGH); //turns on the LED
else digitalWrite(13, LOW); //turns off LED if the first message byte is not "H"
//you could also use myPacket.msg2 for more command data
//you could also use myPacket.msg3 for even more command data
if (myPacket.received==true && myPacket.target=='B') //optionally you can have up to 255 other targets
//you can add message processing here just like you did above

As you can see above the processing of the packets is fairly simple, all you have to do is decide what your target name is (single character with this method) and take whatever action on the message that you want inside the if statement for the target.

One of the most interesting applications of this system is to send packets to multiple devices, and have your message control the same target on all systems connected to the receive line.

Note: Only one device can be connected to the receive of the commanding device and still allow communication, but multiple slave devices can receive from a single tx line.

You can test the sketch by sending the following commands through your Serial terminal “||LHHH” to turn the LED on, and “||LOOO” or anything other than an H for the first message byte to turn off the LED.

Joseph Dattilo Writer, Electrical Engineer, CEO and founder of Virtuabotix LLC, and completely crazy in every way.



Your email address will not be published. Required fields are marked *