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

Serial Port already opened #95

Closed guaicaiboshi closed 4 years ago

guaicaiboshi commented 5 years ago

try { if (!this.serialPortStream.IsOpen) { this.serialPortStream.Open(); } } catch (Exception e) { this.Logger.InfoFormat($"Open port:{this.currCommand.PortName} failure:{e}");
return; }

Open port:COM5 failure:System.InvalidOperationException: Serial Port already opened.

My program ran for a while and occasionally had this problem.I'm using version 1.0.0. I am running on Windows core2.2

jcurl commented 5 years ago

Sorry to hear. The OS doesn't provide a mechanism to know if the serial port is in use or not, so software can only track the usage of the Open() and CloseHandle() APIs. I suspect that the OS is reporting this error, which means that internally it gets the CloseHandle() request, but doesn't clean up resources immediately. This would be driver dependent.

Do you have a backtrace of the exception? I expect it to be raised by Windows directly.

guaicaiboshi commented 5 years ago

I need to run for a period of time again see if we can, if so, I need to add logic to handle, if it is System. InvalidOperationException: Serial Port already the opened is unusual, I will continue to implement the logic behind