FengChendian / serial_port_win32

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

How to check if COM port is disconnected #33

Open sneiksus opened 3 months ago

sneiksus commented 3 months ago

Greeting, is there a way to check that USB cable was unpluged? isOpened property always return true even when the usb cable was physically disconnected?

FengChendian commented 3 months ago

isOpened property just reflects user code states in dart. So it's true if the usb cable was physically disconnected.

I know you want isOpened to automatically become false when cable disconnected. It needs WNDPROC callback function in winuser.h And may be multi-thread? I didn't implement this function because I don't know how dart implements win32 system callback function. 😭 Maybe win32 package supports this function now. I will try it.

Now, there are some other solutions to check USB COM PORT connection status:

  1. Using getAvailablePorts function to compare lists
  2. try write bytes or read bytes, and catch exception
  3. try close and reopen it