Skip to content

Instantly share code, notes, and snippets.

@dvsseed
Created October 3, 2017 12:54
Show Gist options
  • Save dvsseed/1528eba7a6c4c298c968940d2e952201 to your computer and use it in GitHub Desktop.
Save dvsseed/1528eba7a6c4c298c968940d2e952201 to your computer and use it in GitHub Desktop.
To detect the serial ports on windows 10 / MacOS Sierra laptop
#coding:utf-8
"""
Written on a Windows 10 / MacOS Sierra Laptop, Python 2.7.14 Version.
This program automatically detects and lists ports.
Written by Davis, Tseng 10/03/2017.
"""
# import serial
import serial.tools.list_ports
# Find which os
import os
import glob
def common():
# Find Live Ports
ports = list(serial.tools.list_ports.comports())
return ports
def findNT():
ports = common()
# If not found on serial
if not ports:
print u"連接埠(COM) not found!"
# If found
for p in ports:
print p # This causes each port's information to be printed out.
def findMac():
ports = common()
for p in ports:
if str(p).find('usb') != -1:
print p
if os.name == "nt":
findNT()
elif os.name == "posix":
findMac()
else:
print "OS not found!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment