Skip to content

Instantly share code, notes, and snippets.

@0XDE57
Last active July 12, 2025 07:36
Show Gist options
  • Save 0XDE57/733310dc2c1bf780541103cb159d1e03 to your computer and use it in GitHub Desktop.
Save 0XDE57/733310dc2c1bf780541103cb159d1e03 to your computer and use it in GitHub Desktop.
simple script to list all serial ports. if port is populated, device info is printed
import os
import serial.tools.list_ports as list_ports
import stat
from pwd import getpwuid
def list_all_ports():
ports = list_ports.comports()
for port in ports:
print(f"Device: {port.device:<15} Path: {port.device_path}")
if (
port.vid != None
or port.pid != None
or port.hwid != "n/a"
or port.serial_number != None
or port.location != None
):
check_permissions(port.device)
print(
f"\tLocation: {port.location}\n\tManufacturer: {port.manufacturer}\n\tDescription: {port.description}\n\tVID: 0x{port.vid:04X}\n\tPID: 0x{port.pid:04X}\n\tHWID: {port.hwid}\n\tSerial: {port.serial_number}"
)
def check_permissions(file_path):
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
mode = os.stat(file_path).st_mode
permissions = stat.filemode(mode)
st_uid = os.stat(file_path).st_uid
userinfo = getpwuid(st_uid)
username = userinfo.pw_name
# print(f"File: {file_path}")
print(f"\tPermissions: {permissions}\tOwner: {username}")
list_all_ports()
@0XDE57
Copy link
Author

0XDE57 commented Jul 12, 2025

and some helpful serial commands

list usb devices (verbose):
lsusb -v

check udev:
udevadm monitor -p

list devices:
ls -l /dev | grep tty

check dmesg:
sudo dmesg | grep tty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment