消息链接#
什么是消息链接?#
VEXlink 使一个 EXP Brain 能够与另一个 EXP Brain 通信,从而实现点对点连接并在多个机器人之间创建通信网格。它支持无线和有线通信,有线连接建议使用改进的智能电缆,以防止电源布线问题。对于无线通信,每个机器人都需要一个连接到智能端口的 V5 机器人无线电,VEXlink 无线电可以与 V5 控制器的 VEXnet 无线电一起使用,后者应连接到编号最高的智能端口,以避免冲突。
VEXlink 中的 MessageLink 类有助于向连接的机器人发送简单消息,例如“播放”和“停止”,并且只需最少的数据。
初始化 MessageLink 类#
使用以下构造函数创建 VEXlink:
MessageLink(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 "link" with the MessageLink class.
link = MessageLink(Ports.PORT1, "Link", VexlinkType.MANAGER)
This link
object will be used in all subsequent examples throughout this API documentation when referring to MessageLink class methods.
类方法#
is_linked()#
The is_linked()
method returns the VEXlink’s current status.
Returns: True
if the VEXlink is active and connected to the paired Brain. False
if it is not.
send()#
The send(message, index, value)
method sends a message through the VEXlink.
参数 |
描述 |
---|---|
信息 |
要发送的消息。 |
指数 |
**可选。**一个整数,例如端口号。 |
价值 |
可选。 一个浮点数。 |
Returns: The length of transmitted data or None
if there is an error.
# Send the message 'test' with no parameters.
link.send('test')
# Send the message 'test' with parameters.
link.send('test', 1, 3.14)
receive()#
The receive(timeout)
method receives a message from the VEXlink.
参数 |
描述 |
---|---|
暂停 |
**可选。**函数返回之前接收消息的超时时间(以毫秒为单位)。 |
Returns: The received message or None
if there is an error.
received()#
The received(callback, arg)
method 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.
参数 |
描述 |
---|---|
打回来 |
收到消息时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**无。
# Define a function message_received()
def message_received():
# The Brain will print that the message was received by
# the Message Link on the Brain's screen.
Brain.screen.print("message received")
# Run message_received() when a message is received.
link.received(message_received)
installed()#
The installed()
method checks if the VEXlink is connected.
Returns: True
if the VEXlink is connected. False
if it is not.