• 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
  • 序列鏈路
繁體中文
  • 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
  • C++
    • 傳動系統
    • 馬達和馬達控制器
    • 三線制設備
    • 腦
    • 安慰
    • 控制器
    • 邏輯
    • 智慧型連接埠設備
    • VEXlink
      • 訊息連結
      • 序列鏈路
    • CTE 工作單元
  • 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

序列鏈路#

  • 介紹

  • 類別建構函數

  • 類別析構函數

  • 參數

  • 筆記

  • 例子

  • 成員功能

    • isLinked

    • 傳送

    • 收到

    • 已收到

介紹#

serial_link 類別允許在機器人之間發送原始位元組流。它專為機器人之間的通訊而設計,其中發送機器人和接收機器人都能理解資料流的內容。

每次使用 send 都會將資料加入到關聯的 EXP Brain 的佇列中,佇列採用先進先出 (FIFO) 的讀取方式。如果在另一個 EXP Brain 使用接收操作之前發送了多個訊息,這些訊息將被存儲,並按發送順序逐條返回。由於訊息可以排隊,因此在每次循環中重複發送相同的狀態可能會導致積壓;對於時間關鍵型邏輯,僅當值發生變化時才發送訊息。

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

類別建構函數#

serial_link(
 int32_t index,
 const char *name,
 linkType type,
 bool isWired = false );

類別析構函數#

銷毀 serial_link 物件並釋放相關資源。

~serial_link();

參數#

範圍

類型

描述

index

int32_t

訊息鏈路連接的智慧型端口,寫成 PORTx,其中 x 是端口號(例如, PORT1)。

name

const char*

此連結的名稱。建議此唯一字串足夠長,以便 Vexos 對其進行哈希處理後能夠產生唯一的 ID。不建議使用“vexlink”之類的通用名稱,因為它可能被其他團隊使用。

type

linkType

The type of link, either linkType::manager or linkType::worker. This information is used to correctly configure the radio and also determines available bandwidth for transmission and reception. A manager robot has double the available bandwidth (1040 bytes/second) to send information to the worker robot (520 bytes/second).

  • linkType::manager — The serial_link object is a manager
  • linkType::worker — The serial_link object is a worker

isWired

bool

serial_link 物件是否為有線連接(預設為 false):

  • true — serial_link 物件為有線連接
  • false — serial_link 物件為無線連線

筆記#

  • VEXlink 支援無線和有線通信,有線連接建議使用改良的智慧電纜,以防止電源路由問題。

  • 對於無線通信,每個機器人都需要一個連接到智慧型連接埠的 V5 機器人無線電模組。

  • VEXlink 無線電可以與 EXP 控制器的 VEXnet 無線電一起使用,應連接到編號最高的智慧型連接埠以避免衝突。

  • 要建立 VEXlink,兩個 EXP 大腦都必須連接到 V5 機器人無線電。

例子#

機器人 1 的程式碼

// Create a wireless link in Port 1
serial_link link = serial_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::manager, // type
  true); // isWired

機器人2的程式碼

// Create a wireless link in Port 1
serial_link link = serial_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::worker, // type
  true); // isWired

成員功能#

serial_link 類別包含以下成員函數:

  • isLinked — 檢查 EXP Brain 是否已配對並透過此連結進行通訊。

  • send — 向配對的大腦發送訊息。

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

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

在呼叫任何 serial_link 函數之前,必須先建立一個 serial_link ,如下所示:

機器人 1 的程式碼

// Create a wireless link in Port 1
serial_link link = serial_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::manager, // type
  true); // isWired

機器人2的程式碼

// Create a wireless link in Port 1
serial_link link = serial_link(
  PORT1, // index
  "VEXRoboticsLink123456789", // name
  linkType::worker, // type
  true); // isWired

isLinked#

返回串行鏈路上的 EXP Brain 是否彼此配對。

Available Functions
bool isLinked();

Parameters

此函數沒有任何參數。

Return Values

傳回布林值,指示 EXP Brains 是否已連線:

  • true — The two EXP Brains are paired and communicating on this link.
  • false — The two EXP Brains are not paired on this link.
Notes
  • 在專案開始時,運行任何其他程式碼之前,最好始終檢查 EXP Brains 是否已連接。

Examples

機器人 1 的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true);

  // Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.print("Robot 1 - Manager");
  Brain.Screen.newLine();
  Brain.Screen.print("Sending message...");

  // Message to send
  const char* message = "Hello Robot 2";

  // Send the message
  link.send((uint8_t*)message, strlen(message));

機器人2的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true);

  // Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }
  
  Brain.Screen.print("Robot 2 - Worker");
  Brain.Screen.newLine();
  Brain.Screen.print("Waiting...");

  // Buffer to store incoming bytes
  uint8_t buffer[32] = {0};

  int32_t n = link.receive(buffer, sizeof(buffer) - 1);

  // Null-terminate
  if (n > 0) {
    buffer[n] = 0;

    // Display the message that was sent
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("Robot 2 - Worker");
    Brain.Screen.newLine();
    Brain.Screen.print((char*)buffer);
  }
  else {
    Brain.Screen.newLine();
    Brain.Screen.print("No message received");
  }

send#

向配對的大腦發送訊息。

Available Functions

1 — 使用位元組緩衝區傳送訊息。

int32_t send( 
  uint8_t *buffer, 
  int32_t  length );

2 — 使用字元緩衝區傳送訊息。

int32_t send( 
  const char *buffer, 
  int32_t     length );

Parameters

範圍

類型

描述

buffer

uint8_t* or const char*

要傳送到另一個已連接的 EXP Brain 的緩衝區。

length

int32_t

要傳送的緩衝區長度。

Return Values

傳回 int32_t ,表示發送的位元組數。

Examples

機器人 1 的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true);

  // Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Robot 1 - Manager");

  // Track last sent state
  uint8_t lastState = 255;  // impossible initial value

 // Tell Robot 2 when the screen is being pressed
  while (true) {
    uint8_t state = Brain.Screen.pressing() ? 1 : 0;

    // Only send if state changed
    if (state != lastState) {
      link.send(&state, 1);
      lastState = state;
    }

    wait(0.02, seconds);
  }

機器人2的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true);

  // Do not run code UNTIL the Brains are linked
   while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Robot 2 - Worker");
  Brain.Screen.newLine();
  Brain.Screen.print("Waiting...");

  // Track the last state
  uint8_t lastShown = 255;

  while (true) {
    // Variable stores 1 - pressed, 0 - not pressed 
    uint8_t state = 0;
    int32_t n = link.receive(&state, 1, 200);

    // Only update screen if state changes
    if (n == 1 && state != lastShown) {
      Brain.Screen.clearScreen();
      Brain.Screen.setCursor(1, 1);
      Brain.Screen.print("Robot 2 - Worker");
      Brain.Screen.newLine();

      // Display text if Robot 1's screen is being pressed
      if (state == 1) {
        Brain.Screen.print("Manager IS being pressed!");
      } else {
        Brain.Screen.print("Manager is NOT being pressed");
      }

      lastShown = state;
    }
    wait(0.02, seconds);
  }

receive#

等待並返回下一條傳入的訊息。

Available Functions

1 — 將訊息接收到字元緩衝區中,並可選擇設定逾時時間。

int32_t receive( 
  char    *buffer, 
  int32_t  length, 
  int32_t  timeoutMs = 500);

Parameters

範圍

類型

描述

buffer

char*

用於儲存接收到的訊息的緩衝區。

length

int32_t

緩衝區長度。

timeoutMs

int32_t

receive 等待時間(毫秒)僅在佇列為空時才會等待新訊息(預設為 500 毫秒)。

Return Values

傳回 int32_t ,表示接收到的位元組數。

Notes
  • receive 返回來自另一個已連接的 EXP Brain 的下一個已排隊訊息。

  • 訊息以先進先出(FIFO)順序讀取(最舊的未讀訊息優先)。

  • 如果在呼叫 receive 時佇列為空,則最多等待指定的 timeoutMs 以接收新訊息。

  • 如果在該視窗中沒有訊息到達, receive 返回 0,並且之後發送的任何訊息都將保留在佇列中,以便在下次使用 receive 時讀取。

Examples

機器人 1 的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true);

  // Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Robot 1 - Manager");

  // Track last sent state
  uint8_t lastState = 255;  // impossible initial value

 // Tell Robot 2 when the screen is being pressed
  while (true) {
    uint8_t state = Brain.Screen.pressing() ? 1 : 0;

    // Only send if state changed
    if (state != lastState) {
      link.send(&state, 1);
      lastState = state;
    }

    wait(0.02, seconds);
  }

