Python barcode scanner buffer is not clearing
Question: OS is Windows 7 x64 (running Python x86) but code needs to be exportable to
Windows XP and Ubuntu Linux.

This part of the problem is Windows 7 x64 specific, possibly.

I am using a USB barcode scanner that is HID Keyboard compatible.

I open a command prompt in the folder where my keyboard.py file exists and
execute it.

It works perfectly the first time and exits, OK.

When I rerun in the same window the scanner data appears to be buffered and no
longer changes when new scans are made.

I've tried closing the code using:

    
    
    .flush() .readline() and .read() all to no avail. Is this a flaw in Win7 or am I missing something.
    

CODE:

    
    
    import sys
    
    x = ''
    oStr = ''
    #while x != 'exit':
    while True:
        x = sys.stdin.read(1)
        if (x.find('\n') != -1) or (x.find('\04') != -1):
            break
    
        oStr = oStr + x
    
    
    print 'Output String: ' + oStr #+ '\n'
    
    
    Output:
    

> D:\Python Projects\Keyboard Input>keyboard.py
> [)>0617V33SR41P12973001S10515725 Output String:
> [)>0617V33SR41P12973001S10515725
>
> D:\Python Projects\Keyboard Input>keyboard.py
> [)>0617V33SR41P12973001S10515725

The second run stalls, as shown. But, the second scan ended with 23 not 25!

If I press `Enter` the script completes. Further scans are the same.


Answer: To clarify my previous comment, start with this:

    
    
    while 1:
      print sys.stdin.readline.strip()
    

This should give you a barcode per trigger press. If this works and then
running the program sequentially fails you need to look at how your barcode
scanner is configured (and this would be a windows configuration issue, not a
python issue).

* * *

**Update:** windows console displayed some weirdness in console which you can
workaround by using msvcrt.getch(), however this is not a cross platform
solution. One possible approach to making this cross platform would be
something like:

    
    
    try:
      from msvcrt import getch
    except:
      import sys
      getch = lambda: sys.stdin.read(1)
    

Where you would then program against getch() for reading input.

