list_port find nothing
The functions provided by list_ports find no serial ports in /dev, however, there are ports there that can be found with other means.
import os
import re
from serial.tools import list_ports
for port_info in list_ports.grep(r"tty[0-9]+", include_links=True):
print(port_info.device)
print("####################################")
for port_info in list_ports.comports(include_links=True):
print(port_info.device)
print("####################################")
for port in [f"/dev/{dev_file}" for dev_file in os.listdir("/dev") if re.search(r"tty[0-9]+", dev_file)]:
print(port, end=" ")
The output is
####################################
####################################
/dev/tty63 /dev/tty62 /dev/tty61 /dev/tty60 /dev/tty59 /dev/tty58 /dev/tty57 /dev/tty56 /dev/tty55 /dev/tty54 /dev/tty53 /dev/tty52 /dev/tty51 /dev/tty50 /dev/tty49 /dev/tty48 /dev/tty47 /dev/tty46 /dev/tty45 /dev/tty44 /dev/tty43 /dev/tty42 /dev/tty41 /dev/tty40 /dev/tty39 /dev/tty38 /dev/tty37 /dev/tty36 /dev/tty35 /dev/tty34 /dev/tty33 /dev/tty32 /dev/tty31 /dev/tty30 /dev/tty29 /dev/tty28 /dev/tty27 /dev/tty26 /dev/tty25 /dev/tty24 /dev/tty23 /dev/tty22 /dev/tty21 /dev/tty20 /dev/tty19 /dev/tty18 /dev/tty17 /dev/tty16 /dev/tty15 /dev/tty14 /dev/tty13 /dev/tty12 /dev/tty11 /dev/tty10 /dev/tty9 /dev/tty8 /dev/tty7 /dev/tty6 /dev/tty5 /dev/tty4 /dev/tty3 /dev/tty2 /dev/tty1 /dev/tty0
Tested with pyserial 3.5 under Linux 5.15, with and without root right
I tested it in my device and I modify the regex to .*
import os
import re
from serial.tools import list_ports
for port_info in list_ports.grep(r".*", include_links=True):
print(port_info.device)
print("####################################")
for port_info in list_ports.comports(include_links=True):
print(port_info.device)
print("####################################")
for port in [f"/dev/{dev_file}" for dev_file in os.listdir("/dev") if re.search(r"tty[0-9]+", dev_file)]:
print(port, end=" ")
The output is
/dev/ttyS0
/dev/ttyACM2
/dev/ttyACM1
/dev/ttyACM0
####################################
/dev/ttyS0
/dev/ttyACM2
/dev/ttyACM1
/dev/ttyACM0
####################################
/dev/tty63 /dev/tty62 /dev/tty61 /dev/tty60 /dev/tty59 /dev/tty58 /dev/tty57 /dev/tty56 /dev/tty55 /dev/tty54 /dev/tty53 /dev/tty52 /dev/tty51 /dev/tty50 /dev/tty49 /dev/tty48 /dev/tty47 /dev/tty46 /dev/tty45 /dev/tty44 /dev/tty43 /dev/tty42 /dev/tty41 /dev/tty40 /dev/tty39 /dev/tty38 /dev/tty37 /dev/tty36 /dev/tty35 /dev/tty34 /dev/tty33 /dev/tty32 /dev/tty31 /dev/tty30 /dev/tty29 /dev/tty28 /dev/tty27 /dev/tty26 /dev/tty25 /dev/tty24 /dev/tty23 /dev/tty22 /dev/tty21 /dev/tty20 /dev/tty19 /dev/tty18 /dev/tty17 /dev/tty16 /dev/tty15 /dev/tty14 /dev/tty13 /dev/tty12 /dev/tty11 /dev/tty10 /dev/tty9 /dev/tty8 /dev/tty7 /dev/tty6 /dev/tty5 /dev/tty4 /dev/tty3 /dev/tty2 /dev/tty1 /dev/tty0
In pyserial it grabs the port in linux as shown in the following code. Tty[0-9]+ is not in the list.
devices = glob.glob('/dev/ttyS*') # built-in serial ports
devices.extend(glob.glob('/dev/ttyUSB*')) # usb-serial with own driver
devices.extend(glob.glob('/dev/ttyXRUSB*')) # xr-usb-serial port exar (DELL Edge 3001)
devices.extend(glob.glob('/dev/ttyACM*')) # usb-serial with CDC-ACM profile
devices.extend(glob.glob('/dev/ttyAMA*')) # ARM internal port (raspi)
devices.extend(glob.glob('/dev/rfcomm*')) # BT serial devices
devices.extend(glob.glob('/dev/ttyAP*')) # Advantech multi-port serial controllers
Indeed, this is not necessarily practical if you create virtual ports (with socat for example, which is my case) whose name does not respect the format of those in the list.
However, the list_ports.comports function doesn't find anything on my computer, so whatever the regex is, the list_ports.grep function doesn't return anything