Last active
December 2, 2015 19:28
-
-
Save victorvaz/d209637b36cb1185b4e1 to your computer and use it in GitHub Desktop.
Classe para listar as portas seriais em uso
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 serial | |
import sys | |
class Portas : | |
""" | |
Classe com métodos para portas | |
""" | |
MAX_PORTAS = 32 # Número máximo de portas para verificação. | |
def getPortasEmUso () : | |
""" | |
Método para buscar as portas em uso | |
""" | |
# Faz um loop de 1 ao número maximo de portas para verificação e o concatena à string 'COM': | |
for p in ['COM%s' % (i + 1) for i in range(Portas.MAX_PORTAS)] : | |
try : | |
# Tenta abrir a conexão: | |
s = serial.Serial(p) | |
# Tenta fechar a conexão | |
s.close() | |
# Retorna um por um | |
yield s.name | |
except (OSError, serial.SerialException) : | |
# Caso der erro, a porta não está sendo utilizada. Ignoramos neste caso. | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment