• VEXcode Robotics Logo VEX Robotics Logo
  • VEX API Home Button VEX API Home Button
  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white
跳至主要内容
Ctrl+K

< Back to Platform Select

  • VEX V5
  • VEX V5 API 参考
  • VEXlink
  • 消息链接
简体中文
  • English
  • العربية
  • বাংলা
  • Česky
  • Deutsch
  • Ελληνικά
  • Español
  • Español Latinoamericano
  • Suomi
  • Français
  • Gaeilge
  • हिंदी
  • Magyar
  • Bahasa Indonesia
  • 日本語
  • 한국어
  • Lietuvių
  • Latviešu
  • മലയാളം
  • Nederlands
  • Polski
  • Português
  • Русский
  • Svenska
  • ไทย
  • Türkçe
  • Українська
  • Tiếng Việt
  • 繁體中文
  • VEXcode Robotics Logo

版块导航

  • 区块
  • Python
  • C++
    • 传动系统
    • 电机和电机控制器
    • 控制器
    • 脑
    • Sound
    • 竞赛
    • 智能端口设备
    • 三线制设备
    • 安慰
    • 逻辑
    • VEXlink
      • 消息链接
      • 串行链路
    • CTE 工作单元
  • VEXcode教程

平台导航

  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white

消息链接#

  • 介绍

  • 类构造函数

  • 类析构函数

  • 参数

  • 例子

  • 成员功能

    • isLinked

    • 发送

    • 收到

    • 已收到

介绍#

message_link 类允许两个 V5 大脑通过发送带有可选小数据的短字符串消息进行通信。它专为机器人间的协调而设计,例如启动程序、共享传感器结果或触发另一个机器人的行为。

每次调用 send 都会向关联的 V5 Brain 的队列中添加一条消息,队列采用先进先出 (FIFO) 的读取方式。如果在另一个 V5 Brain 使用 receive 之前发送了多条消息,这些消息将被存储,并按发送顺序逐条返回。由于消息可以排队,因此在每次循环中重复发送相同的状态可能会导致积压;对于时间关键型逻辑,仅当值发生变化时才发送消息。

重要提示:两个机器人必须同时运行使用 message_link 项目,否则将不会发送/接收任何消息。

类构造函数#

message_link( 
  int32_t     index, 
  const char  *name, 
  linkType    type, 
  bool        isWired = false );

类析构函数#

销毁 message_link 对象并释放相关资源。

virtual ~message_link();

参数#

范围

类型

描述

index

int32_t

V5 无线电或智能电缆连接的智能端口,记为 PORTx,其中 x 是端口号(例如, PORT1)。

name

char

消息链接的名称。

type

linkType

The type of link, either linkType::manager or linkType::worker. This information is used to correctly configure the radio and also determines available bandwidth for transmission and reception. A manager robot has double the available bandwidth (1040 bytes/second) to send information to the worker robot (520 bytes/second).

  • linkType::manager - The message_link object is a manager.
  • linkType::worker - The message_link object is a worker.

isWired

bool

Whether the message_link object is wired.

  • true - The message_link object is wired.
  • false - The message_link object is wireless.

例子#

机器人 1 的代码

// Create a wireless link in Port 1
message_link link = message_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::manager, // type
  true); // isWired

机器人2的代码

// Create a wireless link in Port 1
message_link link = message_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::worker, // type
  true); // isWired

成员功能#

message_link 类包含以下成员函数:

  • isLinked — 返回此大脑是否与其配对的大脑处于活动连接状态。

  • send — 向配对的大脑发送消息。

  • receive — 等待并返回下一个传入的消息。

  • received — 注册一个函数,每当收到新消息时调用该函数。

在调用任何 message_link 函数之前,必须先创建一个 message_link ,如下所示:

机器人 1 的代码

// Create a wireless link in Port 1
message_link link = message_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::manager, // type
  true); // isWired

机器人2的代码

// Create a wireless link in Port 1
message_link link = message_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::worker, // type
  true); // isWired

isLinked#

isLinked 方法返回 MessageLink 上的 V5 Brains 是否彼此配对。

Available Functions
bool isLinked();

Parameters

此函数没有任何参数。

Return Values

此函数返回一个布尔值。

  • true – 两个 V5 大脑已配对并通过此链路进行通信。

  • false – 此链接中的两个 V5 大脑未配对。

Notes

在项目开始时,运行任何其他代码之前,最好始终检查以确保 V5 Brains 已连接。

Examples

机器人 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);
  }

机器人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#

通过链路连接发送消息。

Available Functions

1 — 发送消息字符串。

int32_t send( 
  const char *message );

2 — 发送一个包含 double消息字符串。

int32_t send( 
  const char *message, 
  double      value );

3 — 发送一个包含 int32_t 和 double的消息字符串。

int32_t send(
  const char *message, 
  int32_t     index, 
  double      value );

Parameters

范围

类型

描述

message

const char*

要发送给另一个已连接的 V5 大脑的消息。

index

int32_t

要发送给另一个已连接的 V5 大脑的整数。

value

double

发送给另一个已连接的 V5 Brain 的双精度浮点数。双精度浮点数默认显示 4 位小数;如果提供的位数少于 4 位,则会在末尾添加零,直到显示 4 位小数为止。

