• 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
Pāriet uz galveno saturu
Ctrl+K

< Back to Platform Select

  • VEX V5
  • VEX V5 API atsauce
  • VEXlink
  • Seriālā saite
Latviešu
  • English
  • العربية
  • বাংলা
  • Česky
  • Deutsch
  • Ελληνικά
  • Español
  • Español Latinoamericano
  • Suomi
  • Français
  • Gaeilge
  • हिंदी
  • Magyar
  • Bahasa Indonesia
  • 日本語
  • 한국어
  • Lietuvių
  • മലയാളം
  • Nederlands
  • Polski
  • Português
  • Русский
  • Svenska
  • ไทย
  • Türkçe
  • Українська
  • Tiếng Việt
  • 简体中文
  • 繁體中文
  • VEXcode Robotics Logo

Sadaļu navigācija

  • Bloki
  • Python
  • C++
    • Piedziņa
    • Motori un motoru kontrolleri
    • Kontrolieris
    • Smadzenes
    • Konkurence
    • Viedās ostas ierīces
    • 3 vadu ierīces
    • Konsole
    • Loģika
    • VEXlink
      • Ziņojuma saite
      • Seriālā saite
    • CTE darba šūna
  • VEXkoda pamācības

Platformas navigācija

  • 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

Seriālā saite#

  • Ievads

  • Klases konstruktori

  • Klases iznīcinātājs

  • Parametri

  • Piezīmes

  • Piemērs

  • Dalībnieku funkcijas

    • ir saistīts

    • sūtīt

    • saņemt

    • saņemts

Ievads#

Klase serial_link ļauj starp robotiem nosūtīt neapstrādātu baitu plūsmu. Tā ir paredzēta robotu savstarpējai saziņai, kur gan raidošais, gan saņemošais robots saprot datu plūsmas saturu.

Katrs send lietojums pievieno datus saistītās V5 Brain rindai, un rinda tiek lasīta pēc principa “pirmais iekšā, pirmais ārā” (FIFO). Ja pirms citu V5 Brain funkciju saņemšanas tiek nosūtīti vairāki ziņojumi, tie tiks saglabāti un atgriezti pa vienam tādā secībā, kādā tie tika nosūtīti. Tā kā ziņojumi var tikt ievietoti rindā, atkārtota viena un tā paša statusa nosūtīšana katrā ciklā var radīt uzkrāšanos; laika ziņā kritiskas loģikas gadījumā sūtiet tikai tad, kad mainās vērtības.

Svarīgi: Abiem robotiem vienlaikus jādarbojas projektos, kas izmanto serial_link , pretējā gadījumā dati netiks nosūtīti/saņemti.

Klases konstruktori#

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

Klases iznīcinātājs#

Iznīcina objektu serial_link un atbrīvo ar to saistītos resursus.

~serial_link();

Parametri#

Parametrs

Tips

Apraksts

index

int32_t

Viedports, ar kuru ir savienota ziņojuma saite, kas rakstīts kā PORTx, kur x ir porta numurs (piemēram, PORT1).

name

const char*

Šīs saites nosaukums. Ieteicams, lai šī unikālā virkne būtu pietiekami gara, lai, to jaukjot ar vexos, tiktu izveidots unikāls ID. Nepareizs saites nosaukums būtu kaut kas vispārīgs, piemēram, “vexlink”, jo to varētu izmantot cita komanda.

type

linkType

Saites veids — linkType::manager vai linkType::workerŠī informācija tiek izmantota, lai pareizi konfigurētu radio, kā arī noteiktu pieejamo joslas platumu pārraidei un uztveršanai. Vadītāja robotam ir divreiz lielāks pieejamais joslas platums (1040 baiti sekundē), lai nosūtītu informāciju darba robotam (520 baiti sekundē).

  • linkType::manager — Serial_link objekts ir vadītājs
  • linkType::worker — Serial_link objekts ir darbinieks

isWired

bool

Vai serial_link objekts ir savienots ar vadu (pēc noklusējuma false):

  • true — serial_link objekts ir savienots ar vadu
  • false — serial_link objekts ir bezvadu

Piezīmes#

  • VEXlink atbalsta gan bezvadu, gan vadu saziņu, un vadu savienojumiem ieteicams modificēts viedkabelis, lai novērstu strāvas padeves problēmas.

  • Bezvadu saziņai katram robotam ir nepieciešams V5 robota radio, kas savienots ar viedportu.

  • VEXlink radio var izmantot līdzās V5 kontrollera VEXnet radio, kas jāpievieno viedajai pieslēgvietai ar augstāko numuru, lai izvairītos no konfliktiem.

  • Lai izveidotu VEXlink, abām V5 smadzenēm jābūt savienotām ar V5 robota radio.

Piemērs#

1. robota kods

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

Robots 2 kods

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

Dalībnieku funkcijas#

Klasē serial_link ir iekļautas šādas dalībfunkcijas:

  • isLinked — Pārbauda, ​​vai V5 smadzenes ir savienotas pārī un sazinās šajā saitē.

  • send — Nosūta ziņojumu pārī savienotajām smadzenēm.

  • receive — Gaida un atgriež nākamo ienākošo ziņojumu.

  • received — Reģistrē funkciju, kas tiks izsaukta ikreiz, kad tiek saņemts jauns ziņojums.

Pirms jebkuru serial_link dalībfunkciju izsaukšanas ir jāizveido serial_link instance, kā parādīts zemāk:

1. robota kods

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

Robots 2 kods

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

isLinked#

Atgriež, vai V5 smadzenes seriālajā saitē ir savienotas pārī viena ar otru.

Available Functions
bool isLinked();

Parameters

Šai funkcijai nav parametru.

Return Values

Atgriež Būla vērtību, kas norāda, vai V5 smadzenes ir saistītas:

  • true — The two V5 Brains are paired and communicating on this link.
  • false — The two V5 Brains are not paired on this link.
Notes
  • Pirms jebkāda turpmāka koda palaišanas projekta sākumā vienmēr ir ieteicams pārbaudīt, vai V5 smadzenes ir saistītas.

Examples

1. robota kods

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

Robots 2 kods

// 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#

Nosūta ziņojumu pārī savienotajām smadzenēm.

Available Functions

1 — Nosūta ziņojumu, izmantojot baitu buferi.

int32_t send( 
  uint8_t *buffer, 
  int32_t  length );

2 — Nosūta ziņojumu, izmantojot rakstzīmju buferi.

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

Parameters

Parametrs

Tips

Apraksts

buffer

uint8_t* or const char*

Buferis, kas jānosūta uz otru saistīto V5 Brain.

length

int32_t

Nosūtāmā bufera garums.

Return Values

Atgriež int32_t , kas apzīmē nosūtīto baitu skaitu.

Examples

1. robota kods

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

Robots 2 kods

// 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#

Gaida un atgriež nākamo ienākošo ziņojumu.

Available Functions

1 — Saņem ziņojumu rakstzīmju buferī ar papildu taimautu.

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

Parameters

Parametrs

Tips

Apraksts

buffer

char*

Buferis saņemtā ziņojuma glabāšanai.

length

int32_t

Bufera garums.

timeoutMs

int32_t

Cik ilgi milisekundēs receive gaidīs jaunu ziņojumu tikai tad, ja rinda ir tukša (pēc noklusējuma 500 ms).

Return Values

Atgriež int32_t , kas apzīmē saņemto baitu skaitu.

Notes
  • receive atgriež nākamo rindā esošo ziņojumu no otras saistītās V5 smadzenes.

  • Ziņojumi tiek lasīti FIFO secībā (vispirms vecākie nelasītie).

  • Ja rinda ir tukša, kad tiek izsaukta komanda receive , tā gaida jaunu ziņojumu līdz norādītajam timeoutMs laikam.

  • Ja šajā logā netiek saņemts neviens ziņojums, receive atgriež 0, un visi pēc tam nosūtītie ziņojumi paliek rindā, lai tos lasītu nākamajā reizē, kad tiks izmantota receive.

Examples

1. robota kods

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

Robots 2 kods

// 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#

Reģistrē funkciju, kas tiks izsaukta ikreiz, kad V5 Brain saņem datus.

Available Functions

1 — Reģistrē atzvanīšanas funkciju katram saņemtajam baitu buferim.

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

Parameters

Parametrs

Tips

Apraksts

callback

void (*)(uint8_t *, int32_t)

Iepriekš definēta funkcija, kas darbojas, kad V5 Brain saņem datus.

Return Values

Šī funkcija neatgriež vērtību.

Notes
  • received atzvanīšanas signāli tiek izsaukti ar diviem argumentiem: baitu buferi un saņemto baitu skaitu.

  • Saņemtie dati, iespējams, nav pabeigti ar nulli. Ja atzvanīšanas funkcija apstrādā datus kā virkni, kopējiet saņemtos baitus rakstzīmju masīvā un pievienojiet nulles pārtrauktāju.

Callback Signature

callback(buffer, length)

Arguments

Apraksts

buffer

Rādītājs uz saņemto baitu buferi.

length

Saņemto baitu skaits.

Examples

1. robota kods

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

Robots 2 kods

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

iepriekšējs

Ziņojuma saite

nākošais

CTE darba šūna

On this page
  • Ievads
  • Klases konstruktori
  • Klases iznīcinātājs
  • Parametri
  • Piezīmes
  • Piemērs
  • Dalībnieku funkcijas
    • isLinked
    • send
    • receive
    • received
Innovation First, International
​

VEX un VEX Robotics ir Innovation First, Inc. preču zīmes vai pakalpojumu zīmes. Autortiesības ©2026Visas tiesības aizsargātas. VEX Robotics, Inc. ir Innovation First International, Inc. meitasuzņēmums. Visi pārējie citu produktu nosaukumi/zīmes pieder to attiecīgajiem īpašniekiem. Patenti un/vai patenti gaida apstiprinājumu - innovationfirst.com/patents
Vietnes konfidencialitātes politika / Vietnes lietošanas noteikumi / Sīkfailu politika / Programmatūras konfidencialitātes politika

Apmeklējiet VEX Robotics Facebook lapu Apmeklējiet VEX Robotics Twitter lapu Apmeklējiet VEX Robotics Instagram lapu Apmeklējiet VEX Robotics YouTube lapu
VEX API atsauksmju veidlapa

Mēs novērtējam jūsu atsauksmes! Izmantojiet šo veidlapu, lai sniegtu ieteikumus, komplimentus vai ziņotu par kļūdām saistībā ar VEX API. Jūsu sniegtā informācija palīdz mums uzlabot VEX API dokumentāciju.

​

Ja rodas tehniskas problēmas vai nepieciešams klientu atbalsts, lūdzu, apmeklējiet vietni support.vex.com.

  • Send Happy Feedback
  • Send Sad Feedback

Piezīme. Pašreizējais URL tiks kopīgots ar jūsu ziņojumu.

Norādot savu e-pasta adresi, jūs piekrītat, ka VEX var jums sūtīt e-pastus, ja mums ir jautājumi par jūsu atsauksmēm.
Privātuma politika >
Lūdzu, sniedziet savu atsauksmi. Atsauksme iesniegta veiksmīgi!
Choose Which VEX IQ Generation to View

VEX IQ (1st gen)

VEX IQ (2nd gen)