In my script I have a while True
loop that constantly checks a mysql database. My problem is, that this while True loop seems to run only once! I added variable i
, but it's also stuck at 0. Can anyone help me?
import mysql.connector
import serial
import threading
import time
mydb = mysql.connector.connect(
host="",
user="",
passwd="",
database=""
)
cursor = mydb.cursor(buffered=True)
getData = "SELECT * FROM log"
ser = serial.Serial(
port="/dev/tty.usbmodem14111"
)
def serial_read():
while True:
line = ser.readline().decode('utf-8')
return line
def serial_write(mode):
while serial_read().startswith("OK") == False:
print(f"Set: {handle_data().mode}")
ser.write(f"Set: {handle_data().mode}".encode('utf-8'))
return True
def handle_data():
modeOld = None
i = 0
while True:
i += 1
cursor.execute(getData)
mydb.commit()
values = cursor.fetchall()
mode = values[-1][1]
print(serial_read())
if mode != modeOld:
print("{}: {}".format(i, mode))
serial_write(mode)
modeOld = mode
if serial_write(mode) == True:
print("Mode Set successfully!")
serial_read_thread = threading.Thread(target=serial_read())
handle_data_thread = threading.Thread(target=handle_data())
serial_read_thread.start()
handle_data_thread.start()
Edit 1: I deleted the try except block
Edit 2: Now I know that it's in the if mode != modeOld
-block. I just can't find the error there.
Edit 3: I now use threading. It still seems to iterate only once