Reputation: 19
Currently I am doing project where I want to use a Raspberry pi 3 as a monitoring device for motors. The Raspberry pi itself communicates with Roboteq controller using USB connection. Below is the simple code I'm trying to send via USB to the motor controller. I want to get motor Amps and show values in the terminal:
import time
import serial
def init_serial():
global ser
ser = serial.Serial(
port = '/dev/ttyACM0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
if ser.isOpen():
print("Connected to: " + ser.portstr)
def motor_ampers():
motorChannel = 1
command = '?A '+str(motorChannel)+' \r'
ser.write(command.encode())
data = ser.readline().decode().strip()
print(data)
value = 0
if data.startswith('A='):
value = int(data.split('=')[1])
print(value)
init_serial()
motor_ampers()
A little explanation to my code. By sending ?A 1
command to roboteq controller I can get first motor ampers. As an output I'm getting ?A 1 ▫A=-2
where -2 is motor ampers. After I'm trying to extract motor ampers from string and store it as integer. However I'm always getting value = 0
even if motors are running. I can't figure out mistake by myself so any help is greatly appreciated.
Upvotes: 1
Views: 228
Reputation: 3496
Your problem is the data you are getting back on the port starts with a non-ASCII character so this line:
if data.startswith('A='):
will never be tested as true.
Try replacing it with:
if 'A=' in data:
Upvotes: 0