1

Resolved

SerialPort.ReadLineProcess Exception Issue

description

hello
 
i use socket type u on a ghi gadgeteer board to communicate through rs232 with a synapse wireless module (pin 4 and 5)
pin 3 is used to have the possibility to reset the synapse module (active low) if the module hangs
 
what i would expect is no exception when the serial port is closed
 
the important code part and the result is attached
 
gadgeteer core version 2.41.500.0
framework version 4.1.2821.0
 
br
Andre

file attachments

comments

cyberdummy wrote Apr 4, 2012 at 7:39 PM

hello

may be it has todo with the 2 definitions of the SerialDataReceivedEventHandler within Serial.cs

the first definition is during the public Serial() method and the second during the Open() method

i would recommend to remove the handler in the public Serial() method and let the only one in the Open() method

br
Andre

StevenJohnston wrote Apr 13, 2012 at 6:20 PM

Hello,

Thanks for this, I will look into it and get back to you.

Regards
Steven

StevenJohnston wrote Apr 16, 2012 at 11:31 AM

Dear Andre,

I can repeat the issue here. It is beacause Serial close() can close the port after a check to IsOpen() and before Read().

We will probably lock the serial port during a read to solve this issue. It will be updated in the next version:

If you need a temp fix for now, this appears to work.

<snip>
private void ReadLineProcess()
    {
        string outstandingText = String.Empty;
        byte[] buf = new byte[1];

        this._serialPort.ReadTimeout = 1000;
        while (true)
        {
            try
            {
                if (!this._serialPort.IsOpen)
                {
                    Thread.Sleep(100);
                    continue;
                }
                int res = -1;
                lock (_serialPort)//Lock to avoid a close() call between isOpen and read
                {
                    if (this._serialPort.IsOpen)//Check it did not get closed
                    {
                        res = this._serialPort.Read(buf, 0, 1);
                    }
                    else
                    {
                        continue; //Closed 
                    }
                }
                if (res <= 0)
                    { ...
<snip>

wrote May 11, 2012 at 9:36 AM

wrote Feb 22, 2013 at 12:45 AM

wrote May 16, 2013 at 12:26 PM

wrote May 16, 2013 at 12:26 PM

wrote Jun 14, 2013 at 8:28 AM