Mudi
Posts: 3
Joined: Fri Aug 09, 2019 10:04 am

How to read and process the input from Barcode Scanner/USB Port

Fri Aug 09, 2019 10:29 am

Goal:
Read using Barcode-scanner and process the information (Check the received code is correct and take decisions accordingly)

Problems:
Error 13: permission denied opening/dev/hidraw0 [which can be solved with sudo permission ]

Code: Select all

import sys
fp = open('/dev/hidraw0', 'rb')
simply display the number at the terminal, I want that number in my program to further process it and ideally, it should not display it on terminal output

User avatar
thagrol
Posts: 2961
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to read and process the input from Barcode Scanner/USB Port

Fri Aug 09, 2019 7:23 pm

Make, model and a link to the specs of your barcode reader would help...

While it's beem quite some time since I last did anything with barcode readers it was my experience that most appeared on the system as USB keyboard devices though things may have changed since then. Back then pretty much all a barcode reader did was insert characters into the keyboard buffer.

Given the behaviour you describe, (characters appearing on screen with no explicit print code) I'd guess that's what is happening here. Plus if the code you supplied is the entirety of your programme, it will exit immediately after opening the hidraw device without ever reading from it and without producing any output.

Post your whole programme and check what device the system thinks is attached and the driver it's using.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Mudi
Posts: 3
Joined: Fri Aug 09, 2019 10:04 am

Re: How to read and process the input from Barcode Scanner/USB Port

Wed Aug 21, 2019 10:49 pm

Bluetooth Laser Barcode Scanner from Tao Tronics (www.taotronics.com)
Model TT-BS022

Raspberry Pi 4 detects it as a keyboard and I can see /dev/hidraw2 appearing and disappearing after connecting and disconnecting the barcode reader respectively.

Code: Select all

import sys

hid = { 4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';' , 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'  }

hid2 = { 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':' , 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'  }

fp = open('/dev/hidraw2', 'rb')


ss = ""
shift = False

done = False

while not done:

	## Get the character from the HID
	buffer = fp.read(8)
	for c in buffer:
		if ord(c) > 0:

			##  40 is carriage return which signifies
			##  we are done looking for characters
			if int(ord(c)) == 40:
				done = True
				break;

			##  If we are shifted then we have to 
			##  use the hid2 characters.
			if shift: 

				## If it is a '2' then it is the shift key
				if int(ord(c)) == 2 :
					shift = True

				## if not a 2 then lookup the mapping
				else:
					ss += hid2[ int(ord(c)) ]
					shift = False

			##  If we are not shifted then use
			##  the hid characters

			else:

				## If it is a '2' then it is the shift key
				if int(ord(c)) == 2 :
					shift = True

				## if not a 2 then lookup the mapping
				else:
					ss += hid[ int(ord(c)) ]
			
print ss

Mudi
Posts: 3
Joined: Fri Aug 09, 2019 10:04 am

Re: How to read and process the input from Barcode Scanner/USB Port

Wed Aug 21, 2019 11:11 pm

thagrol wrote:
Fri Aug 09, 2019 7:23 pm
Make, model and a link to the specs of your barcode reader would help...

While it's beem quite some time since I last did anything with barcode readers it was my experience that most appeared on the system as USB keyboard devices though things may have changed since then. Back then pretty much all a barcode reader did was insert characters into the keyboard buffer.

Given the behaviour you describe, (characters appearing on screen with no explicit print code) I'd guess that's what is happening here. Plus if the code you supplied is the entirety of your programme, it will exit immediately after opening the hidraw device without ever reading from it and without producing any output.

Post your whole programme and check what device the system thinks is attached and the driver it's using.
Bluetooth Laser Barcode Scanner from Tao Tronics (www.taotronics.com)
Model TT-BS022

Raspberry Pi 4 detects it as a keyboard and I can see /dev/hidraw2 appearing and disappearing after connecting and disconnecting the barcode reader respectively.

Code: Select all

import sys

hid = { 4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';' , 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'  }

hid2 = { 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':' , 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'  }

fp = open('/dev/hidraw2', 'rb')


ss = ""
shift = False

done = False

while not done:

	## Get the character from the HID
	buffer = fp.read(8)
	for c in buffer:
		if ord(c) > 0:

			##  40 is carriage return which signifies
			##  we are done looking for characters
			if int(ord(c)) == 40:
				done = True
				break;

			##  If we are shifted then we have to 
			##  use the hid2 characters.
			if shift: 

				## If it is a '2' then it is the shift key
				if int(ord(c)) == 2 :
					shift = True

				## if not a 2 then lookup the mapping
				else:
					ss += hid2[ int(ord(c)) ]
					shift = False

			##  If we are not shifted then use
			##  the hid characters

			else:

				## If it is a '2' then it is the shift key
				if int(ord(c)) == 2 :
					shift = True

				## if not a 2 then lookup the mapping
				else:
					ss += hid[ int(ord(c)) ]
			
print ss

User avatar
thagrol
Posts: 2961
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to read and process the input from Barcode Scanner/USB Port

Wed Aug 21, 2019 11:35 pm

As I said, it's been a while since I last did anything with barcode readers.

This is speculative but I have a couple of suggestions based on the information you've provided. The only thing I've done recently that relates to USB keyboard is make a Pi Zero impersonate one.

The behaviour you've descibes makes me think that your system is sending the key events from the barcode reader into the console's keyboard buffer.

The way I see it you have two options:
  1. Stop trying to use the hidraw device and just read from the keyboard. Might not be possible as you clearly also need keyboard input to stop your program.
  2. Stop your system from treating the barcode reader as a keyboard so it only sends data via the hidraw device. Not sure how you'll do that with a bluetooth connected device. If it were hardwired I'd be looking at udev.
Looks like I'm not going to be of much further help to you. Sorry. I don't have or have access to that barcode reader.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

myuncleisahunkle
Posts: 2
Joined: Thu Apr 16, 2020 11:14 am

Re: How to read and process the input from Barcode Scanner/USB Port

Thu Apr 16, 2020 11:25 am

Wishing to revive this old thread to ask if anyone knows why this wouldn't be compatible with my P3 Model B+?

WINSON WNL-2000g
Image

deepo
Posts: 574
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: How to read and process the input from Barcode Scanner/USB Port

Thu Apr 16, 2020 8:20 pm

The info I can find says that it's compatible (no need for drivers) with Windows, Mac, and Linux.
So it's probably a HID device, i.e. it sends data on USB as a USB keyboard does.
So you should be able to plug it in, start a text editor, scan a barcode and see the contents of the barcode.

/Mogens

myuncleisahunkle
Posts: 2
Joined: Thu Apr 16, 2020 11:14 am

Re: How to read and process the input from Barcode Scanner/USB Port

Fri Apr 17, 2020 1:37 pm

Thanks [deepo]

As luck would have it I didn't win the bidding on that item in the end... I'll get a device and post again if I have any trouble!

PawanN
Posts: 1
Joined: Wed Jul 01, 2020 1:09 pm

Re: How to read and process the input from Barcode Scanner/USB Port

Wed Jul 01, 2020 1:14 pm

Hi,

I am using MH-ET LIVE Scanner v3.0, over /dev/hidraw1 i see some buffer data coming when scanned but getting below error

b'\x00\x00%\x00\x00\x00\x00\x00'
0
00 00 25 00 00 00 00 00
Traceback (most recent call last):
File "Serialport2.py", line 100, in <module>
UPC_lookup(api_key, barcode_reader())
File "Serialport2.py", line 42, in barcode_reader
if ord(c) > 0:
TypeError: ord() expected string of length 1, but int found

Code: Select all

#!/usr/bin/python
import sys
import requests
import json

api_key = ""  # https://upcdatabase.org/


def barcode_reader():
    print('from barcode reader')
    """Barcode code obtained from 'brechmos'
    https://www.raspberrypi.org/forums/viewtopic.php?f=45&t=55100"""
    hid = {4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k', 15: 'l',
           16: 'm',
           17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v', 26: 'w', 27: 'x', 28: 'y',
           29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7', 37: '8', 38: '9', 39: '0', 44: ' ',
           45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';', 52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'}

    hid2 = {4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K', 15: 'L',
            16: 'M',
            17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V', 26: 'W', 27: 'X', 28: 'Y',
            29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&', 37: '*', 38: '(', 39: ')', 44: ' ',
            45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':', 52: '"', 53: '~', 54: '<', 55: '>', 56: '?'}

    fp = open('/dev/hidraw1', 'rb')
    #fp = open('/dev/ttyUSB0', 'rb')
    #print(fp)

    ss = ""
    shift = False

    done = False

    while not done:

        ## Get the character from the HID
        buffer = fp.read(8)
        print(buffer)
        for c in buffer:
            print(c)
            print(" ".join("{:02X}".format(c) for c in buffer))
            if ord(c) > 0:
                print(ord(c))

                ##  40 is carriage return which signifies
                ##  we are done looking for characters
                if int(ord(c)) == 40:
                    done = True
                    break;

                ##  If we are shifted then we have to
                ##  use the hid2 characters.
                if shift:

                    ## If it is a '2' then it is the shift key
                    if int(ord(c)) == 2:
                        shift = True

                    ## if not a 2 then lookup the mapping
                    else:
                        ss += hid2[int(ord(c))]
                        shift = False

                ##  If we are not shifted then use
                ##  the hid characters

                else:

                    ## If it is a '2' then it is the shift key
                    if int(ord(c)) == 2:
                        shift = True

                    ## if not a 2 then lookup the mapping
                    else:
                        ss += hid[int(ord(c))]
    return ss


def UPC_lookup(api_key, upc):
    '''V3 API'''
    print(upc)
    # url = "https://api.upcdatabase.org/product/%s/%s" % (upc, api_key)
    #
    # headers = {
    #     'cache-control': "no-cache",
    # }
    #
    # response = requests.request("GET", url, headers=headers)
    #
    # print("-----" * 5)
    # print(upc)
    # print(json.dumps(response.json(), indent=2))
    # print("-----" * 5 + "\n")


if __name__ == '__main__':
    try:
        while True:
            #print('hi')
            UPC_lookup(api_key, barcode_reader())
    except KeyboardInterrupt:
        pass

Return to “Beginners”