VEX U

Here’s some example code that shows use of the low level C API for sending and receiving on a V5 port that’s configured as a generic serial port. The ports are RS485, you will need to figure out the physical connection using a suitable adapter. There are other C functions beyond these few, look in the header files, they should be reasonably obvious how to use.

and no, you cannot communicate with a V5 smart device using this API.


using namespace vex;

int receiveTask() {
    // enable port 18 as generic serial port
    vexGenericSerialEnable( vex::PORT18, 0 );
    // change baud rate, default is 230k
    vexGenericSerialBaudrate( vex::PORT18, 115200 );
    // allow vexos to reconfigure the port
    // the port will remain as a generic serial port until the brain is power cycled
    this_thread::sleep_for(10);

    // buffer for rx data
    uint8_t rxbuf[256];

    int totalRead = 0;

    while(1) {
      // check to see if we have any bytes in the receive buffer
      int nRead = vexGenericSerialReceive( vex::PORT18, rxbuf, sizeof(rxbuf)  );
    
      // yes ? then display
      if( nRead > 0 ) {
        totalRead += nRead;
        Brain.Screen.setCursor( 4, 2 );
        Brain.Screen.print("Bytes received %d", totalRead );


        if( nRead > 16 )
          nRead = 16;
        Brain.Screen.setCursor( 5, 2 );
        Brain.Screen.clearLine();
        Brain.Screen.setCursor( 5, 2 );
        for(int i=0;i<nRead;i++)
          Brain.Screen.print("%02X ", rxbuf* );
      }
    
      // read often
      this_thread::sleep_for(5);
    }
  
    return(0);
}

int main() {
    int msgCount = 0;

    // start the rx thread
    vex::thread rxThread( receiveTask );

    // enable port 20 as generic serial port
    vexGenericSerialEnable( vex::PORT20, 0 );
    // change baud rate, default is 230k
    vexGenericSerialBaudrate( vex::PORT20, 115200 );
    // allow vexos to reconfigure the port
    // the port will remain as a generic serial port until the brain is power cycled
    this_thread::sleep_for(10);

    char msg] = "Hello World";
    int  msglen = strlen( msg );
  
    while(1) {
      // send message
      vexGenericSerialTransmit( vex::PORT20, (uint8_t *)msg, msglen  );
      // 
      Brain.Screen.setCursor( 3, 2 );
      Brain.Screen.print( "Number sent %d", msgCount++ );

      // Allow other tasks to run
      // send message every 100mS
      this_thread::sleep_for(100);
    }
}