Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1246

Python • Re: EOFError from input( )

$
0
0
A more recent update. I came across this post and it did exactly what I was looking for. This person used threading instead of multiprocessing to handle serial read and write separately and it worked reasonably well. I will post the code below

Code:

import serialimport threadingimport timeDEVICE_NAME      = '/dev/ttyUSB0'DEVICE_BAUDRATE  = 19200DEVICE_SETUP_KEY = '*'PORT_DELAY_SEC   = 0.1def port_reader( port, delay=PORT_DELAY_SEC ) :    while True :        time.sleep( delay )        if ( not port.in_waiting ) : continue        with threading.Lock() : line = port.read( port.in_waiting )        print( line.decode(), end='' )def port_writer( port, setup_key=DEVICE_SETUP_KEY, delay=PORT_DELAY_SEC ) :    while True :        time.sleep( delay )        command = input()        if ( command != setup_key ) : command += '\n'        with threading.Lock() : port.write( command.encode() )if __name__ == '__main__' :    port = serial.Serial( DEVICE_NAME, baudrate=DEVICE_BAUDRATE )    thread_reader = threading.Thread( target=port_reader, args=(port,) )    thread_writer = threading.Thread( target=port_writer, args=(port,) )    thread_reader.start()    thread_writer.start()    thread_reader.join()    thread_writer.join()
Here is the original artcle on Medium describing the code:
https://tesudo-yamasaki.medium.com/tran ... 1e2068b8e9

Gerry.

Statistics: Posted by gerrypkan — Mon Apr 08, 2024 12:19 pm



Viewing all articles
Browse latest Browse all 1246

Trending Articles