I thought I would write a small Python 2.7 program to read the serial output from the ChipKit Pi. This would be the equivalent of using the serial monitor in the Mpide. Why would you want to do this? Mainly to use Python to graphically display data from the shield attached to your ChipKit Pi. Plus its alway fun to try something on your new ChipKit Pi. So here it goes:
First using the 'Quick Start guide for ChipKit Pi' from Newark. Download the 'chipkit.zip' software. Warning! Its a big file 2.1gb and for some reason it was very slow downloading, almost 12 hours. Follow the quickstart guide and get to the point of being able to program the ChipKit Pi. Dont forget to follow the instructions on uploading to the ChipKit Pi. I'll call the upload instructions the 'Upload pushbutton sequence'.
I installed a piece of Black Electrical tape on top of the USB port connector. Looks to me like you could accidentally cause the board to touch the USB connector while doing the upload pushbutton sequence.
If you open the Mpide, click 'File', then 'Sketchbook'. You will find a sketch called 'chipKITPi_SerialTest'. You can use this to test your ChipKit Pi. Upload the file, remember to use the pushbutton sequence from the PDF. At the bottom of the Mpide you should see 'Uploading to I/O Board...'. Takes a minute be patient. It will give you timeout errors if you forgot to do the upload pushbutton sequence. When its done it will display 'Done Uploading'.
At this point click on the 'serial monitor'. If everything worked you should see 'Hello Raspberry Pi!'.
Now that things are working I changed the baud rate to 38400 in the sketch from the 9600. Example: Serial1.begin(38400)
Then I changed 'Hello Rasberry Pi!' to 'Hello Python! Greetings from ChipKit Pi!'. Example: Serial1.println("Hello Python! Greeting from ChipKit Pi!!"); Go ahead and upload the sketch to your ChipKit Pi. You can test this with the serial monitor, just change the baud rate to 38400 in the serial monitor.
Now double click 'Idle' on your desktop of the Raspberry Pi. When it comes up you should see the title bar display 'Python Shell'. So click 'File', then 'New Window". That should bring up a new window with the title bar displaying 'Untitled'. Now lets type in the program to read the serial port and get the data from the ChipKit Pi.
Type in the following:
#!/usr/bin/python2.7
#must load a serial library
import serial
#setup the serial port to get data from ChipKit Pi.
#Where did /dev/ttyS0 come from?
#In Mpide, 'Tools' the 'serial port'
ser = serial.Serial('/dev/ttyS0', 38400)
#create a loop
while 1:
#read a line of data from ChipKit Pi
x=ser.readline()
#print the data to the window
print x
Be sure you typed it in exactly as displayed above. You dont have to type the lines(Green color) with a # sign, these are comments. BUT you MUST type the first line beginning with #.
I tried to make the program with lots of comments so you know how it works.
Now lets run the program. Save the program, click 'Run', then 'Run Module'. Wait! before you do, make sure that serial monitor it not running.
Oh great! You probably got some nasty error from Python.
pi@raspberrypi /dev $ python ext.py
Traceback (most recent call last):
File "ext.py", line 1, in <module>
import serial
ImportError: No module named serial
Well pyserial is not installed in the distro. So let install it now. Make sure RPi has an Internet connection, then double click LXterminal on the desktop and type the following: sudo apt-get install python-serial
Now run the program again.
Well, I hope everything ran fine. If not ask some questions.
Jerry