FengChendian / serial_port_win32

A flutter SerialPort library using win32 API.
BSD 3-Clause "New" or "Revised" License
31 stars 9 forks source link

Detect Serial Port disconnection #question #28

Open altafc22 opened 1 year ago

altafc22 commented 1 year ago

Hi @FengChendian, thanks for creating such a great package for flutter, just wandering is there any way to detect serial device disconnection, i was looking arround it in my application. kindly waiting for your response on this.

Thanks

FengChendian commented 1 year ago

I think Microsoft doesn't provide a disconnection function directly. In my opinion, there are some indirect methods to detect the device connection status.

  1. You can write bytes to COM periodically. If device is disconnected, write should fail. And GetLastError() will return ERROR_HANDLE_INVALID. Read function is also like this. Maybe it's a little stupid...
  2. GetCommState should return 0 when device disconnected. Periodically calling GetCommState may be a good solution. I will test and add some function if it works.
  3. SerialPort.getAvailablePorts() will lose current COM port if device disconnected.
  4. I'm reading win32 doc to find better solutions