Skip to content

Commit 6108afd

Browse files
committed
Merge pull request geekcomputers#29 from LoadCode/master
Added Script serial_scanner.py
2 parents 0c7b532 + 8161c24 commit 6108afd

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ In the scripts the comments etc are lined up correctly when they are viewed in [
3535
- `script_listing.py` - This will list all the files in the given directory, it will also go through all the subdirectories as well.
3636

3737
- `testlines.py` - This very simple script open a file and prints out 100 lines of whatever is set for the line variable.
38+
39+
- `serial_scanner.py` contains a method called ListAvailablePorts which returns a list with the names of the serial ports that are in use in our computer, this method works only on Linux and Windows (can be extended for mac osx). If no port is found, an empty list is returned.

serial_scanner.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)