Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UART Receive characters comes out of order #11

Open
CharlesMod opened this issue Jun 16, 2023 · 1 comment
Open

UART Receive characters comes out of order #11

CharlesMod opened this issue Jun 16, 2023 · 1 comment

Comments

@CharlesMod
Copy link

CharlesMod commented Jun 16, 2023

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant