Skip to content

Added Script serial_scanner.py #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ In the scripts the comments etc are lined up correctly when they are viewed in [
- `script_listing.py` - This will list all the files in the given directory, it will also go through all the subdirectories as well.

- `testlines.py` - This very simple script open a file and prints out 100 lines of whatever is set for the line variable.

- `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.
49 changes: 49 additions & 0 deletions serial_scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import serial
import sys

#A serial port-scanner for linux and windows platforms

#Author: Julio César Echeverri Marulanda
#e-mail: julio.em7@gmail.com
#blog: blogdelingeniero1.wordpress.com

#You should have installed the PySerial module to use this method.

#You can install pyserial with the following line: pip install pyserial


def ListAvailablePorts():
#This function return a list containing the string names for Virtual Serial Ports
#availables in the computer (this function works only for Windows & Linux Platforms but you can extend it)
#if there isn't available ports, returns an empty List
AvailablePorts = []
platform = sys.platform
if platform == 'win32':
for i in range(255):
try:
ser = serial.Serial(i,9600)
except serial.serialutil.SerialException:
pass
else:
AvailablePorts.append(ser.portstr)
ser.close()

elif platform == 'linux':
for i in range(0,255):
try:
ser = serial.Serial('/dev/ttyUSB'+str(i))
except serial.serialutil.SerialException:
pass
else:
AvailablePorts.append('/dev/ttyUSB'+str(i))
ser.close()
else:
print '''This method was developed only for linux and windows
the current platform isn't recognised'''
return AvailablePorts


# EXAMPLE OF HOW IT WORKS

#if an Arduino is connected to the computer, the port will be show in the terminal
#print ListAvailablePorts()