消息链接#
什么是消息链接?#
VEXlink 使 V5 主控模块能够与其他 V5 主控模块通信,从而实现点对点连接并在多个机器人之间创建通信网。它支持无线和有线通信,有线连接建议使用改良的智能电缆以避免电源路由问题。对于无线通信,每个机器人都需要一个连接到智能端口的 V5 机器人无线电模块,VEXlink 无线电模块可以与 V5 控制器的 VEXnet 无线电模块配合使用,后者应连接到编号最高的智能端口以避免冲突。
VEXlink 中的 message_link 类便于向连接的机器人发送简单的消息,例如“播放”和“停止”,只需发送最少的数据。
初始化 message_link 类#
VEXlink 是通过使用以下构造函数创建的:
The message_link constructor creates a message_link object in the specified Port, with the specified ID and linktype, and sets the wired flag to the specified value.
范围 |
描述 |
|---|---|
|
A valid |
|
此链接的名称。建议此唯一字符串足够长,以便 Vexos 对其进行哈希处理后能够生成唯一的 ID。不建议使用“vexlink”之类的通用名称,因为它可能已被其他团队使用。 |
|
The type of link, either |
|
Whether or not it is a wired link. Set to |
// Construct a VEXlink "link" with the message_link class.
message_link link = message_link(PORT1, "link", linkType::manager);
This link object will be used in all subsequent examples throughout this API documentation when referring to message_link class methods.
类方法#
send()#
该方法可通过以下方式调用:
The send(message) method sends a message through the Message Link.
参数 |
描述 |
|---|---|
|
要发送的信息。 |
返回值: 表示传输数据长度的整数。
// Send the message "test" with no parameters.
link.send("test");
The send(message, value) method sends a message through the Message Link.
参数 |
描述 |
|---|---|
|
要发送的信息。 |
|
表示消息 ID 的浮点数。 |
返回值: 表示传输数据长度的整数。
The send(message, index, value) method sends a message through the Message Link.
参数 |
描述 |
|---|---|
|
要发送的信息。 |
|
表示接收消息链路所连接的端口的整数。 |
|
表示消息 ID 的浮点数。 |
返回值: 表示传输数据长度的整数。
receive()#
The receive(timeout) command receives a message from the Message Link.
参数 |
描述 |
|---|---|
|
函数返回前接收消息的超时时间(以毫秒为单位)。 |
返回值: 收到的消息。
received()#
The received(callback) command registers a callback function for when a message is received. If the message is omitted then the callback function will be called for all messages.
这是一个非等待命令,允许下一个命令立即运行。
参数 |
描述 |
|---|---|
|
收到消息时要调用的回调函数。 |
**返回值:**事件类的一个实例。