Reading Serial Data in a Python Script

I’ve recently had the idea(and I’m not the first one) to read in the position of a tag node by directly connecting through the serial port; specifically using a python script. I’ve been able to do so using programs like putty and teraterm but have been unable to do so in Python. The difficulty is that in order to access the data on the tag node you first must hit enter twice. This is difficult in python because you have to encode the “enter” key with a ASCII equivalent. I’ve tried to do so by using the ASCII equivalent for enter, “0x0D”, but have been unsuccseful. My code looks like this.

import serial
import time

ser = serial.Serial(‘COM7’, 115200, timeout=0, parity = serial.PARITY_EVEN, rtscts=1)

apg = “apg”
apg = apg.encode(‘utf-8’)

if ser.isOpen():
print()
print(ser.name + ’ Is open’)
time.sleep(.1)

while True:
x = ser.readline(100)
x = x.decode(‘utf-8’)
print(x)
time.sleep(1)

ser.close()

Hello,
you forgot to write to UART, where is your write data to set the UART to SHELL mode and ser.write(apg) function?

#!/usr/bin/python

-- coding: utf-8 --

import serial, time

class DecawaveCmds:
shell_mode = b’\x0D\x0D’
apg = b’apg\n’

ser = serial.Serial(“COM10”, 115200, timeout=0.1)

ser.write(DecawaveCmds.shell_mode)
time.sleep(1)

st = ser.read(300) # len 225
data = st
print ("DATA: ", data)
print (“LEN: “, len(data))
print (”------------------------\n”)

ser.write(DecawaveCmds.apg)

st = ser.read(50)
print ("Data: ", st)
print ("LEN: ", len(st))

If you want it perriodically, add some delay and loop when sending Cmds.apg
Have a nice day.
Reagrds Pavol