FengChendian / serial_port_win32

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

Detect Serial Port disconnection #question #28

Open altafc22 opened 10 months ago

altafc22 commented 10 months 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 10 months 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