Created
July 5, 2018 00:50
-
-
Save fvilante/8b501754f7af67673eec691147605036 to your computer and use it in GitHub Desktop.
Lista_Serial.py
This file contains hidden or 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 sys | |
import glob | |
import serial | |
def serial_ports(): | |
""" Lists serial port names | |
:raises EnvironmentError: | |
On unsupported or unknown platforms | |
:returns: | |
A list of the serial ports available on the system | |
""" | |
if sys.platform.startswith('win'): | |
ports = ['COM%s' % (i + 1) for i in range(256)] | |
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): | |
# this excludes your current terminal "/dev/tty" | |
ports = glob.glob('/dev/tty[A-Za-z]*') | |
elif sys.platform.startswith('darwin'): | |
ports = glob.glob('/dev/tty.*') | |
else: | |
raise EnvironmentError('Unsupported platform') | |
result = [] | |
for port in ports: | |
try: | |
s = serial.Serial(port) | |
s.close() | |
result.append(port) | |
except (OSError, serial.SerialException): | |
pass | |
return result | |
if __name__ == '__main__': | |
print(serial_ports()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testado em windows7 com pyton 3.7, emulando porta serial com com0com_signed.
Funcionou.