• 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 EXP
  • VEX EXP 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 讓兩個 EXP Brain 透過 VEX Link 發送和接收原始串列資料進行通訊。 SerialLink 不傳送帶有標籤的訊息,而是傳送一個緩衝區(字串或位元組陣列)。這使其適用於高級機器人間通信,例如串流感測器資料、發送自訂資料包格式,或基於位元組建立自訂協定。

每次使用 send 都會將資料加入連結的 EXP Brain 的接收緩衝區。資料依到達順序(先進先出)讀取。如果在另一個 EXP Brain 使用 receive 之前發送了多個緩衝區,它們將保持排隊狀態,並按發送順序逐個返回。由於資料可以排隊,因此在每次循環中重複發送大型緩衝區可能會導致積壓;對於時間要求嚴格的鏈接,請僅發送所需資料。

重要提示:兩個機器人必須同時運行使用 SerialLink 項目,否則將不會發送/接收任何資料。

本頁面使用 link 作為範例 SerialLink 名稱。請根據需要將其替換為您自己配置的名稱。

以下是可用方法清單:

  • is_linked – 傳回此大腦是否與其配對的大腦處於活動連結狀態。

  • send – 向配對的大腦發送一條短訊息(可選整數/浮點數資料)。

  • receive – 等待並傳回下一個傳入的訊息。

  • received – 註冊一個函數,每當收到新訊息時呼叫函數。

建立連結#

在兩個 EXP 大腦上創建 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 EXP Brains are connected via a Smart Cable.
  • False (default) – The EXP 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 上的 EXP Brains 是否彼此配對。

  • True – 兩個 EXP 大腦已配對並透過此連結進行通訊。

  • False 此連結中的兩個 EXP 大腦並未配對。

注意:在專案開始時,運行任何其他程式碼之前,最好始終檢查以確保 EXP 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 將原始串列位元組傳送到另一個連接的 EXP Brain。

當使用 receive 讀取資料時, receive 只有在收集到其 length 參數指定的位元組數後才會回傳。因此,如果你的程式碼總是使用固定的 length調用 receive ,你應該確保使用 send 發送的每條訊息都恰好包含該位元組數(通常透過填充),這樣接收方就不會因為等待額外的位元組而阻塞。

如果你不想依賴精確的位元組數,請改用 received;它允許你處理任何可用的位元組,而無需特定長度。

Usage:
send(buffer)

參數

描述

buffer

要傳送給另一個已連接的 EXP Brain 的資料。這可以是 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 以 FIFO 順序(最早的未讀資料優先)從另一個連接的 EXP Brain 傳回串列資料。

由於 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 註冊一個函數,每當 EXP Brain 收到串列資料時都要呼叫函數。

Usage:
link.received(callback)

參數

描述

callback

先前定義的 函數,當 EXP Brain 接收到串列資料時執行。

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 推特頁面 造訪 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)