From 6d6c466d8db71579cd8186980a3ae04c5b55b5ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar=20Echeverri=20Marulanda?= Date: Sun, 24 Jan 2016 17:11:29 -0500 Subject: [PATCH 1/2] A script that shows all the serial devices [port-names] conneted to the serial ports of the computer --- serial_scanner.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 serial_scanner.py diff --git a/serial_scanner.py b/serial_scanner.py new file mode 100644 index 00000000000..21fa023e0fb --- /dev/null +++ b/serial_scanner.py @@ -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() \ No newline at end of file From 8161c242b5ecdf53267c421e5e9150cab06cadb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar=20Echeverri=20Marulanda?= Date: Sun, 24 Jan 2016 17:16:46 -0500 Subject: [PATCH 2/2] added serial_scanner description --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a2a2c15cdfb..f9d7d430517 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file