Shop OBEX P1 Docs P2 Docs Learn Events
Here is the Python to Propeller Tutorial!! — Parallax Forums

Here is the Python to Propeller Tutorial!!

RavenkallenRavenkallen Posts: 1,057
edited 2011-01-04 17:25 in Propeller 1
Here it is. It took a while to figure out the best method to display the data. So i figured that almost everybody has a TV and a demo board. So here are some requirements of this demo...

You will need...

A PC:
Obviously. I have only tested this demo on Windows Vista. You will need to know how to find your COM port number. That can be found in the device manager and should be under COM ports...

Python 2.7:
You will need to have installed this and know how to use it some. You will be using the Python shell for entering commands, but you could save it as a program and run it later if desired. I have only tested this on this version of Python. It will probably work on the 3X series, but no guarantees.

Pyserial:
This can be found here. Install it using regular Windows procedures. You will want the 2x series exe.
http://pypi.python.org/pypi/pyserial.

Propeller demo board(Or equivalent):
This will be the main device for sending and receiving data and for displaying it on the TV.

TV:
For displaying values..

______________________________________________________________________________________________


Tutorial: For writing data to Propeller.

1: Connect your Propeller demo board to USB, power and a TV.

2: Find your COM port number.

3: Fire up the Python shell in the GUI. Test function by using a simple print command(e.g, print 'hello')

4: Load the program Python to Prop(write) onto your Propeller demo board. You should see a message displayed on the TV indicating a successful download.

5: Enter the following code into the Python shell...Substitute the COM port number(9) for your own number..

import serial
ser = serial.Serial('COM9')
ser.write("Hello")

6: You should see "Hello" displayed on your TV. That means it worked. You can write to the TV more if you want using the same ser.write("") command...after you are done, CLOSE THE COM PORT!! Use this command

ser.close()

If you don't close the COM port the port will lock out any other outside programs trying to access it. Including the Propeller serial terminal.


______________________________________________________________________________________________

Tutorial: Read from Propeller...

1: Follow all steps above except you must; swap the Propeller program for the other called Python to Prop(Read). Then type in the following code to the Python shell...

import serial
ser = serial.Serial('COM9')
ser.read(5)

You will then see the data displayed in the Python shell. Make sure to close the COM port again

______________________________________________________________________________________________

I am open for any comments/ suggestions. Need any help? Just give me a holler!

Comments

  • RonPRonP Posts: 384
    edited 2011-01-03 12:16
    Cool Thanks Ravenkallen

    I broke out my "Python Programming for the absolute beginner" that I never really did use. So many books so little time. Or maybe I just have a hard time focusing on one thing. Good thing I don't do this stuff for a living. Anyway I went through to Chapter 4 yesterday in anticipation for this tutorial, I should know just enough to get into trouble. I am going to try it in Ubuntu, If I can't get it going there I'll switch over to Vista. I'll report on my success or failure. Thanks again.

    Ron
  • atlstangatlstang Posts: 20
    edited 2011-01-03 12:25
    i dunno if it helps or anything, i had a program i wrote long time ago to dl accelerometer data logged to an sd card that was used a storage. ya can set baudrate and timeout for it to fail when initializing the port, heres what i used to initialize it. way i understood it, the read(5) where the 5 was the number of bytes in buffer. im not the best coder but it worked so i was happy.

    import serial, sys, time, string
    
    port = "COM5"
    baudrate = 115200
    
    try:                                                        #try to open a serial port
        propcom = serial.Serial(port, baudrate, timeout=.05)    #open serial port with timeout .05s
        print port + ' opened' 
    except:                                                     #could not open serial display to screen and quit
        print 'Could not open serial port: ' + port
        sys.exit()
    
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-01-03 14:55
    OK is working
    though I think retyping all these commands line by line is not very comfortable
    how about posting a script that receives strings and shows the received strings in an extra window?
    next step would be how to save the received data into a textfile transformin each CR-LF into a comma to create a CSV-file

    best regards

    Stefan
  • RavenkallenRavenkallen Posts: 1,057
    edited 2011-01-03 15:35
    @altstang.. Yeah, it was a 5 byte buffer. It was just big enough to read in the word hello once..

    @Stefanl38 ... Yes, that is the next logical step. I just wanted for people to get the feel of the whole thing before they went any further. It is a basic demo. If you want to modify any of my code, you are certainly welcome...

    @RonP... I am still somewhat of a newbie at Python to. It does seem to be a easy to learn language however.
  • RonPRonP Posts: 384
    edited 2011-01-03 23:29
    Well it works just fine had some trouble with the port, just my lack of knowledge and understanding. But I did learn a lot doing this little exercise, for me the learning is all fun. I used Ubuntu 10.04, Python 2.6.5 (IDLE).

    For Linux it went like this for me:
    import serial
    ser = serial.Serial('/dev/ttyUSB0')
    ser.write("Hello")
    
    I used scanlinux.py to find the port.



    I also found out by mistake that this monitor hooked up to my laptop has PIP. My TV here is about 20 feet away from my desk and I knew this monitor had TV inputs and every other input under the sun. So I thought well I can get by with just the laptop screen for this little demo, one gets spoiled with two monitors. Next thing I know pressing buttons on side to switch and presto PIP. This is also the first time hooking the demo board to a TV pretty neat. Think I'll play with the TV debug thing instead of the PST.

    @Ravenkallen Thanks again. I find Python to be a like SPIN also. But I don't know much about other languages. I think I'll pursue learning Python as a PC programing language it should work for anything I need to do.

    EDIT:
    Later I found out I couldn't read I got this '\x00\x00\x00\xf8H' which means nothing to me. Found out I needed a timeout so this fixed it.

    ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
    ser.read(5)
    'Hello'
    1024 x 765 - 44K
    PTP.jpg 43.6K
  • RavenkallenRavenkallen Posts: 1,057
    edited 2011-01-04 12:19
    @Ronp... I am glad it worked for you. I was very happy when i finally got it to work also. It enables me to have large programs running on a PC and have a Prop feed it sensor data..
  • LevLev Posts: 182
    edited 2011-01-04 17:25
    @Ravenkallen:

    Thanks for the tutorial! I plan on trying it out this weekend.
Sign In or Register to comment.