COM port DataReceived not firing?

Can anyone suggest why the com port DataReceived is not firing?

It worked for a while now it doesn’t. I have 2 com devices on com1 and com4 but both are not receiving data. Usinf Fez Cobra || with netmf 4.3

        _gpsSP = new SerialPort(COMPort, 9600);
                Thread.Sleep(2000);
                _gpsSP.Open();
                _gpsSP.DataReceived += new SerialDataReceivedEventHandler(_gpsSP_DataReceived);
                _gpsSP.ErrorReceived += new SerialErrorReceivedEventHandler(_gpsSP_DataError);
I just noticed that _gpsSP_DataError  fires?   What does this tell me?  Return EventType=Frame  m_error=4

Is this error stopping the _gpsSP_DataReceived event firing?

@ anthonys -

It is only fired the once, you have to clear the received buffer for next fire, if I remember correctly

@ anthonys -
Hi, what means ComPort in your example? I think it must be … new SerialPort(“COM1”, 9600);

Also note that most types of serial errors (framing, buffer overrun, etc) will cause the DataReceived event to stop firing. This is a known issue in netmf.

@ mcalsyn - Can you provide any URLs where those type of issues (DataReceived event stops firing) are listed/described/reported?

I now do all receive handling in a thread. The DataReceived event is just not ideal. Create a thread to process the received data.

Unfortunately not at the moment - I am in an airport on a sub-optimal connection, but if I remember, I will look when I get back. It is mentioned in these forums, for sure.

@ mcalsyn - Interesting, I don’t recall reading about it :think:

@ iamin - Are you interested in serial port DataReceive event on any NETMF platform or only the original OPs Fez Cobra platform?

@ PHITEK - I am interested in general, i. e. any platform, but I am mostly concerned with Cobra II as it is my favorite board.

@ anthonys -

Connect their TX and RX together to see if your serial port is working first :smiley:

@ Dave McLaughlin - Can you refer me to an example method of doing this? Are you saying not to use the datareceive event?

@ Dat - thanks…why didn;'t i think of that

I am not saying don’t use it, but I’ve never found it reliable enough to work with the telegrams I have been using.

I have a number of ways depending on the process. For one method, I create a thread to read the data from the serial port and stick this into a circular buffer. This buffer is then read by the foreground task. I have functions to read bytes and advance the pointers to the data.

Another way is to simply call a function to read the buffer if your link is half duplex. This works great for poll and request type serial devices such as modems. With this I call a function to read the serial port and I pass in a timeout value. If I get data within the timeout period I then read the data until I have a 3-4 character time gap. Modbus works this way and I’ve found this to be pretty reliable. If not data within the initial timeout I return null or some other flag to indicate no data.

Quite a few ways to handle serial data.

By the way, have a look at the Cellular Modem driver as that uses a thread the handle the data reception.

Just remember to use Thread.Sleep(0) in there so other threads can still run.

@ Dave McLaughlin - thanks Dave. I assume i put Thread.Sleep(0) at the end of the program function? Where can i see the modem drivers code?

I got them from the gadgeteer source. Needs some work to port to Pure NETMF.

https://gadgeteer.codeplex.com/SourceControl/latest

@ anthonys -

Did you:

  • Connect FEZ Cobra II TX and RX and it working?
  • Connect FEZ Cobra II to another device?

I don’t think problem is in Fez cobra II or your code, it looks fine to me, something wrong with your connection or your gps device.
Simply FEZ Cobra doesn’t see coming data then no event fired.
Something very simple but we are usually forget (usually happens to me :)), they are: swap TX and RX, baudrate, comport id…

Thanks Guys…got it working by implementing a loop on thread to read the port data…as Dave suggested, alot more reliable!

Hi…
COM Serial Port data received event note firing…i can’t find solution
My code is

                    if (_serialPort != null && _serialPort.IsOpen)
                        _serialPort.Close();
                    if (_serialPort != null)
                        _serialPort.Dispose();

                    _serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);   
                    _serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortOnDataReceived);

                    try
                    {
                        if (!_serialPort.IsOpen)
                        {
                            _serialPort.Open();
                        }
                    }
                    catch (InvalidOperationException ex)
                    {
                    }