• VEXcode Robotics Logo VEX Robotics Logo
  • VEX API Home Button VEX API Home Button
  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white
跳至主要内容
Ctrl+K

< Back to Platform Select

  • VEX V5
  • VEX V5 API 参考
  • VEXlink
  • SerialLink
简体中文
  • English
  • العربية
  • বাংলা
  • Česky
  • Deutsch
  • Ελληνικά
  • Español
  • Español Latinoamericano
  • Suomi
  • Français
  • Gaeilge
  • हिंदी
  • Magyar
  • Bahasa Indonesia
  • 日本語
  • 한국어
  • Lietuvių
  • Latviešu
  • മലയാളം
  • Nederlands
  • Polski
  • Português
  • Русский
  • Svenska
  • ไทย
  • Türkçe
  • Українська
  • Tiếng Việt
  • 繁體中文
  • VEXcode Robotics Logo

版块导航

  • 区块
  • Python
    • 传动系统
    • 运动
    • 想象
    • 屏幕
    • 控制器
    • 传感
    • 惯性
    • 三线制设备
    • 气动元件
    • 脑
    • 声音
    • SD卡
    • VEXlink
      • 消息链接
      • SerialLink
    • 安慰
    • 逻辑
    • 六轴机械臂
    • 磁铁
    • 竞赛
    • MicroPython 库
  • C++
  • VEXcode教程

平台导航

  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white

SerialLink#

  • 介绍

  • 创建链接

  • 可用方法

    • 是链接的

    • 发送

    • 收到

    • 已收到

介绍#

SerialLink 允许两个 V5 主控模块通过 VEX Link 发送和接收原始串行数据进行通信。SerialLink 不发送带标签的消息,而是传输一个缓冲区(字符串或字节数组)。这使其适用于高级机器人间通信,例如流式传输传感器数据、发送自定义数据包格式,或基于字节构建自定义协议。

每次使用 send 都会将数据添加到链接的 V5 Brain 的接收缓冲区。数据按到达顺序(先进先出)读取。如果在另一个 V5 Brain 使用 receive 之前发送了多个缓冲区,这些缓冲区将保持排队状态,并按发送顺序逐个返回。由于数据可以排队,因此在每次循环中重复发送大型缓冲区可能会导致积压;对于时间要求严格的链路,请仅发送所需数据。

重要提示:两个机器人必须同时运行使用 SerialLink 项目,否则将不会发送/接收任何数据。

本页面使用 link 作为示例 SerialLink 名称。请根据需要将其替换为您自己配置的名称。

以下是可用方法列表:

  • is_linked – 返回此大脑是否与其配对的大脑处于活动连接状态。

  • send – 向配对的大脑发送一条短消息(可选整数/浮点数数据)。

  • receive – 等待并返回下一个传入的消息。

  • received – 注册一个函数,每当收到新消息时调用该函数。

创建链接#

在两个 V5 大脑上创建 SerialLink,以在它们之间建立通信通道——可以通过 V5 机器人无线电进行无线连接,也可以通过有线智能电缆进行连接——以便机器人可以互相发送原始串行数据。

SerialLink(smartport, name, linktype, wired)

范围

描述

smartport

此连接使用的智能端口——V5 机器人无线电连接的端口(无线)或智能电缆插入的端口(有线)。格式为 Ports.PORTx ,其中 x 为端口号。

name

此链接的唯一名称(字符串形式)
注意: 此名称应足够长,以生成唯一的 ID,避免与其他团队的链接意外匹配。

linktype

The role this Brain will use for this link pair. Each pair must include one Manager and one Worker:

  • VexlinkType.MANAGER – Use for the robot that will send more or larger messages. Managers have higher outbound bandwidth (~1040 bytes/sec).
  • VexlinkType.WORKER – Use for the robot that will send fewer or smaller messages. Workers have lower outbound bandwidth (~520 bytes/sec).

wired

Optional. Whether this link is wired or wireless:

  • True – The V5 Brains are connected via a Smart Cable.
  • False (default) – The V5 Brains connect wirelessly using V5 RObot Radios.

机器人 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)

参数

描述

buffer

要发送到另一个已连接的 V5 大脑的数据。这可以是 string 或 bytearray。

机器人 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)

参数

描述

length

要接收的字节数 receive 会等待直到收到指定数量的字节,然后将其作为 receive返回。如果 receive 为 0, bytearray 会立即返回 None 如果您请求的字节数超过发送方发送的字节数, length 将继续等待。如果发送方发送的字节数超过此值,则返回的缓冲区长度限制为 length。

timeout

可选。等待新消息的毫秒数 receive 表示队列为空)。默认值为 300000(300 秒)。

机器人 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)

参数

描述

callback

先前定义的 函数,当 V5 大脑接收到串行数据时执行。

received 回调函数,每个回调函数都带有两个参数:

回调函数签名:
callback(buffer, length)

争论

描述

buffer

一个包含接收到的串行数据的 bytearray 该字节数组可能比接收到的消息更大。

length

此回调函数在缓冲区 buffer 有效字节数。使用 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)

上一页

消息链接

下一页

安慰

On this page
  • 介绍
  • 创建链接
  • 可用方法
    • is_linked
    • send
    • receive
    • received
Innovation First, International
​

VEX 和 VEX Robotics 是 Innovation First, Inc. 的商标或服务标志 版权所有 ©2026保留所有权利。VEX Robotics, Inc. 是 Innovation First International, Inc. 的子公司。所有其他产品名称/标志均为其各自所有者的财产。专利和/或专利申请中 - innovationfirst.com/patents
网站隐私政策 / 网站使用条款 / Cookie 政策 / 软件隐私政策

访问 VEX Robotics Facebook 页面 访问 VEX Robotics Twitter 页面 访问 VEX Robotics Instagram 页面 访问 VEX Robotics YouTube 页面
VEX API 反馈表

我们重视您的反馈!请使用此表单分享建议、赞美或报告 VEX API 的错误。您的反馈有助于我们完善 VEX API 文档。

​

如果您遇到技术问题或需要客户支持,请访问 support.vex.com.

  • Send Happy Feedback
  • Send Sad Feedback

注意:当前 URL 将与您的消息共享

通过添加您的电子邮件地址,您同意如果我们对您的反馈有疑问,VEX 可以向您发送电子邮件。
隐私政策 >
请提供您的反馈。 反馈提交成功!
Choose Which VEX IQ Generation to View

VEX IQ (1st gen)

VEX IQ (2nd gen)