Shop OBEX P1 Docs P2 Docs Learn Events
Bidirectional serial communication between Python program and Propeller Micro — Parallax Forums

Bidirectional serial communication between Python program and Propeller Micro

bomberbomber Posts: 297
edited 2013-10-07 11:02 in Propeller 1
I am working on establishing a reliable way of transmitting and receiving data between a Propeller microcontroller and a python program running on my computer. I have got a basic program working, but have ran into a few snags on both ends that someone here probably has an answer to the problems. My python program is here:
import serial #Serial Port communication
import time   #timing methods
BAUD = 115200  #BAUD rate of serial communication
COM_DEFAULT = 7 #default com port
Failed = False # variable storing boolean condition on weather or not the com port connection failed
line = '' #variable storing the string output from propeller
Com = 'COM' + input('Enter Com Port #:') #get input from the user on the com port to connect to.
try:          #attempt to establish serial connection
    ser = serial.Serial(Com, BAUD, timeout=0.9,parity=serial.PARITY_EVEN, rtscts=1)
    print('Started Communications on: '+ser.portstr)
except serial.serialutil.SerialException: #if it failed
    print('Could Not open Serial Port, exiting now') #print error message
    Failed = True #set fail to true
    
while Failed == True: #repeat infinitley if no problems were detected
    oldLine = line    #make a variable for checking if line changed
    line = ser.read(100).strip() #read 100 bytes of data, strip out any whitespaces, and write the variable line to the output
    
    if not line == oldLine: # if the line read is different than the last line recieved, 
       print(line) #print the line to the terminal
    time.sleep(0.01) # pause for 10 ms
    
if Failed == False:    #if the serial port is open,
    ser.close()        #close it
  

The code running on my propeller is here:
Con
_clkmode      = xtal1 + pll16x
_clkfreq = 80_000_000 
PST_BAUD        = 115200
OBJ
time   : "Timing"                                     ' Timing convenience methods             ' WAV file playback system
pst    : "Parallax Serial Terminal"
probe  : "MonitorPWM"               'An object to aid in the reading of the Polar Haertrate Reciever
var
long hrate, hrateOld
long tHprobe, tlprobe, pulseCnt
long recieveStack[3000]  
PUB Main | count
pst.start(PST_BAUD)
probe.start(0, @tHprobe, @tLprobe, @pulseCnt)
hrate := 0
cognew(recieve, @recieveStack) 
repeat '        "                              "
 hrateOld := hrate
 Heart
 if not hrate == hrateOld
  pst.str(string("Heartrate = "))
   pst.dec(hrate)
  time.pause(1000)

pri Heart | dum1
dum1:= (tHprobe+ tlprobe)/1000 
hrate:= 4800000/dum1

pub recieve | count
dira[1] := 1
repeat
 count := pst.decin
 repeat count
  time.pause(100)
  dira[1] := 1
  time.pause(100)
  dira[1] := 0
a synopsis of what happens is that I run both programs, the prop sends the heartrate from an attached Polar Heartrate Receiver (pin 1) to the Python program, and the python program will send a numerical value back that is the number of times the prop will blink the LED attached to pin 1 (Not Yet Implemented). For example, the prop will send "Heartrate = 150", but the value printed to the Python terminal is "b'Heartrate = 150' ". I tried using regular expressions to remove the extra characters, but that did not work. I am also having issues sending values to the Propeller. Are there any subroutines in the "Parallax Serial Terminal" or Parallax Serial Terminal Plus" that allow you to read the buffer of received messages? Any help/comments/criticism would be appreciated!

Comments

  • rosco_pcrosco_pc Posts: 464
    edited 2013-10-06 21:23
    bomber wrote: »
    For example, the prop will send "Heartrate = 150", but the value printed to the Python terminal is "b'Heartrate = 150' ". I tried using regular expressions to remove the extra characters, but that did not work.
    the b' are NOT extra characters received from the propeller, but come from the python interpreter and indicate that the string is a byte string (with vakues garantued to be between 0 .. 255). See here for an explanation: http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal

    [update] You can get rid of it like this: print(line.decode('UTF-8'))
    bomber wrote: »
    I am also having issues sending values to the Propeller. Are there any subroutines in the "Parallax Serial Terminal" or Parallax Serial Terminal Plus" that allow you to read the buffer of received messages? Any help/comments/criticism would be appreciated!
    I never used PST so I don't know if ti is possible to receive data, But instead you might use FullDuplexSerial, where you can use check for data received with .rx and .rxcheck commands
  • Mike GreenMike Green Posts: 23,101
    edited 2013-10-06 21:25
    The Parallex Serial Terminal object is a "wrapper" around the Full Duplex Serial object that adds terminal control characters for interaction with a specific terminal program on the PC. If you're not using that program (as in your case), the extra characters may throw things off. Use FullDuplexSerialPlus instead. You have to supply additional parameters to the .start method that include the I/O pins you're going to use (30 & 31 in your case), a mode value (use %0000), and the Baud value. You'll need to send whatever delimiter you need (like with .tx(13) for a carriage return or .tx(10) for a line feed).
  • pedwardpedward Posts: 1,642
    edited 2013-10-07 11:02
    Mike, that is incorrect. PST is based on FDS, but does not use FDS.

    Something that is important is that pst.decin() requires an NL character, defined as 13 (which itself isn't a newline, but rather CR or carriage return, newline is actually 10) to terminate the input.

    In python you'd use \r as the escape for the NL character, contrary to the standard \n.
Sign In or Register to comment.