0

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

Niels Dingsbums
  • 113
  • 1
  • 1
  • 7

1 Answers1

1

In the threading calls, you are calling the target, remove the parens from the end of the target= assignments and it will work as expected.

Also, you do not need to call the serial read function in a thread, that is wasting cycles, as the handle data thread already calls serial read to get the data.

DJHenjin
  • 87
  • 4