機器人2的程式碼

// Create the link
  serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true);

  // Do not run code UNTIL the Brains are linked
   while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Robot 2 - Worker");
  Brain.Screen.newLine();
  Brain.Screen.print("Waiting...");

  // Track the last state
  uint8_t lastShown = 255;

  while (true) {
    // Variable stores 1 - pressed, 0 - not pressed 
    uint8_t state = 0;
    int32_t n = link.receive(&state, 1, 200);

    // Only update screen if state changes
    if (n == 1 && state != lastShown) {
      Brain.Screen.clearScreen();
      Brain.Screen.setCursor(1, 1);
      Brain.Screen.print("Robot 2 - Worker");
      Brain.Screen.newLine();

      // Display text if Robot 1's screen is being pressed
      if (state == 1) {
        Brain.Screen.print("Manager IS being pressed!");
      } else {
        Brain.Screen.print("Manager is NOT being pressed");
      }

      lastShown = state;
    }
    wait(0.02, seconds);
  }

received#

註冊一個函數,當 EXP Brain 收到發送的訊息時,該函數將被呼叫。

Available Functions

1 — 為收到的每個訊息註冊一個回呼函數。

void received( 
  void (* callback)(const char *, const char *, double) );

2 — 註冊一個回呼函數,該函數會針對每個接收到的訊息調用,其中包含一個額外的 int32_t 欄位。

void received( 
  void (* callback)(const char *, const char *, int32_t, double) );

3 — 註冊一個回呼函數,該回呼函數僅在接收到名稱與 message 相符的訊息時才會呼叫。

void received( 
  const char *message, void (* callback)(const char *, const char *, double) );

4 — 註冊一個回呼函數,該回呼函數僅在接收到名稱與 message 匹配的訊息時調用,其中訊息有效負載包含一個額外的 int32_t 欄位。

void received( 
  const char *message, void (* callback)(const char *, const char *, int32_t, double) );

Parameters

範圍

類型

描述

message

const char*

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

callback

function

當 EXP Brain 收到與 message相符的訊息時,會執行一個先前定義的函數。

Return Values

此函數不傳回值。

Notes
  • received 回呼被調用,並帶有四個參數:

Callback Signature

callback(message, linkname, index, value)

爭論

描述

message

收到的訊息名稱(例如, “add”)。

linkname

收到該訊息的連結名稱。

index

訊息中發送的整數值(如果提供)。

value

訊息中發送的浮點值(如果提供)。

Examples

機器人 1 的程式碼

// Send left or right when pressed
void screen_pressed() {
  if (Brain.Screen.xPosition() < 240) {
    link.send((uint8_t*)"left", 4);
  } else {
    link.send((uint8_t*)"right", 5);
  }
}

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

// Create the link
serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::manager, true);

// Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  Brain.Screen.print("Robot 1 - Manager");


  Brain.Screen.pressed(screen_pressed);
}

機器人2的程式碼

// Called when message received
void received_callback(uint8_t *buffer, int32_t length) {
  char msg[32];

  // Copy safely and null terminate
  int n = (length < 31) ? length : 31;
  memcpy(msg, buffer, n);
  msg[n] = '\0';

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
  Brain.Screen.print("Robot 2 - Worker");
  Brain.Screen.newLine();

  // Display if Robot 1's screen is pressed on left or right
  if (strcmp(msg, "left") == 0) {
    Brain.Screen.print("Manager pressed LEFT");
  }
  else if (strcmp(msg, "right") == 0) {
    Brain.Screen.print("Manager pressed RIGHT");
  }
}

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

// Create the link
serial_link link(PORT1, "VEXRoboticsLink123456789", linkType::worker, true);

// Do not run code UNTIL the Brains are linked
  while (!link.isLinked()) {
    wait(0.1, seconds);
  }

  link.received(received_callback);

  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1,1);
  Brain.Screen.print("Robot 2 - Worker");
  Brain.Screen.newLine();
  Brain.Screen.print("Waiting...");
}

上一頁

訊息連結

下一頁

CTE 工作單元

On this page
  • 介紹
  • 類別建構函數
  • 類別析構函數
  • 參數
  • 筆記
  • 例子
  • 成員功能
    • isLinked
    • 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)