We have had several posts delving into the advanced use of the serial port on a processor like Arduino. We have seen sending and receiving data as bytes, and how to apply it to send and receive equal elements in an array. In this post, we will generalize the concept to send or receive an object or structure via serial port.
Before diving into the code, let’s explain the interest we have in sending and receiving an object or structure via serial port, putting it into context. It is common to have a project where we need two or more processors to communicate, and where we can define the behavior of both the sender and the receiver (it’s not a communication imposed on us). For example, controlling a vehicle from a mobile phone or a robotic arm from a computer.
We need the processors to exchange specific information, which depends on the project we are doing. For this, we define one or several messages that contain this data, which can consist of any grouping of other basic variables or objects.
Furthermore, as a general rule, we are interested in sending and receiving messages completely and, after verifying that the message is correct, executing an action with the data.
If we define a structure that contains our message, encoded in a mutually agreed way between the sender and receiver, and send the structure via serial port as bytes, the message is sent and received directly in a simple way, without the need for intermediate conversions or processing.
Remember that different processors may use a different number of bytes to represent basic types, in which case we will have to take it into account in the sender or receiver.
How complicated is this “pro” way of communication? Curiously, much simpler than dealing with text, splitting files by commas, and converting variables.
Sending a structure via serial port is as simple as,
void sendStructure(byte *structurePointer, int structureLength)
{
Serial.write(structurePointer, structureLength);
}
sendStructure((byte*)&myStruct, sizeof(myStruct));
Receiving the structure via serial port is similar, simply,
void recieveStructure(byte *structurePointer, int structureLength)
{
Serial.readBytes(structurePointer, structureLength);
}
recieveStructure((byte*)&myStruct, sizeof(myStruct));
Example: Sending a Structure via Serial Port
Simple, right? Let’s see it in a hypothetical example where we send the position of a servo to a robot, and the time in which we want the movement to be made.
The sender code would be as follows,
struct servoMoveMessage
{
int servoNum;
int positionGoal;
float interval;
};
struct servoMoveMessage message;
void sendStructure(byte *structurePointer, int structureLength)
{
Serial.write(structurePointer, structureLength);
}
void setup()
{
Serial.begin(9600);
message.servoNum = 10;
message.positionGoal = 1200;
message.interval = 2.5;
sendStructure((byte*)&message, sizeof(message));
}
void loop()
{
}
And the receiver code
struct servoMoveMessage
{
int servoNum;
int positionGoal;
float interval;
};
struct servoMoveMessage message;
void recieveStructure(byte *structurePointer, int structureLength)
{
if(Serial.available() < sizeof(message)) return;
Serial.readBytes(structurePointer, structureLength);
}
void setup()
{
Serial.begin(9600);
recieveStructure((byte*)&message, sizeof(message));
}
void loop()
{
}
Of course, we can still improve the process a lot. For example, we can establish mechanisms to verify that the received data is correct (frame delimiters, checksums), a watchdog. We will see all this in upcoming posts.
Download the Code
All the code from this post is available for download on Github.

