element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Use Python to read Serial Output from ChipKit Pi
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jlchapman
  • Date Created: 12 Oct 2013 5:07 PM Date Created
  • Views 329 views
  • Likes 2 likes
  • Comments 4 comments
Related
Recommended
  • raspberry_pi
  • raspeberry_pi_accessories

Use Python to read Serial Output from ChipKit Pi

jlchapman
jlchapman
12 Oct 2013

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

  • Sign in to reply
Parents
  • lbittner
    lbittner over 9 years ago

    Thanks Howard and Jerry for your questions and comments. If you have questions about your chipKIT Pi, don't miss our chipKIT Pi expert event with Tom Devine. Tom, an engineer by day and electronics teacher by night, is here to answer your questions about the chipKIT through November 1:Want Help with Your chipKITTm Pi? Get AnswersTom Devine Through Nov. 8th!

     

    Lauren

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • lbittner
    lbittner over 9 years ago

    Thanks Howard and Jerry for your questions and comments. If you have questions about your chipKIT Pi, don't miss our chipKIT Pi expert event with Tom Devine. Tom, an engineer by day and electronics teacher by night, is here to answer your questions about the chipKIT through November 1:Want Help with Your chipKITTm Pi? Get AnswersTom Devine Through Nov. 8th!

     

    Lauren

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube