Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeat request by else
#1
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()
	
Reply
#2
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?
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  get out of while loop and stop repeat Frankduc 11 2,997 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Avoid multiple repeat in indent Frankduc 8 2,906 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  how can I correct the Bad Request error on my curl request tomtom 8 5,088 Oct-03-2021, 06:32 AM
Last Post: tomtom
  How to discard list repeat values akanowhere 7 3,716 Dec-28-2020, 09:14 PM
Last Post: akanowhere
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 2,802 Nov-23-2020, 11:01 PM
Last Post: perfringo
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,945 Jun-18-2020, 08:07 AM
Last Post: buran
  is there a way: repeat key word args Skaperen 2 2,249 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,568 Jul-25-2019, 03:12 PM
Last Post: silfer
  Trying to Repeat a Function? DanielHetherington 1 2,351 Mar-27-2019, 09:48 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020