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

Python • Non-blocking capture of input from stdin on headless unit

$
0
0
I have a barcode scanner that I want to read non-blocking with no monitor.
The code I have works if I boot to desktop, run the code from Geany , and leave the resulting terminal window open and in focus. If I click anywhere on the monitor that does not have any type of input field, it won't work.
It will also work if I boot to CLI, as long as I have a monitor connected.
If I disconnect the monitor, it no longer captures anything (at least from the scanner) from stdin.
Here is a pared down version where I'm just trying to get the input and write it to a file: (code from the web, I'll admit I don't know how it works)

Code:

import system, selectdef barcode(block=True):    if block or select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):        bc = sys.stdin.readline()        return bc    return Nonewhile True:    bar = barcode()    if bar:        with open('/home/pi/result', 'a') as f:            f.write(f'barcode = {bar}\n')    else:        with open('/home/pi/result', 'a') as f:            f.write('No code read\n')
Just gives me rows and rows of 'No code read'

Statistics: Posted by evanmars — Thu Sep 12, 2024 6:29 pm



Viewing all articles
Browse latest Browse all 4919

Trending Articles