jcurl / RJCP.DLL.SerialPortStream

SerialPortStream is an independent implementation of System.IO.Ports.SerialPort and SerialStream for better reliability and maintainability. Default branch is 2.x and now has support for Mono with help of a C library.
Microsoft Public License
628 stars 197 forks source link

"Unable to clear the serial break state" exception in Open() #2

Closed jwoodwardtfx closed 8 years ago

jwoodwardtfx commented 8 years ago

When calling SerialPortStream.cs:Open() on a serial port whose driver does not support clearing the serial break state an exception is generated and the port cannot be opened.

There is an attempt to catch this exception when setting the "BreakState" in SerialPortStream.cs:Open() but the exception is actually first generated before this point by the call to "m_NativeSerial.SetPortSettings()" (towards the end of this function the break state is set or cleared, causing the exception to be generated).

[SerialPortStream.cs]
        private void Open(bool setCommState)
        {
            if (IsDisposed) throw new ObjectDisposedException("SerialPortStream");
            if (IsOpen) throw new InvalidOperationException("Serial Port already opened");

            m_NativeSerial.Open();
            try {
                if (setCommState) {
                    m_NativeSerial.SetPortSettings(); **<--- EXCEPTION HERE**
                    // Fetch the actual settings and get the capabilities
                    m_NativeSerial.GetPortSettings();

                    try {
                        m_NativeSerial.BreakState = false;
                    } catch (System.IO.IOException) {
                        // Ignore IOException. Not all serial port drivers support clearing the
                        // Break signal, so we ignore it when opening.
                    }
                }
[WinNativeSerial.cs]

        /// <summary>
        /// Writes the settings of the serial port as set in this object.
        /// </summary>
        /// <exception cref="ObjectDisposedException"/>
        /// <exception cref="InvalidOperationException">Port not open.</exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes")]
        public void SetPortSettings()
        {
            if (m_IsDisposed) throw new ObjectDisposedException("WinNativeSerial");
            if (!IsOpen) throw new InvalidOperationException("Port not open");

...snip...

            if (m_BreakState) {
                m_CommModemStatus.SetCommBreak();
            } else {
                m_CommModemStatus.ClearCommBreak(); **<--- EXCEPTION FROM HERE**
            }
        }
jcurl commented 8 years ago

Thanks for the bug report. Clearly a regression from 1.x. Can you let me know what chipset you use and if it's easily available for my own testing?

jwoodwardtfx commented 8 years ago

It's a USB cellular modem (u-blox TOBY L210) that enumerates as a number of COM ports.

Probably not easy to get hold of for you to test, but if you have any changes you want me to try then just let me know...

networkfusion commented 8 years ago

That sounds a lot like the issue I had back along... Try opening direct

jcurl commented 8 years ago

Can you try applying this patch if this fixes your problem? I don't have my equipment with me to test.

0001-Fix-exception-on-Windows-for-serial-drivers-that-don.patch.txt

jwoodwardtfx commented 8 years ago

Yep, that fixes it (it's the same as the fix I've made locally :p).

Thanks.