• 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
  • 消息链接
简体中文
  • 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

消息链接#

  • 介绍

  • 创建链接

  • 可用方法

    • 是链接的

    • 发送

    • 收到

    • 已收到

介绍#

MessageLink允许两个V5大脑通过发送带有可选少量数据的短字符串消息进行通信。它专为机器人之间的协调而设计,例如启动程序、共享传感器结果或触发另一个机器人的行为。

每次调用 send 都会向关联的 V5 Brain 的队列中添加一条消息,队列采用先进先出 (FIFO) 的读取方式。如果在另一个 V5 Brain 使用 receive 之前发送了多条消息,这些消息将被存储,并按发送顺序逐条返回。由于消息可以排队,因此在每次循环中重复发送相同的状态可能会导致积压;对于时间关键型逻辑,仅当值发生变化时才发送消息。

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

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

以下是可用方法列表:

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

  • send – 向配对的大脑发送消息。

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

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

创建链接#

在两个 V5 大脑上创建 MessageLink,以在它们之间建立通信通道——可以通过 V5 机器人无线电进行无线通信,也可以通过有线智能电缆进行通信——以便机器人可以互相发送消息。

MessageLink(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 = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)

机器人2的代码

"""
Create a wireless link in Port 1
name: VEXRoboticsLink123456789
linktype: Worker
"""
link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)

可用方法#

一旦 MessageLink 设置完毕且 Brains 连接成功,您就可以使用以下方法发送、接收和回复消息。

is_linked#

is_linked 方法返回 MessageLink 上的 V5 Brains 是否彼此配对。

  • True – 两个 V5 大脑已配对并通过此链路进行通信。

  • False ​​– 此链接中的两个 V5 大脑未配对。

注意:在项目开始时,运行任何其他代码之前,最好始终检查以确保 V5 Brains 已连接。

Usage:
link.is_linked()

参数

描述

此方法没有参数。

机器人 1 的代码

# Tell the other robot when the screen is being pressed

# Create the link
link = MessageLink(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")

# Continuously send if the screen is being pressed
while True:
    if brain.screen.pressing():
        link.send("pressed")
    else:
        link.send("released")

    wait(50, MSEC)

机器人2的代码

# Display if the other robot's screen is being pressed

# Create the link
link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)

# Do not run code UNTIL the Brains are linked
while not link.is_linked():
    wait(0.1, SECONDS)

# Continuously check if Robot 1 has sent "pressed"
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Robot 2 - Worker")
    brain.screen.next_row()

    if link.receive() == "pressed":
        brain.screen.new_line()
        brain.screen.print("Manager is being pressed!")

    wait(50, MSEC)

send#

send 向另一个连接的 V5 Brain 发送字符串和/或整数和/或浮点数。

Usage:
send(string, integer, float)

参数

描述

string

要发送给另一个已连接的 V5 大脑的字符串。

integer

可选。要发送给另一个已连接的 V5 大脑的整数。

float

可选。要发送到另一个已连接的 V5 Brain 的浮点数。浮点数默认显示 4 位小数;如果提供的位数少于 4 位,则会在末尾添加零,直到显示 4 位小数为止。

如果只发送字符串 string ,则另一个连接的 V5 大脑会收到完全相同的字符串。

如果包含 integer 和/或 float ,则接收端 Brain 将获得一个按以下顺序组合所有字段的单个编码字符串: .string_integer_float

示例: 使用 link.send(“string”, 1, 2.55) 被接收为 .string_1_2.5500。

机器人 1 的代码

# Tell the other robot when the screen is being pressed

# Create the link
link = MessageLink(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")

# Continuously send if the screen is being pressed
while True:
    if brain.screen.pressing():
        link.send("pressed")
    else:
        link.send("released")

    wait(50, MSEC)

机器人2的代码

# Display if the other robot's screen is being pressed

# Create the link
link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)

# Do not run code UNTIL the Brains are linked
while not link.is_linked():
    wait(0.1, SECONDS)

# Continuously check if Robot 1 has sent "pressed"
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Robot 2 - Worker")
    brain.screen.next_row()

    if link.receive() == "pressed":
        brain.screen.new_line()
        brain.screen.print("Manager is being pressed!")

    wait(50, MSEC)

机器人 1 的代码

# Tell the other robot where the screen was pressed

