0

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:

  1. 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,
.}
  1. 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)

1 Answer 1

1

The first thing you can do is to encapsulate your functions into methods ( it is also possible to use a class if you want )

Check Status

def check_status(ser):
    print('Check Status')
    ser.write('H010000\n'.encode())
    current_status = ser.readline().decode('utf-8')
    return current_status

Rotate Motor

def rotate_motor(ser):
    print('Rotating ..')
    ser.write('H010101\n'.encode())
    rotation_status = ser.readline().decode('utf-8')
    return rotation_status

You also need to import json to load the responses as dict

e.g

>>> import json

>>> rotation_status='{"Drop":"Successful"}'
>>> json.loads(rotation_status)
{'Drop': 'Successful'}

Now that you have these pieces of code ready, you can invoke them to continuously run actions depending on the result

while True:
    status = json.loads(check_status(ser))
    if status['test_switch'] == 0:
        time.sleep(5)
    
    is_rotated = json.loads(rotate_motor(ser))
    if is_rotated['Drop'] == 'Successful':
        time.sleep(60)
    else:
        print('Something went wrong')
        raise Exception(is_rotated)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .