Загрузка данных


import serial
import pydirectinput

ser = serial.Serial('COM3', 9600)

leftHeld = False
rightHeld = False

while True:

    try:
        line = ser.readline().decode().strip()

        print(line)

        # ===== RIGHT =====
        if line == "RIGHT":

            if leftHeld:
                pydirectinput.keyUp('left')
                leftHeld = False

            if not rightHeld:
                pydirectinput.keyDown('right')
                rightHeld = True

        # ===== LEFT =====
        elif line == "LEFT":

            if rightHeld:
                pydirectinput.keyUp('right')
                rightHeld = False

            if not leftHeld:
                pydirectinput.keyDown('left')
                leftHeld = True

        # ===== STOP =====
        elif line == "STOP":

            if leftHeld:
                pydirectinput.keyUp('left')
                leftHeld = False

            if rightHeld:
                pydirectinput.keyUp('right')
                rightHeld = False

    except Exception as e:
        print(e)