• 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 推特頁面 造訪 VEX Robotics 的 Instagram 頁面 造訪 VEX Robotics YouTube 頁面
VEX API 回饋表

我們重視您的回饋!請使用此表單分享您對 VEX API 的建議、讚揚或報告錯誤。您的意見有助於我們改進 VEX API 文件。

​

如果您遇到技術問題或需要客戶支持,請造訪 support.vex.com。

  • Send Happy Feedback
  • Send Sad Feedback

注意:目前網址將隨您的訊息一起發送。

提供您的電子郵件地址即表示您同意,如果我們對您的回饋有任何疑問,VEX 可以向您發送電子郵件。
隱私權政策 >
請提供您的回饋意見。 反饋已成功提交!
Choose Which VEX IQ Generation to View

VEX IQ (1st gen)

VEX IQ (2nd gen)