SerialLink#
介紹#
SerialLink 讓兩個 EXP Brain 透過 VEX Link 發送和接收原始串列資料進行通訊。 SerialLink 不傳送帶有標籤的訊息,而是傳送一個緩衝區(字串或位元組陣列)。這使其適用於高級機器人間通信,例如串流感測器資料、發送自訂資料包格式,或基於位元組建立自訂協定。
每次使用 send 都會將資料加入連結的 EXP Brain 的接收緩衝區。資料依到達順序(先進先出)讀取。如果在另一個 EXP Brain 使用 receive 之前發送了多個緩衝區,它們將保持排隊狀態,並按發送順序逐個返回。由於資料可以排隊,因此在每次循環中重複發送大型緩衝區可能會導致積壓;對於時間要求嚴格的鏈接,請僅發送所需資料。
重要提示:兩個機器人必須同時運行使用 SerialLink 項目,否則將不會發送/接收任何資料。
本頁面使用 link 作為範例 SerialLink 名稱。請根據需要將其替換為您自己配置的名稱。
以下是可用方法清單:
建立連結#
在兩個 EXP 大腦上創建 SerialLink,以在它們之間建立通訊通道——可以透過 V5 機器人無線電進行無線連接,也可以透過有線智慧電纜進行連接——以便機器人可以互相發送原始串行數據。
SerialLink(smartport, name, linktype, wired)
範圍 |
描述 |
|---|---|
|
此連接使用的智慧型連接埠-V5 機器人無線電連接的連接埠(無線)或智慧電纜插入的連接埠(有線)。格式為 |
|
此連結的唯一名稱(字串形式) |
|
The role this Brain will use for this link pair. Each pair must include one Manager and one Worker:
|
|
Optional. Whether this link is wired or wireless:
|
機器人 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)
參數 |
描述 |
|---|---|
|
要傳送給另一個已連接的 EXP Brain 的資料。這可以是 |
機器人 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)
參數 |
描述 |
|---|---|
|
要接收的位元組數 |
|
可選。等待新訊息的毫秒數 |
機器人 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)
參數 |
描述 |
|---|---|
|
先前定義的 函數,當 EXP Brain 接收到串列資料時執行。 |
received 回呼函數,每個回呼函數都帶有兩個參數:
回呼函數簽章:
callback(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)