0

I want to check if one specific device is connected to one of my serial ports.

with list_ports.comports() I get all the COM Ports
with x = list_ports.grep("0403:6001") I can search for a specific device. This gives me
<class 'serial.tools.list_ports_common.ListPortInfo'> output.

But I just want an a bolean expression as an output if this device is connected or not

I have read this answer, but it didnt solve my problem Pyserial get the name of the device behind a COM Port

Sator
  • 636
  • 4
  • 13
  • 34

1 Answers1

1

According to the manual .grep() returns an iterable of ListPortInfos, and I assume it'd return nothing if there was no matching device connected.

To get from an iterable to a boolean, the built-in any() is your friend:

device_connected = any(list_ports.grep("0403:6001"))
AKX
  • 152,115
  • 15
  • 115
  • 172