Wednesday, 22 October 2014

Python Find Serial Ports - Windows and Raspberry Pi

I started with code taken from stack overflow at the following page: http://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python with the code posted by user "Thomas" (code has since been modified). I tested the code on Windows 7 and a Raspberry Pi. The original 'unix' code did not work for me, so I found a method that did.
import sys
import glob
import serial
def ScanPorts():
    """
    Returns a generator for all available serial ports
    """
    if os.name == 'nt':
        # windows
        for i in range(256):
            try:
                s = serial.Serial(i)
                s.close()
                yield 'COM' + str(i + 1)
            except serial.SerialException:
                pass
    else:
        # unix
        ports = glob.glob('/dev/tty[A-Za-z]*')
       
        for port in ports:
            yield port
To run this on the Raspberry Pi, first the python serial package must be installed by running:
sudo apt-get install python-serial
Also the software package "Device Monitoring Studio" found online here: http://freeserialanalyzer.com/ was useful for peeking into what messages were being sent and received from another program. The free version has a 22 minute time limit.

"yarn" in R

Whilst trying to follow the PCA tutorial from the following page http://nir-quimiometria.blogspot.de/2012/02/pca-for-nir-spectrapart-001-plotting.html I couldn't find the "yarn" dataset, and couldn't find any information about where it was from, whether it was bundled with the R source etc. Trying to evaluate yarn simply gave the following:
Error: object 'yarn' not found
After some searching I stumbled across the documentation for the pls library. It turns out the data is bundled with the pls library. Running the following allowed me to use the dataset:
install.packages("pls")
library("pls")
Now on with the rest of the tutorial!