2023-11-06 20:44:36 +00:00
|
|
|
import serial.tools.list_ports
|
|
|
|
import crcengine
|
|
|
|
|
|
|
|
def get_ports():
|
2024-05-11 08:45:15 +00:00
|
|
|
crc_algorithm = crcengine.new("crc16-ccitt-false") # load crc16 library
|
2023-11-06 20:44:36 +00:00
|
|
|
|
|
|
|
serial_devices = []
|
2024-05-11 08:45:15 +00:00
|
|
|
ports = serial.tools.list_ports.comports(include_links=False)
|
2023-11-06 20:44:36 +00:00
|
|
|
for port, desc, hwid in ports:
|
|
|
|
# calculate hex of hwid if we have unique names
|
|
|
|
crc_hwid = crc_algorithm(bytes(hwid, encoding="utf-8"))
|
|
|
|
crc_hwid = crc_hwid.to_bytes(2, byteorder="big")
|
|
|
|
crc_hwid = crc_hwid.hex()
|
|
|
|
description = f"{desc} [{crc_hwid}]"
|
|
|
|
serial_devices.append(
|
|
|
|
{"port": str(port), "description": str(description)}
|
|
|
|
)
|
|
|
|
return serial_devices
|