wiringOP-Python icon indicating copy to clipboard operation
wiringOP-Python copied to clipboard

UART Receive characters comes out of order

Open CharlesMod opened this issue 1 year ago • 1 comments

I have written a simple program that passes characters received over SSH onto the UART device that my OPi Zero 2 is connected to.

However, for some reason after a few messages, characters begin to arrive out of order.

Program (passthru.py):

import wiringpi
import sys
import argparse
from time import sleep
from sshkeyboard import listen_keyboard

angle1 = 10
angle2 = 0

parser = argparse.ArgumentParser(description='')
parser.add_argument("--device", type=str, default="/dev/ttyS5", help='specify the serial node')
args = parser.parse_args()

wiringpi.wiringPiSetup()
serial = wiringpi.serialOpen(args.device, 115200)
if serial < 0:
    print("Unable to open serial device: %s"% args.device)
    sys.exit(-1)

def readSerial():
    try:
        while (wiringpi.serialDataAvail(serial) != -1):
            print(chr(wiringpi.serialGetchar(serial)), end = '')
            sys.stdout.flush()
            #sleep(0.02)
    except ValueError:
        pass
    except KeyboardInterrupt:
        sys.exit(0)

def press(key):
    key = key.capitalize()   
    
    if (key != "Enter"):
      print(key, end = '')
      sys.stdout.flush()
      wiringpi.serialPrintf(serial, key)
    else:
      print("\n", end = '')
      sys.stdout.flush()
      wiringpi.serialPutchar(serial, 10) #newline 
      readSerial()
      wiringpi.serialFlush(serial)
      sys.stdout.flush()
    
    

listen_keyboard(on_press=press)



wiringpi.serialClose(serial)  # Pass in ID

Output: image

CharlesMod avatar Jun 16 '23 22:06 CharlesMod

Additional info: restarting the sketch makes the input clear up every time, the first block of text always comes through OK.

CharlesMod avatar Jun 16 '23 22:06 CharlesMod