Message Link#
Introduction#
The message_link class lets two V5 Brains communicate by sending short string messages with optional small data. It’s designed for robot-to-robot coordination — like starting routines, sharing sensor results, or triggering behaviors on another robot.
Every usage of send adds a message to the linked V5 Brain’s queue, and the queue is read first-in, first-out (FIFO). If multiple messages are sent before the other V5 Brain uses receive, they will be stored and returned one at a time in the order they were sent. Because messages can queue, repeatedly sending the same status every loop may create backlog; for time-critical logic, send only when values change.
Important: Both robots must be running projects that use message_link at the same time, or no messages will be sent/received.
Class Constructors#
message_link(
int32_t index,
const char *name,
linkType type,
bool isWired = false );
Class Destructor#
Destroys the message_link object and releases associated resources.
virtual ~message_link();
Parameters#
Parameter |
Type |
Description |
|---|---|---|
|
|
The Smart Port that the V5 Radio, or the Smart Cable, is connected to, written as |
|
|
The name of the Message Link. |
|
|
The type of link, either
|
|
|
Whether the message_link object is wired.
|
Example#
Code for Robot 1
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredCode for Robot 2
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
Member Functions#
The message_link class includes the following member functions:
isLinked— Returns whether this Brain is actively connected to its paired Brain.send— Sends a message to the paired Brain.receive— Waits for and returns the next incoming message.received— Registers a function to be called whenever a new message is received.
Before calling any message_link member functions, a message_link instance must be created, as shown below:
Code for Robot 1
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::manager, // type true); // isWiredCode for Robot 2
// Create a wireless link in Port 1 message_link link = message_link( PORT1, // index "VEXRoboticsLink123456789", // name linkType::worker, // type true); // isWired
isLinked#
isLinked method returns whether the V5 Brains on a MessageLink are paired with one another.
bool isLinked();
This function does not have any parameters.
Return ValuesThis function returns a boolean value.
true– The two V5 Brains are paired and communicating on this link.false– The two V5 Brains are not paired on this link.
It is good practice to always check to ensure the V5 Brains are linked at the start of a project before running any further code.
ExamplesCode for Robot 1
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } Brain.Screen.print("Robot 1 - Manager"); // Continuously send if the screen is being pressed while (true){ if (Brain.Screen.pressing()){ link.send("pressed"); } else { link.send("released"); } wait(0.05, seconds); }Code for Robot 2
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
send#
Sends a message over the link connection.
Available FunctionsParameters1 — Sends a message string.
int32_t send( const char *message );2 — Sends a message string with a
double.int32_t send( const char *message, double value );3 — Sends a message string with an
int32_tand adouble.int32_t send( const char *message, int32_t index, double value );
Parameter |
Type |
Description |
|---|---|---|
|
|
The message to send to the other linked V5 Brain. |
|
|
An integer to send to the other linked V5 Brain. |
|
|
A double to send to the other linked V5 Brain. Doubles are formatted to 4 decimal places; if fewer decimals are provided, trailing zeros are added until 4 are shown. |
This function does not return any values.
NotesIf only message is sent, the other linked V5 Brain receives that exact message.
If value (and index if provided) are included, the receiving Brain gets a single encoded message that combines all fields in this order: .message_index_value
Using link.send(“string”, 1, 2.55) is received as .string_1_2.5500.
Code for Robot 1
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } Brain.Screen.print("Robot 1 - Manager"); // Continuously send if the screen is being pressed while (true){ if (Brain.Screen.pressing()){ link.send("pressed"); } else { link.send("released"); } wait(0.05, seconds); }Code for Robot 2
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
receive#
Receives a message from the link connection.
Available Functionsint32_t receive(
char *buffer,
int32_t length,
int32_t timeoutMs = 500 );
Parameter |
Type |
Description |
|---|---|---|
|
|
The buffer to store the received message. |
|
|
The length of the buffer. |
|
|
How long in milliseconds |
This function returns the number of bytes received.
Notesreceivereturns the next queued message from the other linked V5 Brain. Messages are read in FIFO order (oldest unread first). If the queue is empty whenreceiveis called, it waits up to the specifiedtimeoutfor a new message. If no message arrives in that window,receivereturns0, and any message sent afterward remains in the queue to be read the next timereceiveis used.If the other linked V5 Brain sent only a
string,receivereturns that exact string.If the other linked V5 Brain sent an integer and/or float,
receivereturns a single encoded string that combines all fields in this order:.string_integer_floatFor example, if the other linked V5 Brain uses
link.send(“string”, 1, 2.55), then usinglink.receive()returns an encoded string such as.string_1_2.5500. You can split this encoded string (see the examples below) to extract the message name, integer, and float values.If you’d rather avoid manual splitting/parsing, use
receivedto register a handler for a specific message name; the handler receives the sent values as arguments.
Using link.send(“string”, 1, 2.55) is received as .string_1_2.5500.
Code for Robot 1
// Tell the other robot when the screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } Brain.Screen.print("Robot 1 - Manager"); // Continuously send if the screen is being pressed while (true){ if (Brain.Screen.pressing()){ link.send("pressed"); } else { link.send("released"); } wait(0.05, seconds); }Code for Robot 2
// Display if the other robot's screen is being pressed // Create the link message_link link = message_link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()){ wait(0.1, seconds); } // Continuously check if Robot 1 has sent "pressed" while (true){ Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); char msg[32] = {0}; // buffer for incoming message int32_t n = link.receive(msg, sizeof(msg), 100); // wait up to 100ms if (n > 0 && strcmp(msg, "pressed") == 0) { Brain.Screen.newLine(); Brain.Screen.print("Manager is being pressed!"); } wait(0.05, seconds); }
received#
Registers a function to be called whenever the V5 Brain receives a sent message.
Available FunctionsParameters1 Registers a callback for all received messages (message + value).
void received( void (* callback)(const char *, const char *, double) );2 Registers a callback for all received messages (message + index + value).
void received( void (* callback)(const char *, const char *, int32_t, double) );3 Registers a callback for a specific message (message + value).
void received( const char *message, void (* callback)(const char *, const char *, double) );4 Registers a callback for a specific message (message + index + value).
void received( const char *message, void (* callback)(const char *, const char *, int32_t, double) );
Parameter |
Type |
Description |
|---|---|---|
|
|
The message name to listen for. If omitted, the callback runs for all received messages. |
|
Function pointer |
A function that executes when a message is received. |
This function does not return any values.
Callback Signature1 Receives sender name, message name, and value.
void callback(const char *, const char *, double)2 Receives sender name, message name, index, and value.
void callback(const char *, const char *, int32_t, double)
Type |
Description |
|---|---|
1st |
The sender link name. |
2nd |
The message name received. |
|
The index associated with the message. |
|
The value associated with the message. |
Code for Robot 1
// Send whether screen is pressed on left or right void screen_pressed() { if (Brain.Screen.xPosition() < 240) { link.send("left"); } else { link.send("right"); } } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create the link message_link link(PORT1, "LeftRightLink", linkType::manager, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } Brain.Screen.print("Robot 1 - Manager"); Brain.Screen.pressed(screen_pressed); while (true) { wait(10, msec); } }Code for Robot 2
// Create variable to track where screen is pressed int lastPress = 0; // Update the variable whenever a new message is received void message_received(const char* message, const char* linkname, double timestamp) { if (strcmp(message, "left") == 0) lastPress = 1; if (strcmp(message, "right") == 0) lastPress = 2; } int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Create the link message_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true); // Do not run code UNTIL the Brains are linked while (!link.isLinked()) { wait(0.1, seconds); } link.received(message_received); // Display if Robot 1's screen was pressed on the left or right while (true) { Brain.Screen.clearScreen(); Brain.Screen.setCursor(1, 1); Brain.Screen.print("Robot 2 - Worker"); Brain.Screen.newLine(); if (lastPress == 1) { Brain.Screen.print("Manager pressed LEFT"); } else if (lastPress == 2) { Brain.Screen.print("Manager pressed RIGHT"); } else { Brain.Screen.print("Waiting..."); } wait(0.1, seconds); } }