• 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
  • Spanish
  • VEXcode Robotics Logo

版块导航

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

平台导航

  • 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 不发送带标签的消息,而是传输一个缓冲区(字符串或字节数组)。这使其适用于高级机器人间通信,例如流式传输传感器数据、发送自定义数据包格式,或基于字节构建自定义协议。

Every usage of send adds data to the linked V5 Brain’s receive buffer. Data is read in the order it arrives (first-in, first-out). If multiple buffers are sent before the other V5 Brain uses receive, they remain queued and are returned one at a time in the order they were sent. Because data can queue, repeatedly sending large buffers every loop may create backlog; for time-critical links, send only what you need.

Important: Both robots must be running projects that use SerialLink at the same time, or no data will be sent/received.

This page uses link as the example SerialLink name. Replace it with your own configured name as needed.

以下是可用方法列表:

  • is_linked – Returns whether this Brain is actively connected to its paired Brain.

  • send – Sends a short message (with optional int/float data) to the paired Brain.

  • receive – Waits for and returns the next incoming message.

  • received – Registers a function to be called whenever a new message is received.

创建链接#

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

SerialLink(smartport, name, linktype, wired)

范围

描述

smartport

The Smart Port used for this link—the port the V5 Robot Radio is connected to (wireless) or the Smart Cable is plugged into (wired). Written as Ports.PORTx where x is the port number.

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 method returns whether the V5 Brains on a SerialLink are paired with one another.

  • True – The two V5 Brains are paired and communicating on this link.

  • False – The two V5 Brains are not paired on this link.

**注意:**在项目开始时,运行任何其他代码之前,最好始终检查以确保 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 transmits raw serial bytes to the other linked V5 Brain.

When reading that data with receive, receive will only return once it has collected the number of bytes specified by its length parameter. Because of that, if your code always calls receive with a fixed length, you should ensure each message sent with send is exactly that many bytes (often by padding) so the receiver doesn’t block waiting for additional bytes.

If you don’t want to depend on an exact byte count, use received instead; it lets you handle whatever bytes are available without requiring a specific length.

Usage:
send(buffer)

参数

描述

buffer

The data to send to the other linked V5 Brain. This can be a string or a 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 returns serial data from the other linked V5 Brain in FIFO order (oldest unread data first).

Because receive waits for an exact byte count, fixed-length messaging often requires padding when using send so each message reaches the expected size. If the sender transmits fewer than length bytes, receive will continue waiting for the remaining bytes and not return anything.

If the queue is empty and you provide a timeout, receive waits up to that many milliseconds for new data. If nothing arrives in that window, it returns None. Any data sent afterward remains queued to be read later.

Usage:
receive(length, timeout)

参数

描述

length

The number of bytes to receive. receive waits until this many bytes have arrived, then returns them as a bytearray. If length is 0, receive returns None immediately. If you request more bytes than the sender transmits, receive will keep waiting. If the sender transmits more than this, the returned buffer is limited to length bytes.

timeout

Optional. How long in milliseconds receive will wait for a new message only if the queue is empty. Default is 300000 (300 seconds).

机器人 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 registers a function to be called whenever the V5 Brain receives serial data.

Usage:
link.received(callback)

参数

描述

callback

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

received callbacks are called with two arguments:

Callback Signature:
callback(buffer, length)

争论

描述

buffer

A bytearray containing the received serial data. The bytearray may be larger than the received message.

length

The number of valid bytes received in buffer for this callback. Use buffer[:length] to access the received data.

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