Created
December 16, 2015 16:27
-
-
Save kitusmark/d1697b4cbc8296c1bee4 to your computer and use it in GitHub Desktop.
Python program to list all the serial ports connected to the computer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import serial | |
import sys | |
import time | |
BAUDRATE = 115200 | |
TIMEOUT = 0 | |
# Create list of potential ports | |
if sys.platform.startswith('win'): | |
ports = ['COM' + str(i + 1) for i in range(256)] | |
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): | |
ports = glob.glob('/dev/tty[A-Za-z]*') | |
else: | |
raise EnvironmentError('Unsupported platform') | |
port_exceptions = ['dev/ttyprintk'] | |
for port in port_exceptions: | |
if port in ports: | |
ports.remove(port) | |
def serial_scanner(): | |
global ports | |
while True: | |
for port in ports: | |
try: | |
s = serial.Serial(port, BAUDRATE, timeout = TIMEOUT, rtscts = 0) | |
print('Connected to %s'%port) | |
except (OSError, serial.SerialException): | |
pass | |
time.sleep(1) | |
if __name__ == '__main__': | |
serial_scanner() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment