1

I have looked around on the StackOverflow forums, unable to find a solution that applies to my specific problem.

I need to write a bit of code, that is continuously checking for user input. I have got a Raspberry Pi with a Barcode Scanner attached to it. I want my Python script to loop, waiting for my Barcode Scanner to bleep something (which will then "type" it in the active window, it's acting like a keyboard). When the barcode scanner 'types' the 8 digit number - I need the Python script to stop - take the input and save it in a variable.

This is the only psuedocode I could come up with:

// Create variable, store an empty string

// Create a while loop
// Within the while loop, continuously check for input. 
// If input has been found, stop the loop and save the input in a variable.

I am terribly sorry I couldn't come up with my own code - I just have no idea where to start.

EDIT: The scanner 'types' the digits out. But does not press ENTER. So I have no idea how I can program around that.

Cake
  • 493
  • 7
  • 16

3 Answers3

1

You can use this library on a RaspberryPi: https://pypi.python.org/pypi/readchar

import readchar


inputStr = ""

while len(inputStr) != 8:
    inputStr += str(readchar.readchar())

# Quote: "Save it in variable"

variable = inputStr

# Clean
inputStr = ""

Or, to shorten everything:

import readchar

variable = ""
while len(variable) != 8:
    variable += str(readchar.readchar())
Mijago
  • 1,569
  • 15
  • 18
  • Readchar definitely seems to be the way to go. However, when I try your code. I get a UnsupportedOperation: fileno error. It says there is something wrong with `variable += readchar.readchar()` – Cake Jan 26 '16 at 18:08
  • I added a cast to string. Please try this, maybe this was the error. If not, try variable = variable + str(readchar.readchar()) – Mijago Jan 26 '16 at 18:10
  • It seems at it is a library-error :( I will take a look on this later. – Mijago Jan 26 '16 at 18:14
1

Have you considered evdev?

There is a good example: How can I get a String from HID device in Python with evdev?

import evdev
from evdev import InputDevice, categorize, ecodes  

# Change /dev/input/event1 to the eventX, where X is your scanner's address
dev = InputDevice('/dev/input/event1')


scancodes = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'q', 17: u'w', 18: u'e', 19: u'r',
    20: u't', 21: u'y', 22: u'u', 23: u'i', 24: u'o', 25: u'p', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'a', 31: u's', 32: u'd', 33: u'f', 34: u'g', 35: u'h', 36: u'j', 37: u'k', 38: u'l', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'z', 45: u'x', 46: u'c', 47: u'v', 48: u'b', 49: u'n',
    50: u'm', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}

capscodes = {
    0: None, 1: u'ESC', 2: u'!', 3: u'@', 4: u'#', 5: u'$', 6: u'%', 7: u'^', 8: u'&', 9: u'*',
    10: u'(', 11: u')', 12: u'_', 13: u'+', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'{', 27: u'}', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u':',
    40: u'\'', 41: u'~', 42: u'LSHFT', 43: u'|', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u'<', 52: u'>', 53: u'?', 54: u'RSHFT', 56: u'LALT', , 57: u' ' 100: u'RALT'
}
#setup vars
x = ''
caps = False

#grab grants exclusive access to device
dev.grab()

#loop
for event in dev.read_loop():
    if event.type == ecodes.EV_KEY:
        data = categorize(event)  # Save the event temporarily to introspect it
        if data.scancode == 42:
            if data.keystate == 1:
                caps = True
            if data.keystate == 0:
                caps = False
        if data.keystate == 1:  # Down events only
            if caps:
                key_lookup = u'{}'.format(capscodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode)  # Lookup or return UNKNOWN:XX
            else:
                key_lookup = u'{}'.format(scancodes.get(data.scancode)) or u'UNKNOWN:[{}]'.format(data.scancode)  # Lookup or return UNKNOWN:XX
            if (data.scancode != 42) and (data.scancode != 28):
                x += key_lookup  # Print it all out!
            if(data.scancode == 28):
                print x
                x = ''
Community
  • 1
  • 1
gatorback
  • 1,351
  • 4
  • 19
  • 44
0

Maybe loop over until the input is not equalled to the barcode length?

barcode = ""
while len(barcode) != 8:
    enter = input("")
    barcode = barcode + enter
print("8 characters were entered.")

I don't know if that is what you want. However, it will check for the user input and for each number inputed., it will add it to the string until the length of the barcode is equalled to 8, which then the loop will break.

Bink
  • 91
  • 6
  • Problem with this is, the barcode scanner only types the digits. It doesn't press ENTER. – Cake Jan 26 '16 at 17:56
  • You can view [this article](http://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-for-a-pressed-key) which shows some ways to do that. – Bink Jan 26 '16 at 17:59