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
624 stars 197 forks source link

port.Close() hangs if 'ccTalk' setup is used #91

Closed Catter38 closed 4 years ago

Catter38 commented 5 years ago

The issue was observed with the ccTalk protocol (more information). TLDR: ccTalk is a protocol, where you have to short the RX and TX lines, to get a single multi-drop bus line.

If I try to close a port using this setup, it hangs and even if I force kill the application, I have to restart the computer to get it working again, otherwise I get an System.UnauthorizedAccessException if I try to open the port again. I was able to trace the hang back to line 257 in CommOverlappedIo.cs (m_Thread.Join();). It seems like some kind of deadlock in OverlappedIoThreadMainLoop since it never finishes the thread, even if m_StopRunning is set.

The only way to reproduce the issue, is to cut a serial cable and short the RX and TX lines. After that it can easily be reproduced with something like this:

var port = new SerialPortStream("COM8", 9600, 8, Parity.None, StopBits.One);

port.Open();
port.DiscardInBuffer();
port.Write(new byte[] {0x00, 0x01, 0x02, 0x03}, 0, 4);

Thread.Sleep(40);

port.Close();

Console.ReadLine();
jcurl commented 5 years ago

when you say "short the Rx/Tx" lines, do you mean connect RX <-> TX, or RX - GND and TX - GND?

Catter38 commented 5 years ago

I mean RX <-> TX. This is needed to be able to connect many devices to a single "Data" line, so every device can talk to every other device on the "Bus".

jcurl commented 5 years ago

What you've described is just nothing more than testing with a NULL modem cable. Could you further investigate by identifying at what line the software is blocked in the thread that is not joining?

Catter38 commented 5 years ago

Yes, I'll try to identify the exact line and report back if there are any news.

jcurl commented 4 years ago

I need a reproducible case, and possibly a coredump with the binaries where the hang occurs so I can further investigate, as don't have any equipment here. Closing.