Home Arduino Code Passing your own data packets over Serial Port (Changing packet size)
Passing your own data packets over Serial Port (Changing packet size)

Passing your own data packets over Serial Port (Changing packet size)

1

So we made a packet system for sending data to and from our Arduino/Versalino systems, but we only gave ourselves 3 message bytes to work with.

Though this covers us for a bunch of different applications, it just so happens we needed one more byte of data to work with the format of data we wanted to use with a control system for the Versalino Rove, so I thought this was a perfect opportunity to go over the details that have to change to make it possible to add an extra byte to our message. The instructions that follow can essentially be repeated as needed to add even more message bytes, and can be followed in reverse to remove them.

struct VirtuabotixPacket {
boolean received;
char target;
char msg1;
char msg2;
char msg3;
char msg4; //this is where we add the new message byte.
};
VirtuabotixPacket myPacket;

Note that the only change we made to the declaration structure was to add char msg4; to it. Everything else can stay the same in this block of code.

boolean listenforcommand()
{
if(Serial.available() >=7) //this line was >= 6 we changed it to >= 7
{
if(Serial.read()=='|' && Serial.read()=='|')
{
myPacket.received=true;
myPacket.target=Serial.read();
myPacket.msg1=Serial.read();
myPacket.msg2=Serial.read();
myPacket.msg3=Serial.read();
myPacket.msg4=Serial.read();//<−added
//We added the line above to read in the extra message byte into msg4.
}
else myPacket.received=false;
} else myPacket.received=false;
return myPacket.received;
}

Just like before the code group above is designed to handle the incoming packets, and the only changes are adding one to the packet length making it 6 instead of 7, and adding a line to read and store the serial data for msg4 (char 4 of the message content). Otherwise the code above is unchanged.

Don’t forget that you can repeat the steps above as many times as you like (to the limits of your available micro-controller SRAM of course) to further increase the number of bytes sent in your packets.

Note: If you wanted to eliminate bytes from the message it’s size can also be decremented by removing the last message byte from the serial retrieval function, and the packet structure. Don’t forget to decrement the message length as well.

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

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
//-> you could also use myPacket.msg4 now that you added it! Yay!
}
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
}
}
}

There was not need to change anything in our setup to make these changes work, and the only thing that changed in processing portion of our sketch is that now we have an additional message byte available for processing. You can still process most messages as if they were only 3 bytes long though. The only time that you need to ensure you are processing the whole message is if you design it that way, or if you are trying to convert ascii to integer from your master application (which just so happens to be the next thing we will be doing).

For now I hope that this helps if you ever need to modify the VirtuabotixPacket structure for your own applications.

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

Comment(1)

LEAVE YOUR COMMENT

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