Rs232 module serialPort_DataReceived only read zeros from serial port

experimenting wit the rs232 module from GHI i only get zeros sending three bytes to it reading the port like;

 void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {

          

            HostorClient = 1;



             // Create a buffer that is the length of the data that is available
            // the BytesToRead variable tells us how many bytes are waiting in a buffer.
            byte[] readData = new byte[rs232.serialPort.BytesToRead];

            // Now we read the data into the buffer we just created, starting at the 
            //  first byte.
            

            // Use the data that you just read however you need to

           if (Hconnected)
            {
                rs232.serialPort.Read(readData, 0, readData.Length);

                if (readData.Length> 0)
                {

                    serialUSBHost.Write(readData, 0, readData.Length);
                    serialUSBHost.Flush();

                    cdcwrite1(readData, readData.Length, HostorClient);
                }
            }

but using this as a thread with a while loop functions well.

wath is wrong with the serialdatareceived void.








        }

For a start, flush() is a very bad thing to put in your code. If you think you need flush(), you’re wrong… Read all the data available in the code block, put it in your external queue, and then exit the handler.

Make your experimenting code simpler. Get rid of CDC and get rid of Hconnected test. Read Just the data that is there, debug.print it, and exit the handler and see if that works.

One other thing I would not do that you have done, is use the global serial port object; you probably should use “sender” as it will always be correct for what port you’re meant to use.

I also notice that you’re using Gadgeteer interface, which might change things (they override some of the netmf methods). Might be worth seeing if your code works differently when just using netmf.

There are many many valid uses of flush(), but I would say that this case is definitely not one of them.

Yes, ok, Flush() has it’s uses but certainly not in DataRecieved events - flush() is useful when you’re sending data not receiving data. Often though you might need to be considering DiscardInBuffer or DiscardOutBuffer.

But he is writing… he’s reading from the serial port and writing to a different serial port.

Most likely he only needs one flush() after the loop, but that doesn’t help him with the original problem…

the netmf conuterpart works oke :



 static  System.IO.Ports.SerialPort   netmfcom;

 netmfcom = new System.IO.Ports.SerialPort("COM2", 115200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
           netmfcom.Open();

netmfcom.DataReceived += new SerialDataReceivedEventHandler(netmfcom_ReceiveData);


 void netmfcom_ReceiveData(object sender, SerialDataReceivedEventArgs e)
        {

            byte[] readsender = new byte[netmfcom.BytesToRead];

          netmfcom.Read(readsender, 0, readsender.Length);





        }