1+ import serial
2+ import sys
3+
4+ #A serial port-scanner for linux and windows platforms
5+
6+ #Author: Julio César Echeverri Marulanda
7+ 8+ #blog: blogdelingeniero1.wordpress.com
9+
10+ #You should have installed the PySerial module to use this method.
11+
12+ #You can install pyserial with the following line: pip install pyserial
13+
14+
15+ def ListAvailablePorts ():
16+ #This function return a list containing the string names for Virtual Serial Ports
17+ #availables in the computer (this function works only for Windows & Linux Platforms but you can extend it)
18+ #if there isn't available ports, returns an empty List
19+ AvailablePorts = []
20+ platform = sys .platform
21+ if platform == 'win32' :
22+ for i in range (255 ):
23+ try :
24+ ser = serial .Serial (i ,9600 )
25+ except serial .serialutil .SerialException :
26+ pass
27+ else :
28+ AvailablePorts .append (ser .portstr )
29+ ser .close ()
30+
31+ elif platform == 'linux' :
32+ for i in range (0 ,255 ):
33+ try :
34+ ser = serial .Serial ('/dev/ttyUSB' + str (i ))
35+ except serial .serialutil .SerialException :
36+ pass
37+ else :
38+ AvailablePorts .append ('/dev/ttyUSB' + str (i ))
39+ ser .close ()
40+ else :
41+ print '''This method was developed only for linux and windows
42+ the current platform isn't recognised'''
43+ return AvailablePorts
44+
45+
46+ # EXAMPLE OF HOW IT WORKS
47+
48+ #if an Arduino is connected to the computer, the port will be show in the terminal
49+ #print ListAvailablePorts()
0 commit comments