Python Forum

Full Version: Repeat request by else
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good afternoon, there is a code that removes parameters, the request is made 2 times and if the answers match, the data is written to the database. I did not have any problems with this, if the data is not equal - it is printed (the data is not equal) and the program ends. Tell me, how to send the request again under the else condition?
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
cmd = bytearray([0x1])
ser.write(cmd)
reply = ser.read(22)
out = reply[11:16]
print(out)
time.sleep(0)
cmd = bytearray([0x1])
ser.write(cmd)
reply = ser.read(22)
out2 = reply[11:16]
print(out2)
if out == out2:
	cursor = connection.cursor()
	sql = "Update silos1 set weight = %s, date = %s where id = %s " 
	datetime.strptime("12/31/2000", "%m/%d/%Y")
	newHireDate = datetime.today()
    rowCount = cursor.execute(sql, (out, newHireDate, 1 ) )
	connection.commit() 
	print ("ok", newHireDate)
else: 
	print ("data is not correct")
    connection.close()
	
You need a loop to repeat sending the request until the responses match. I do not know enough about your process to know what code belongs inside the loop and what needs to be done if there out and out2 don't match. Here's a guess.
# missing inports

def write_command(ser, cmd):
    """Avoid duplicate code"""
    ser.write(cmd)
    reply = ser.read(22)
    out = reply[11:16]
    print(out)
    return out

ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate=9600,
        timeout=1,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS)

cmd = bytearray([0x1])
while True:
    out = write_command(ser, cmd)
    time.sleep(0)
    out2 = write_command(ser, cmd)

    if out == out2:
        cursor = connection.cursor()
        sql = "Update silos1 set weight = %s, date = %s where id = %s " 
        datetime.strptime("12/31/2000", "%m/%d/%Y")
        newHireDate = datetime.today()
        rowCount = cursor.execute(sql, (out, newHireDate, 1 ) )
        connection.commit() 
        print ("ok", newHireDate)
        break

    print ("data is not correct")
    connection.close()  # Do you want to do this?
(Jul-28-2022, 05:49 PM)deanhystad Wrote: [ -> ]You need a loop to repeat sending the request until the responses match. I do not know enough about your process to know what code belongs inside the loop and what needs to be done if there out and out2 don't match. Here's a guess.
# missing inports

def write_command(ser, cmd):
    """Avoid duplicate code"""
    ser.write(cmd)
    reply = ser.read(22)
    out = reply[11:16]
    print(out)
    return out

ser = serial.Serial(
        port='/dev/ttyUSB0',
        baudrate=9600,
        timeout=1,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS)

cmd = bytearray([0x1])
while True:
    out = write_command(ser, cmd)
    time.sleep(0)
    out2 = write_command(ser, cmd)

    if out == out2:
        cursor = connection.cursor()
        sql = "Update silos1 set weight = %s, date = %s where id = %s " 
        datetime.strptime("12/31/2000", "%m/%d/%Y")
        newHireDate = datetime.today()
        rowCount = cursor.execute(sql, (out, newHireDate, 1 ) )
        connection.commit() 
        print ("ok", newHireDate)
        break

    print ("data is not correct")
    connection.close()  # Do you want to do this?

thanks a lot. It turned out even better than I wanted.