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

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

2

So we can read and react to data packets over the serial port now, but how do we send a packet from one device to another? Specifically how can one Arduino compatible device send data packets to another? Checkout the Receive Side guide if you haven’t already.

[wdsm_ad id=”61″ ]

#include
char tbs[6];//formatting vector
void sendPacket(char myTarget, int myMessage)
{
if(myMessage > 999) myMessage=999;//truncate data if it is too high.
if(myMessage < −99) myMessage = −99;//truncate low data sprintf(tbs, "||%c%03d",myTarget,(int)myMessage ); Serial.print(tbs);//send command } void sendPacket(char myTarget, char myC1, char myC2, char myC3) { sprintf(tbs, "||%c%c%c%c",myTarget,myC1,myC2,myC3 ); Serial.print(tbs);//send command }

So if we want to easily send multiple message types in the same format, we can use the handy functions I wrote above to ensure that they are all formatted correctly for transmission with a nice easy function call.

If you aren't familiar with the sprintf function here is a quick review of it's functionality. First of all you have use #include to import the appropriate C libraries into your sketch of application source. Secondly you have to establish a character array to which the data can be stored, in our case a six character array called tbs.

After you have that much set up you can use the function sprintf as follows:

sprintf(char *str, const char *format, ...)

Where *str in this case refers to our char array tbs, and const char *format refers to the formatting string we will be using to get the output we want. There are tons of formatting hooks in the sprintf function, but in our case we are going to use %c for a single character, and %d for an integer.

%c is pretty simple, but you probably noticed we don't use just %d in the sprintf functions above, instead we use %03d which gives us specifically three integers in our formatted output, the three is what does that bit, and the zero before the three makes all empty spots get filled with zeros. That is to say if you were to call sprintf(tbs, "||T%03d", 5); your output would be "||T005" which is exactly what you want in the case of a set length data packet.

That brings us to the last major detail of the sprintf function. When you add a %? hook, where ? = whatever type of formatted input you are looking to add, you also have to add a corresponding variable or input for that formatting piece. that is what the ... in the sprintf definition represents. So for the following sprintf(tbs, "||%c%03d", myTarget, (int)myMessage); we have two variables at the end instead of the one we had in the previous example.

Finally on the possibility that function overloads are new to you I will clarify the reason that there are two sendPacket() functions defined. What we are doing by having two definitions with different inputs is creating an overload to the function, so that it can support variations to the input type and still get the expected output message.

void setup()
{
Serial.begin(9600);//setup the serial port for transmission
//do other stuff here too
}
void loop()
{
//sendPacket with method 1: Send a preformatted packet to target
Serial.print("||TABC");
delay(500);
//sendPacket with method 2: Sending a number between −99 and 999 to target
sendPacket('T', 900);
delay(500);
//sendPacket with method 3: Sending 3 characters to target
sendPacket('T', 'A','B','C');
delay(500);
}

In the code above we first set up our serial port to send and receive at 9600 baud, and then we send formatted packets in three different ways.

The first way is pretty simple, we simply transmit a preformed string that is our packet. This method is very easy to do, but in the case of dynamic messaging it is very inefficient. I recommend preformed messages only in the case where you have less than 10 or so different possible messages, any more than that and you become progressively less efficient in your memory usage, and things become difficult to follow and debug later.

The second way is to send an integer as characters, which though easy to translate by a human does present the problem of having to be decoded at the distance end, and is a fairly inefficient use of the available space in a 3 byte message. Just the same, sending a number between -99 and 999 may be easier to do than to handle the numbers or bytes directly yourself, and shouldn't be a problem if that range is sufficient for your application.

The third method is to send characters of your choice in the message to your target. Keep in mind that you can perform any formatting on the characters yourself before transmission, which makes this method the most flexible. Of course you can always just make your own overload that processes a different input type, which often is desirable.

Thanks for reading, and good luck with your nerdly endeavors. I look forward to writing the next guide shortly.

[wdsm_ad id="61" ]

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

Comment(2)

LEAVE YOUR COMMENT

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