Bidirectional serial communication between Python program and Propeller Micro
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
[update] You can get rid of it like this: print(line.decode('UTF-8'))
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
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.