# Create the link
link = MessageLink(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")
brain.screen.next_row()
brain.screen.print("Tap to send circles")

# Send the coordinates whenever the screen is pressed
while True:
    if brain.screen.pressing():
        x = brain.screen.x_position()
        y = brain.screen.y_position()

        link.send("touch", x, y)

    wait(10, MSEC)

机器人2的代码

# Draw a circle where Robot 1's screen was pressed

# Create the link
link = MessageLink(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")

while True:
    # Get the full encoded string
    circle_coordinates = link.receive(200)

    if circle_coordinates:

        # Split the encoded string by _
        parts = circle_coordinates.split("_")

        if len(parts) == 3:
            # Convert the coordinates back into numbers
            x = float(parts[1])
            y = float(parts[2])

            brain.screen.set_fill_color(Color.WHITE)
            brain.screen.draw_circle(x, y, 20)

    wait(10, MSEC)

receive#

receive 返回来自另一个已连接的 V5 Brain 的下一条已排队的消息。消息按先进先出 (FIFO) 顺序读取(最旧的未读消息优先)。如果在调用 receive 时队列为空,则会等待最多指定的 receive 时间以接收新消息。如果在该时间内没有消息到达, receive 返回 None,之后发送的任何消息都将保留在队列中,以便在下次使用 timeout 时读取。

If the other linked V5 Brain sent only a string, receive returns that exact string.

如果另一个连接的 V5 Brain 发送的是整数和/或浮点数, receive 返回一个编码字符串,该字符串按以下顺序组合所有字段: .string_integer_float

例如,如果另一个链接的 V5 Brain 使用 link.send(“string”, 1, 2.55),那么使用 link.receive() 将返回一个编码字符串,例如 .string_1_2.5500您可以拆分此编码字符串(参见下面的示例)以提取消息名称、整数和浮点值。

如果您不想手动拆分/解析,请使用 received 为特定消息名称注册处理程序;处理程序接收发送的值作为参数。

Usage:
receive(timeout)

参数

描述

timeout

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

机器人 1 的代码

# Tell the other robot when the screen is being pressed

# Create the link
link = MessageLink(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")

# Continuously send if the screen is being pressed
while True:
    if brain.screen.pressing():
        link.send("pressed")
    else:
        link.send("released")

    wait(50, MSEC)

机器人2的代码

# Display if the other robot's screen is being pressed

# Create the link
link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)

# Do not run code UNTIL the Brains are linked
while not link.is_linked():
    wait(0.1, SECONDS)

# Continuously check if Robot 1 has sent "pressed"
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Robot 2 - Worker")
    brain.screen.next_row()

    if link.receive() == "pressed":
        brain.screen.new_line()
        brain.screen.print("Manager is being pressed!")

    wait(50, MSEC)

机器人 1 的代码

# Send a math problem for the other Brain to solve

# Create the link
link = MessageLink(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.
"""
link.send("add", 7, 2.5)

机器人2的代码

# Solve the math problem that the other Brain sends

# Create the link
link = MessageLink(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()

math_problem = link.receive()

if math_problem:

    # Split the encoded string by _
    parts = math_problem.split("_")

    if len(parts) == 3:
        # Remove the . from the beginning of the string
        operator = "".join([c for c in parts[0] if c.isalpha()])

        # Separate out the numbers for the operation
        a = int(parts[1])
        b = float(parts[2])

        # Use what operator was sent
        if operator == "add":
            result = a + b
        elif operator == "subtract":
            result = a - b
        # If neither add or subtract is sent, do nothing
        else:
            pass

brain.screen.print("Result = ", result)

received#

received 注册一个函数,当 V5 大脑收到发送的消息时,该函数将被调用。

Usage:
link.received(message, arg)

参数

描述

message

The message name (string) to match. When a received message’s name matches this value, the callback function is called.

callback

先前定义的 函数 在 V5 大脑收到与消息 message匹配的消息时运行。

received 回调被调用,并带有四个参数:

回调函数签名:
callback(message, linkname, index, value)

争论

描述

message

收到的消息名称(例如, “add”)。

linkname

收到该消息的链接名称。

index

消息中发送的整数值(如果提供)。

value

消息中发送的浮点值(如果提供)。

机器人 1 的代码

# Send a math problem for the other Brain to solve

# Create the link
link = MessageLink(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.
"""
link.send("add", 7, 2.5)

机器人2的代码

# Solve the math problem that the other Brain sends

# Create the link
link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)

while not link.is_linked():
    wait(0.1, SECONDS)

brain.screen.print("Robot 2 - Worker")
brain.screen.next_row()

# add if "add" is received
def add_cmd(message, linkname, a, b):
    result = a + b
    brain.screen.print("Result = ", result)

# subtract if ""subtract" is received
def subtract_cmd(message, linkname, a, b):
    result = a - b
    brain.screen.print("Result = ", result)

# Create event handlers for "add" and "subtract"
link.received("add", add_cmd)
link.received("subtract", subtract_cmd)

上一页

VEXlink

下一页

SerialLink

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)