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 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