SerialLink#
介绍#
SerialLink 允许两个 V5 主控模块通过 VEX Link 发送和接收原始串行数据进行通信。SerialLink 不发送带标签的消息,而是传输一个缓冲区(字符串或字节数组)。这使其适用于高级机器人间通信,例如流式传输传感器数据、发送自定义数据包格式,或基于字节构建自定义协议。
每次使用 send 都会将数据添加到链接的 V5 Brain 的接收缓冲区。数据按到达顺序(先进先出)读取。如果在另一个 V5 Brain 使用 receive 之前发送了多个缓冲区,这些缓冲区将保持排队状态,并按发送顺序逐个返回。由于数据可以排队,因此在每次循环中重复发送大型缓冲区可能会导致积压;对于时间要求严格的链路,请仅发送所需数据。
重要提示:两个机器人必须同时运行使用 SerialLink 项目,否则将不会发送/接收任何数据。
本页面使用 link 作为示例 SerialLink 名称。请根据需要将其替换为您自己配置的名称。
以下是可用方法列表:
创建链接#
在两个 V5 大脑上创建 SerialLink,以在它们之间建立通信通道——可以通过 V5 机器人无线电进行无线连接,也可以通过有线智能电缆进行连接——以便机器人可以互相发送原始串行数据。
SerialLink(smartport, name, linktype, wired)
范围 |
描述 |
|---|---|
|
此连接使用的智能端口——V5 机器人无线电连接的端口(无线)或智能电缆插入的端口(有线)。格式为 |
|
此链接的唯一名称(字符串形式) |
|
The role this Brain will use for this link pair. Each pair must include one Manager and one Worker:
|
|
Optional. Whether this link is wired or wireless:
|
机器人 1 的代码
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)机器人2的代码
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
可用方法#
一旦您设置好 SerialLink 并且 Brains 连接完毕,您就可以使用以下方法发送、接收和响应原始串行数据。
is_linked#
is_linked 方法返回 SerialLink 上的 V5 Brains 是否彼此配对。
True– 两个 V5 大脑已配对并通过此链路进行通信。False– 此链接中的两个 V5 大脑未配对。
注意:在项目开始时,运行任何其他代码之前,最好始终检查以确保 V5 Brains 已连接。
Usage:
link.is_linked()
参数 |
描述 |
|---|---|
此方法没有参数。 |
机器人 1 的代码
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")机器人2的代码
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)
send#
send 将原始串行字节发送到另一个连接的 V5 Brain。
当使用 receive 读取数据时, receive 只有在收集到其 length 参数指定的字节数后才会返回。因此,如果你的代码总是使用固定的 length调用 receive ,你应该确保使用 send 发送的每条消息都恰好包含该字节数(通常通过填充),这样接收方就不会因为等待额外的字节而阻塞。
如果你不想依赖精确的字节数,请改用 received;它允许你处理任何可用的字节,而无需特定长度。
Usage:
send(buffer)
参数 |
描述 |
|---|---|
|
要发送到另一个已连接的 V5 大脑的数据。这可以是 |
机器人 1 的代码
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")机器人2的代码
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)
receive#
receive 从另一个连接的 V5 Brain 按 FIFO 顺序(最早的未读数据在前)返回串行数据。
由于 receive 会等待精确的字节数,因此在使用 receive 时,固定长度消息通常需要填充,以确保每条消息达到预期大小。如果发送方发送的字节数少于 send ,则 length 将继续等待剩余字节,而不会返回任何内容。
如果队列为空,且您提供的超时时间为 timeout receive 并等待最多指定毫秒数以内的新数据。如果在该时间内没有新数据到达,则返回 None之后发送的任何数据仍保留在队列中,以便稍后读取。
Usage:
receive(length, timeout)
参数 |
描述 |
|---|---|
|
要接收的字节数 |
|
可选。等待新消息的毫秒数 |
机器人 1 的代码
# Send a math problem for the other Brain to solve # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ packet = "add,7,2.5" # Pad with spaces until it's exactly 16 bytes while len(packet) < 16: packet = packet + " " link.send(packet)机器人2的代码
# Solve the math problem that the other Brain sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() # Receive exactly 16 bytes buffer = link.receive(16) # Remove the added " " padding text = buffer.decode().strip() # Separate the operator and numbers parts = text.split(",") operator = parts[0] a = float(parts[1]) b = float(parts[2]) # Use what operator was sent if operator == "add": result = a + b elif operator == "subtract": result = a - b # If not "add" or "subtract", do nothing else: pass brain.screen.print("Result = ", result)
received#
received 注册一个函数,当 V5 大脑收到串行数据时要调用该函数。
Usage:
link.received(callback)
参数 |
描述 |
|---|---|
|
先前定义的 函数,当 V5 大脑接收到串行数据时执行。 |
received 回调函数,每个回调函数都带有两个参数:
回调函数签名:
callback(buffer, length)
争论 |
描述 |
|---|---|
|
一个包含接收到的串行数据的 |
|
此回调函数在缓冲区 |
机器人 1 的代码
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")机器人2的代码
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)