ofxSerial write byte

Hi Everyone,

I’m trying to communicate with the digital transceiver K-LD7 (here the datasheet [https://www.rfbeam.ch/files/products/40/downloads/Datasheet_K-LD7.pdf ] ) through a serial to USB converter using the ofxSerial addon. The packet format is composed by Header ( ASCII character, 4 bytes ), Payload Length (UINT32, 4 bytes, sent as little endian), Payload. Taking as an example the command to establish a connection with the radar would be the following one:

  • Header: INIT
  • Payload Length: 4
  • Values: 0= 115200, 1= 460800 (corresponding to baud rate)

In arduino i could communicate in this way:

// Send header
 Serial1.write("INIT");
// Send payload length in little endian (LSB first)
 Serial1.write(4);
 Serial1.write(0);
 Serial1.write(0);
 Serial1.write(0);
// Send payload 0x00000000 in little endian -> baudrate stays at 115200
 Serial1.write(0);
 Serial1.write(0);
 Serial1.write(0);
 Serial1.write(0);

in OF I can establish the connection but then looks like I can’t communicate ( I don’t get any response). Here is the code I’m using:

 device.DATA_BITS_EIGHT;
 device.PAR_EVEN;
 device.STOP_ONE;
 device.FLOW_CTRL_NONE;
    
 string mydevice = "/dev/tty.SLAB_USBtoUART";
 int baudrate = 115200;
 // Connect to the first matching device.
 bool success = device.setup(mydevice, baudrate);

    if (success)
    {
        ofLogNotice("ofApp::setup") << "Successfully setup " << mydevice;
    }
    else
    {
        ofLogNotice("ofApp::setup") << "Unable to setup " << mydevice;
    }
   

  device.writeBytes("INIT");
 
  device.writeByte(4);
  device.writeByte(0);
  device.writeByte(0);
  device.writeByte(0);
   
  device.writeByte(0);
  device.writeByte(0);
  device.writeByte(0);
  device.writeByte(0);
 


     //we want to read 8 bytes
    int bytesRequired = 8;
    unsigned char bytes[bytesRequired];
    int bytesRemaining = bytesRequired;
    // loop until we've read everything
    while ( bytesRemaining > 0 )
    {
      // check for data
      if ( device.available() > 0 )
      {
        // try to read - note offset into the bytes[] array, this is so
        // that we don't overwrite the bytes we already have
        int bytesArrayOffset = bytesRequired - bytesRemaining;
        int result = device.readBytes( &bytes[bytesArrayOffset], bytesRemaining );
          
        cout << "reading " << result << endl;
        // check for error code
        if ( result == OF_SERIAL_ERROR )
        {
          // something bad happened
          ofLog( OF_LOG_ERROR, "unrecoverable error reading from serial" );
          // bail out
          break;
        }
        else if ( result == OF_SERIAL_NO_DATA )
        {
          // nothing was read, try again
        }
        else
        {
          // we read some data!
          bytesRemaining -= result;
        }
      }
    }

Do you have an idea of what I’m doing wrong?
Thanks

OK, I found out the error. I have to specify the configuration of the serial connection in the setup function.

 string mydevice = "/dev/tty.SLAB_USBtoUART";
 int baudrate = 115200;
 bool success = device.setup(mydevice,
                             baudrate,
                             device.DATA_BITS_EIGHT,
                             device.PAR_EVEN,
                             device.STOP_ONE,
                             device.FLOW_CTRL_NONE);