MAS 863: How to Make (Almost) Anything

Run Python code to read out serial output from Mac OX Lion, Windows 7 and Ubuntu


<Under Mac Bash Terminal>


1. Python should be installed by default

Open bash terminal, type in :
Python
if it shows lines such as:
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

It means the mac has in built Python.


2. Install Serial and Tkinter

To check if your mac has python serial built in, type in "Python" to switch to python shell, then type in:
>>>> import serial
If serial is installed, there won't be any remind message. Otherwise, install the serial by typing in:
sudo easy_install pyserial

3. Run the Python code

 

From the folder where term.py is located, try the command
python filename.py /dev/tty.usb* SPEED
for example, if the baud rate is 9600, the you could type in "python filename.py /dev/tty.usb* 9600"; notice that in Neil's examples for different sensors, some take in only the USB serial port but not the baud rate (baut rate is pre-defined inside the file), in that case, you could just do "python filename.py /dev.usb*"; Also check the baud rate, some file has 9600, others have 115200;

Explanations:
Since filename.py is a python script, you need to call it with the python interpreter.
It takes in two argument - the first is the port for the usb-serial interface (so the * uses the shell's matching to select anything of the form /dev/tty.usbSOMECRYPTICNUMBER)

the second argument is the baud rate (I think Neil's normal code runs at 115200 baud).

4. Choices to read out serials

1) rx.py
Neil wrote a general file to read out the raw data of the serial's print out both in decimal and hex, it can be used as a general serial reader. download here

1) term.py
Print out character in a GUI window.

2) screen (suggested by Moriz)
screen is a command line serial terminal that comes with mac os. try running it from the terminal:
screen [serialport: e.g. /dev/tty.usb-… ] [baud-rate]
to get out of screen, press Control-A, and then K, and then say yes when it asks if you want to kill the session (if you just close the window, it remains connected in the background and makes it hard to reconnect).

3) CoolTerm
if you like to see the raw serial communication as well as ASCI and prefer a GUI over command line this may be of interest.


<Under Cygwin Terminal in Windows 7>

Cygwin is a collection of tools which gives a linux look under windows system. Once you install it, you could open the Cygwin terminal through Start menu; If you navigate to Cygwin folder, you will see the home folder; The file path in the Cygwin terminal starts from this home folder.

The following tutorials are based on the class project documentation of Chris.

1. Install Cygwin with Python package.

Download and install Cygwin. You must make sure that the Python packages are included in your installation of Cygwin, including Tkinter (you can do this by reinstalling Cygwin and enabling the packages)

2. Install Pyserial

To get the serial module, you'll have to download the source file here, unzip it under the home directory of Cygwin;
Under Cygwin terminal, change directory to the unzipped file, and enter:
python serial.py install
(this should work if and only if you have python installed in Cygwin)

3. How Cygwin interprets Serail Port

/dev/ttyUSB0 does not work for running Neil's code, since this is a Linux location for serial; instead, go to your Device Manager and expand "Ports", and note the USB Serial port (should be COM6 or COM7 or something); The translation in Cygwin for directing towards COM? is /dev/ttySx, where x is the number of the COM port, starting with 0 (COM1 is S0, COM2 is S1, etc.)

So, under Cygwin, to run the python serial reading file, if the USB is under COM4, you should do:
python filename.py /dev/tty S3

<Under cmd Terminal in Windows 7>

1. Download and install Python 2.7.2 through the windows installer
2. Download and install pyserial from the exe file

Test out if Serial and Tkinter are installed correctly by open python.exe through cmd terminal window :
c:/Documents/Python/Python2.7.2.exe
import Serial
import Tkinter

3. cmd in Windows interprets the USB port as simple "COM"

So to run the python serail code, just type in:
C:\Python27\python.exe C:\Users\liningy\Desktop\filename.py COM4

<Under Unbuntu>

This Introduction is from Emma, the only one I never tested out.
1). Python is already installed with Ubuntu, but in order to get the Tkinter graphics module to work you need to enter this line of code into terminal:
sudo apt-get install python-tk

2). Once Tkinter is installed, then you have to check to see which of your serial ports is connected to your board. Type this line of code into terminal to see which serial port is connected:
dmesg | grep tty

3). Now that you know the number, type in this line of code to run your program:
python hello.load.45.py /dev/ttyUSB0


< Back to home