串行链路#
什么是串行链路?#
VEXlink 使一个 EXP Brain 能够与另一个 EXP Brain 通信,从而实现点对点连接并在多个机器人之间创建通信网格。它支持无线和有线通信,有线连接建议使用改进的智能电缆,以防止电源布线问题。对于无线通信,每个机器人都需要一个连接到智能端口的 V5 机器人无线电,VEXlink 无线电可以与 V5 控制器的 VEXnet 无线电一起使用,后者应连接到编号最高的智能端口,以避免冲突。
VEXlink 中的 SerialLink 类允许在机器人之间发送数据流,传输和接收机器人都需要理解流的内容,此类通常用作更高层软件的一部分,根据需要对数据包进行编码和解码。
要创建 VEXlink,两个 V5 Brains 都必须连接到 V5 机器人无线电。
初始化 SerialLink 类#
使用以下构造函数创建 VEXlink:
SerialLink(port, name, linktype, wired)
此构造函数使用四个参数:
范围 |
描述 |
---|---|
|
VEXlink 无线电连接到的有效 智能端口。 |
|
此链接的名称。建议此唯一字符串足够长,以便在 vexos 进行哈希处理时能够创建唯一的 ID。不合适的链接名称可能是通用名称,例如“vexlink”,因为它可能会被其他团队使用。 |
|
The type of link, either |
|
Optional. Whether or not it is a wired link. Set to |
# Construct a VEXlink "seriallink" with the SerialLink class.
seriallink = SerialLink(Ports.PORT1, "Link", VexlinkType.MANAGER)
This seriallink
object will be used in all subsequent examples throughout this API documentation when referring to SerialLink class methods.
类方法#
is_linked()#
The is_linked()
method returns the link status of the Serial Link.
Returns: True
if the Serial Link is active and connected to the paired Brain. False
if it is not.
send()#
The send()
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.
参数 |
描述 |
---|---|
长度 |
等待的最大数据量。 |
暂停 |
**可选。**函数返回前的超时值(以毫秒为单位)。 |
**返回:**包含已接收数据的字节数组,如果没有接收到数据则返回 None。
# Wait for 128 bytes of data for 1000mS.
buffer = seriallink.receive(128, 1000)
received()#
The received(callback)
method registers a callback function for when data is received.
参数 |
描述 |
---|---|
打回来 |
接收到数据时调用的回调函数。 |
**返回:**无。
# Define a function data_received()
def data_received():
# The Brain will print that data was received by the
# Serial Link on the Brain's screen.
Brain.screen.print("data received by Serial Link")
# Run data_received when data is received by the Serial Link.
seriallink.received(data_received)
installed()#
The installed()
method checks if the Serial Link is connected.
Returns: True
if the Serial Link is connected. False
if it is not.