Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectionStatusChanged not fired on disconneciton #22

Open
zizzo81 opened this issue Jan 18, 2022 · 2 comments
Open

ConnectionStatusChanged not fired on disconneciton #22

zizzo81 opened this issue Jan 18, 2022 · 2 comments

Comments

@zizzo81
Copy link

zizzo81 commented Jan 18, 2022

While talking with a device, I pull off the cable.
The port is removed by system.
The IsConnected becomes false but the the ConnectionStatusChanged is not fired.

On closure this gets logged. (Porta chiusa = Closed port)

<ERRO> 2022-01-18 15:31:37.5691 (6792:021) [SerialPortLib.SerialPortInput.LogError] System.InvalidOperationException: Porta chiusa.
   in System.IO.Ports.SerialPort.get_BytesToRead()
   in SerialPortLib.SerialPortInput.ReaderTask(Object data) in C:\projects\serialport-lib-dotnet\SerialPortLib\SerialPort.cs:riga 354 System.InvalidOperationException: Porta chiusa.
   in System.IO.Ports.SerialPort.get_BytesToRead()
   in SerialPortLib.SerialPortInput.ReaderTask(Object data) in C:\projects\serialport-lib-dotnet\SerialPortLib\SerialPort.cs:riga 354

About each second then I get this into log. (La porta 'COM7' non esiste = The port 'COM7' does not exist)

<ERRO> 2022-01-18 15:31:39.5880 (6792:022) [SerialPortLib.SerialPortInput.LogError] System.IO.IOException: La porta 'COM7' non esiste.
   in System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   in System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   in System.IO.Ports.SerialPort.Open()
   in SerialPortLib.SerialPortInput.Open() in C:\projects\serialport-lib-dotnet\SerialPortLib\SerialPort.cs:riga 288 System.IO.IOException: La porta 'COM7' non esiste.
   in System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
   in System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
   in System.IO.Ports.SerialPort.Open()
   in SerialPortLib.SerialPortInput.Open() in C:\projects\serialport-lib-dotnet\SerialPortLib\SerialPort.cs:riga 288

If I put the cable back, as soon as the port is ready again the ConnectionStatusChanged gets fired for a new connection.

I'm currently using reflection to check when IsConnected == false if "connectionWatcher" field is not null and force a Disconnect().
This simple hack seems to fix the issue, but the fact I'm searching for a private field after it's name is so ugly I decided to open this issue.

Thank you for your attention.

@polichan
Copy link

@zizzo81 Can you please tell me how you solved this problem and if you are willing to share some code snippets because I am also experiencing this problem and I am clueless at the moment. Thanks in advance.

@zizzo81
Copy link
Author

zizzo81 commented Feb 28, 2024

Sure, I'm actually having my receiving thread doing this check:

if (_serialPort != null && !_serialPort.IsConnected && _serialPort.GetType().GetFields("connectionWatcher", BindingFlags.NonPublic | BindingFlags.Instance) is FieldInfo field && field.GetValue(_serialPort) != null)
{
  _serialPort.Disconnect();
  return;
}

This is a "hack", I can't guarantee it will work for you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants