Skip to content

Instantly share code, notes, and snippets.

@EiichiroIto
Created September 27, 2022 03:11
Show Gist options
  • Save EiichiroIto/ab2f5aad46b33610d357e48b940085a0 to your computer and use it in GitHub Desktop.
Save EiichiroIto/ab2f5aad46b33610d357e48b940085a0 to your computer and use it in GitHub Desktop.
Sample program to find the serial port
import sys
from serial.tools import list_ports
def auto_select_port_name(port_name):
if port_name:
return port_name
port_names = [port.device for port in list_ports.comports()]
port_names.sort()
if len(port_names) == 0:
return None
elif len(port_names) == 1:
return port_names[0]
print("Available ports:")
for i in range(len(port_names)):
print(" %d --> %s" % (i,port_names[i]))
print("enter number >> ", end="")
num = int(input())
return port_names[num]
port_name = auto_select_port_name(sys.argv[1] if len(sys.argv) >= 2 else None)
print(port_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment