串行链接#
什么是串行链路?#
VEXlink 使 V5 主控模块能够与其他 V5 主控模块通信,从而实现点对点连接并在多个机器人之间创建通信网。它支持无线和有线通信,有线连接建议使用改良的智能电缆以避免电源路由问题。对于无线通信,每个机器人都需要一个连接到智能端口的 V5 机器人无线电模块,VEXlink 无线电模块可以与 V5 控制器的 VEXnet 无线电模块配合使用,后者应连接到编号最高的智能端口以避免冲突。
VEXlink 中的 SerialLink 类允许在机器人之间发送数据流,发送机器人和接收机器人都需要理解数据流的内容,该类通常用作更高层软件的一部分,根据需要对数据包进行编码和解码。
要创建 VEXlink,两个 V5 主控模块都必须连接到 V5 机器人无线电模块。
初始化 SerialLink 类#
VEXlink 是通过使用以下构造函数创建的:
The serial_link constructor creates a serial_link object.
范围 |
描述 |
|---|---|
|
A valid |
|
此链接的名称。建议此唯一字符串足够长,以便 Vexos 对其进行哈希处理后能够生成唯一的 ID。不建议使用“vexlink”之类的通用名称,因为它可能已被其他团队使用。 |
|
The type of link, either |
|
Whether or not it is a wired link. Set to |
// Construct a VEXlink "seriallink" with the serial_link class.
serial_link seriallink = serial_link(PORT1, "Link", linkType::manager);
This seriallink object will be used in all subsequent examples throughout this API documentation when referring to serial_link class methods.
类方法#
send()#
The send(buffer) method sends a buffer through the serial link.
参数 |
描述 |
|---|---|
|
字符串或字节数组。要发送的消息。 |
返回值: 表示所发送消息长度的整数。
// Send the string "test".
seriallink.send("test");
receive()#
The receive(length, timeout) method receives data from the serial link.
参数 |
描述 |
|---|---|
|
等待的最大数据量。 |
|
函数返回前的超时值,单位为毫秒。 |
返回值: 一个整数,表示接收到的数据的长度。
// Wait for 128 bytes of data for 1000mS.
int buffer = seriallink.receive(128, 1000);
已收到()#
The received(callback) method registers a callback function for when data is received.
参数 |
描述 |
|---|---|
|
接收到数据时要调用的回调函数。 |
**返回值:**无。