• 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
  • Αναφορά API VEX V5
  • VEXlink
  • Σειριακός σύνδεσμος
Ελληνικά
  • English
  • Español
  • 简体中文
  • VEXcode Robotics Logo

Πλοήγηση ενότητας

  • Μπλοκ
  • Πύθων
  • C++
    • Σύστημα μετάδοσης κίνησης
    • Κινητήρες και Ελεγκτές Κινητήρων
    • Ελεγκτής
    • Εγκέφαλος
    • Ανταγωνισμός
    • Συσκευές Έξυπνης Θύρας
    • Συσκευές 3 καλωδίων
    • Κονσόλα
    • Λογική
    • VEXlink
      • Σύνδεσμος μηνύματος
      • Σειριακός σύνδεσμος
    • CTE Workcell
  • Εκπαιδευτικά σεμινάρια 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

Σειριακός σύνδεσμος#

  • Εισαγωγή

  • Κατασκευαστές κλάσεων

  • Καταστροφέας κλάσης

  • Παράμετροι

  • Σημειώσεις

  • Παράδειγμα

  • Συναρτήσεις Μελών

    • είναι συνδεδεμένο

    • στέλνω

    • λαμβάνω

    • έλαβε

Εισαγωγή#

The serial_link class allows for a stream of raw bytes to be sent between robots. It’s designed for robot-to-robot communication where the transmitting and receiving robot both understand the contents of the data stream.

Every usage of send adds data to the linked V5 Brain’s queue, and the queue is read first-in, first-out (FIFO). If multiple messages are sent before the other V5 Brain uses receive, they will be stored and returned one at a time in the order they were sent. Because messages can queue, repeatedly sending the same status every loop may create backlog; for time-critical logic, send only when values change.

Important: Both robots must be running projects that use serial_link at the same time, or no data will be sent/received.

Κατασκευαστές κλάσεων#

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

Καταστροφέας κλάσης#

Destroys the serial_link object and releases associated resources.

~serial_link();

Παράμετροι#

Παράμετρος

Τύπος

Περιγραφή

index

int32_t

The Smart Port that the message link is connected to, written as PORTx, where x is the port number (for example, PORT1).

name

const char*

Το όνομα αυτού του συνδέσμου. Συνιστάται αυτή η μοναδική συμβολοσειρά να έχει αρκετά μεγάλο μήκος ώστε όταν κατακερματιστεί από το vexos να δημιουργήσει ένα μοναδικό αναγνωριστικό. Ένα λανθασμένο όνομα συνδέσμου θα ήταν κάτι γενικό, όπως “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

Whether the serial_link object is wired (defaults to false):

  • true — The serial_link object is wired
  • false — The serial_link object is wireless

Σημειώσεις#

  • Το VEXlink υποστηρίζει τόσο ασύρματη όσο και ενσύρματη επικοινωνία, με τις ενσύρματες συνδέσεις να συνιστούν ένα τροποποιημένο έξυπνο καλώδιο για την αποφυγή προβλημάτων δρομολόγησης ρεύματος.

  • Για ασύρματη επικοινωνία, κάθε ρομπότ χρειάζεται ένα V5 Robot Radio συνδεδεμένο σε μια Έξυπνη Θύρα.

  • Το VEXlink Radio μπορεί να χρησιμοποιηθεί παράλληλα με το VEXnet radio ενός V5 Controller, το οποίο θα πρέπει να είναι συνδεδεμένο στην Smart Port με τον μεγαλύτερο αριθμό για την αποφυγή διενέξεων.

  • Για να δημιουργήσετε ένα VEXlink, και οι δύο εγκέφαλοι V5 πρέπει να είναι συνδεδεμένοι σε ένα ρομποτικό ραδιόφωνο 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

Συναρτήσεις Μελών#

The serial_link class includes the following member functions:

  • isLinked — Checks if the V5 Brains are paired and communicating on this link.

  • send — Sends a message to the paired Brain.

  • receive — Waits for and returns the next incoming message.

  • received — Registers a function to be called whenever a new message is received.

Before calling any serial_link member functions, a serial_link instance must be created, as shown below:

Κωδικός για το Ρομπότ 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#

Επιστρέφει εάν οι εγκέφαλοι V5 σε μια σειριακή σύνδεση είναι ζευγαρωμένοι μεταξύ τους.

Available Functions
bool isLinked();

Parameters

Αυτή η συνάρτηση δεν έχει παραμέτρους.

Return Values

Επιστρέφει μια λογική τιμή που υποδεικνύει εάν οι εγκέφαλοι V5 είναι συνδεδεμένοι:

  • true — The two V5 Brains are paired and communicating on this link.
  • false — The two V5 Brains are not paired on this link.
Notes
  • Είναι καλή πρακτική να ελέγχετε πάντα για να βεβαιωθείτε ότι οι εγκέφαλοι V5 είναι συνδεδεμένοι στην αρχή ενός έργου πριν από την εκτέλεση οποιουδήποτε περαιτέρω κώδικα.

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 — Στέλνει ένα μήνυμα χρησιμοποιώντας ένα buffer byte.

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*

Το buffer που θα σταλεί στον άλλο συνδεδεμένο εγκέφαλο V5.

length

int32_t

Το μήκος του buffer που θα αποσταλεί.

Return Values

Returns an int32_t representing the number of bytes sent.

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

Το μήκος του buffer.

timeoutMs

int32_t

How long in milliseconds receive will wait for a new message only if the queue is empty (defaults to 500ms).

Return Values

Returns an int32_t representing the number of bytes received.

Notes
  • receive returns the next queued message from the other linked V5 Brain.

  • Τα μηνύματα διαβάζονται με σειρά FIFO (πρώτα τα παλαιότερα μη αναγνωσμένα).

  • If the queue is empty when receive is called, it waits up to the specified timeoutMs for a new message.

  • If no message arrives in that window, receive returns 0, and any message sent afterward remains in the queue to be read the next time receive is used.

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#

Καταγράφει μια συνάρτηση που θα καλείται κάθε φορά που ο εγκέφαλος V5 λαμβάνει δεδομένα.

Available Functions

1 — Καταχωρεί μια συνάρτηση επανάκλησης για κάθε buffer byte που λαμβάνεται.

void received( 
  void (* callback)(uint8_t *, int32_t) );

Parameters

Παράμετρος

Τύπος

Περιγραφή

callback

void (*)(uint8_t *, int32_t)

Μια προηγουμένως καθορισμένη συνάρτηση που εκτελείται όταν ο εγκέφαλος V5 λαμβάνει δεδομένα.

Return Values

Αυτή η συνάρτηση δεν επιστρέφει τιμή.

Notes
  • received callbacks are called with two arguments: a byte buffer and the number of bytes received.

  • Τα ληφθέντα δεδομένα ενδέχεται να μην έχουν τερματισμό null. Εάν η επανάκληση αντιμετωπίζει τα δεδομένα ως συμβολοσειρά, αντιγράψτε τα ληφθέντα byte σε έναν πίνακα χαρακτήρων και προσθέστε έναν τερματιστή null.

Callback Signature

callback(buffer, length)

Επιχείρημα

Περιγραφή

buffer

Ένας δείκτης προς το buffer byte που λήφθηκε.

length

Ο αριθμός των byte που ελήφθησαν.

Examples

Κωδικός για το Ρομπότ 1

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

// 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();

  // 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);

  while (true) {
    wait(10, msec);
  }
}

Κωδικός για το Ρομπότ 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...");

  while (true) {
    wait(10, msec);
  }
}

προηγούμενο

Σύνδεσμος μηνύματος

επόμενο

CTE Workcell

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 στο 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)