Cerberus and usbSerial module

Hi All,
I’m performing some test (cerberus and sdk beta 4.2) to send data over serial using usbSerial module. Provably i’m missing some thing, I’m using "DataReveived’ event, but the event only fires the first reception of data.

I don’t know if this is an unknow bug, or I need some aditional code.
My test code:


        void ProgramStarted()
        {
            usbSerial.Configure(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One,8);
            usbSerial.SerialLine.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(SerialLine_DataReceived);
            usbSerial.SerialLine.Open();
            Debug.Print("Program Started");
        }

        void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            usbSerial.SerialLine.Write("Data Ready!");
        }

Thanks,

Use code tags please.

Try to open serial line first and then subscribe for the event.

Thanks Architect,
I think the question are inside the small differences between System.IO.Ports.SerialPort from Framework and the GT.Interfaces.Serial :slight_smile:
The ‘DataReceived’ Event in IO.Ports is fired every time when the buffer are incoming new bytes. Now I have understood “GT.Interfaces.Serial” only fires the event one time until the buffer becomes empty again.

I hope this help others like me.
to test you can simply use ‘echo response’


        void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            int toRead = usbSerial.SerialLine.BytesToRead;
            byte[] buffer = new byte[toRead];
            usbSerial.SerialLine.Read(buffer, 0, toRead);
            usbSerial.SerialLine.Write(buffer, 0, toRead); // echo the buffer
        }

Good, so you got it working?

Yes, of course.
As a try explain, you must be carefull to read all bytes from buffer in every ‘DataReceived’ event, otherwise the handler is not firing a new event until the buffer is empty again.

Thanks!

Handling of data in the datarecieved event handler is a common source of confusion, great to hear you have it sorted.

My interpretation of the datarecieved event handler is always that you should de-queue all the data that you have and push it into your app for consumption elsewhere (which is why this event type isn’t always a good way to handle a serial port)