Return Values

此函数不返回任何值。

Notes

如果只发送 message ,则另一个连接的 V5 大脑会收到完全相同的消息。

如果包含 value (以及提供的 index ),则接收端 Brain 将收到一条编码后的消息,该消息按以下顺序组合所有字段: .message_index_value

Examples

使用 link.send(“string”, 1, 2.55) 被接收为 .string_1_2.5500。

机器人 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);
}

机器人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#

接收来自链路连接的消息。

Available Functions
int32_t receive(
  char     *buffer,
  int32_t   length, 
  int32_t   timeoutMs = 500 );

Parameters

范围

类型

描述

buffer

char*

用于存储接收到的消息的缓冲区。

length

int32_t

缓冲区长度。

timeoutMs

int32_t

receive 等待新消息的时长(以毫秒为单位),仅当队列为空时才执行。默认值为 300,000(300 秒)。

Return Values

此函数返回接收到的字节数。

Notes
  • receive 返回来自另一个已连接的 V5 Brain 的下一条已排队的消息。消息按先进先出 (FIFO) 顺序读取(最旧的未读消息优先)。如果在调用 receive 时队列为空,则会等待最多 receive 时间以接收新消息。如果在该时间窗口内没有消息到达,则 receive 返回 0,之后发送的任何消息都将保留在队列中,以便在下次使用 timeout 时读取。

  • If the other linked V5 Brain sent only a string, receive returns that exact string.

  • 如果另一个连接的 V5 Brain 发送的是整数和/或浮点数, receive 返回一个编码字符串,该字符串按以下顺序组合所有字段: .string_integer_float

    • 例如,如果另一个链接的 V5 Brain 使用 link.send(“string”, 1, 2.55),那么使用 link.receive() 将返回一个编码字符串,例如 .string_1_2.5500您可以拆分此编码字符串(参见下面的示例)以提取消息名称、整数和浮点值。

    • 如果您不想手动拆分/解析,请使用 received 为特定消息名称注册处理程序;处理程序接收发送的值作为参数。

Examples

使用 link.send(“string”, 1, 2.55) 被接收为 .string_1_2.5500。

机器人 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);
}

机器人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#

注册一个函数,当 V5 大脑收到发送的消息时,该函数将被调用。

Available Functions

1 为所有收到的消息(消息 + 值)注册回调函数。

void received(
    void (* callback)(const char *, const char *, double)
);

2 为所有收到的消息注册一个回调函数(消息 + 索引 + 值)。

void received(
    void (* callback)(const char *, const char *, int32_t, double)
);

3 为特定消息(消息 + 值)注册回调。

void received(
    const char *message,
    void      (* callback)(const char *, const char *, double)
);

4 为特定消息注册回调(消息 + 索引 + 值)。

void received(
    const char *message,
    void      (* callback)(const char *, const char *, int32_t, double)
);

Parameters

范围

类型

描述

message

const char *

要监听的消息名称。如果省略,则回调函数将对所有接收到的消息运行。

callback

void (*)(const char *, const char , double) or void ()(const char *, const char *, int32_t, double)

收到消息时执行的函数。

Return Values

此函数不返回任何值。

Callback Signature

1 接收发件人姓名、消息名称和值。

void callback(const char *, const char *, double)

2 接收发件人姓名、消息名称、索引和值。

void callback(const char *, const char *, int32_t, double)

类型

描述

1st const char *

发件人链接名称。

2nd const char *

收到的消息名称。

int32_t

与该消息关联的索引。

double

与消息关联的值。

Examples

机器人 1 的代码

// Create the link.
message_link link(PORT1, "LeftRightLink", linkType::manager, true);

// 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();
  
  // 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);
  }
}

机器人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);
  }
}

上一页

VEXlink

下一页

串行链路

On this page
  • 介绍
  • 类构造函数
  • 类析构函数
  • 参数
  • 例子
  • 成员功能
    • isLinked
    • send
    • receive
    • received
Innovation First, International
​

VEX 和 VEX Robotics 是 Innovation First, Inc. 的商标或服务标志 版权所有 ©2026保留所有权利。VEX Robotics, Inc. 是 Innovation First International, Inc. 的子公司。所有其他产品名称/标志均为其各自所有者的财产。专利和/或专利申请中 - innovationfirst.com/patents
网站隐私政策 / 网站使用条款 / Cookie 政策 / 软件隐私政策

访问 VEX Robotics Facebook 页面 访问 VEX Robotics Twitter 页面 访问 VEX Robotics Instagram 页面 访问 VEX Robotics YouTube 页面
VEX API 反馈表

我们重视您的反馈!请使用此表单分享建议、赞美或报告 VEX API 的错误。您的反馈有助于我们完善 VEX API 文档。

​

如果您遇到技术问题或需要客户支持,请访问 support.vex.com.

  • Send Happy Feedback
  • Send Sad Feedback

注意:当前 URL 将与您的消息共享

通过添加您的电子邮件地址,您同意如果我们对您的反馈有疑问,VEX 可以向您发送电子邮件。
隐私政策 >
请提供您的反馈。 反馈提交成功!
Choose Which VEX IQ Generation to View

VEX IQ (1st gen)

VEX IQ (2nd gen)