I am running a python script that uses pyserial package. I use a board to control a motor rotation and is connected via USB port.
The board is already programmed and can rotate a motor with given command. Here are some example:
- Input: Command to check status of the motor : H010100
output :
{
.."beambreak": [0,0,0,0,0],
.."prox": [0.003,0.003],
.."T1": [0,0],
.."T2": [0,0],
.."MT": [-1,-1]
.."test_switch": 0,
.}
- Input : Command to rotate motor once : H010101
Output: {"Rotate":"Successful"}
Task : In a ' while' loop , How to send command (e.g. H010101) every 1 min, check the output message (e.g. {"Rotate":"Successful"} ) and send next command based on output message condition.
Question: When I run the code , the "can set" output message appears in the linux terminal/IDE console but I don't know out to save the message as a variable and apply it in a loop condition. I mean, to check the message if it is the same message wait for 1 min and send H010101 command again?
I have also tried to save file at *.log or *.txt but did not work example:
$ python test.py >> *.txt
$ python test.py > *.log
Here is my code:
import time
import serial
# configure the serial connections
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
while True :
print(' Check Status')
ser.write('H010000\n'.encode())
status_cmd = ser.readline()
print(status_cmd)
if status_cmd === "test_switch: 0 " : # i can't save the message as variable from running terminal
time.sleep(5)
# Command to rotate motor
ser.write('H010101\n'.encode())
# read respond of give command
reading = ser.readline()
print(reading)
if reading == {"Drop":"Successful"} : # i can't save the message as variable from running terminal
time.sleep(60)
# rotate motor
ser.write('H010101\n'.encode())
# read respond of give command
reading = ser.readline()
print(reading)