User avatar
Batmitesg
Posts: 4
Joined: Tue Jun 05, 2018 1:03 am

USB Barcode Reader on Rpi 3 B

Tue Jun 05, 2018 7:00 am

Hi guys,

I am pretty new to Rpi and Python, in the past I was playing in C++ & Java.

On my latest project, I am connecting a Barcode reader trough USB on a Rpi 3 B. (The reader is a Cognex DM50 configured with HID communication) The reader is not just sending what its reading, there is a short Javascript processing from the reader.
Here is the JS:

Code: Select all

function onResult (decodeResults, readerProperties, output)
{	
var x = 0;
	if (decodeResults[0].decoded)
	{
		if(decodeResults[0].validation.state ==2 && (decodeResults[0].symbology.angle < 10 || decodeResults[0].symbology.angle > 350))
			{
			output.content =  '=' + decodeResults[0].content + "\r"; //Correct reading, identification character = ASCII Number 46
			x = 1;
			}
		else if(decodeResults[0].validation.state ==2 && !(decodeResults[0].symbology.angle < 10 || decodeResults[0].symbology.angle > 350))
			{
			output.content = '[' +decodeResults[0].symbology.angle + "\r"; //Wrong Angle, identification character [ ASCII Number 47
			output.events.system = Event.system.validationFailure;
			x = 1;
			}
		else if (decodeResults[0].validation.state ==1)
			{
			output.content = ']'+decodeResults[0].content + "\r"; //Wrong Acode, identification character ] ASCII Number 48
			output.events.system = Event.system.validationFailure;
			x = 1;
			}
	}
	if (x==0)
		{
		output.content = '~' + "\r"; //Reading Error, identification character ~ ASCII Number 53
		output.events.system = Event.system.validationFailure;
		}
}
I have done the same thing on an Arduino Uno with a USB shield and it works fine.

When I am using the code from this post:
viewtopic.php?t=55100
I have a problem, the code is being printed twice :roll: .

Here is the python code:

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/hidraw3', 'rb')


ss = ""
shift = False

done = False

while not done:

	## Get the character from the HID
	buffer = fp.read(1)
	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)) ]
					#print "1-->" + hid[ int(ord(c)) ]
					#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 "2-->" + hid[ int(ord(c)) ]
					#c=""
			
print ss
print "Reading done"
If I run the python file and trigger the barcode reader here is what I have:

Code: Select all

[email protected]:~ $ sudo python barcode1.py
= A1088203 = A1088203 
If I trigger the barcode reader in the terminal or in a text editor I have the correct expected, and the \r is there:

Code: Select all

Terminal
[email protected]:~ $ = A1088203 
bash: =: command not found

Text Editor
= A1088203 

I tried to add some prints here and there to try to locate the issue and the extra = A1088203 is printed during the while.

The Barcode reader is configured as an HID keyboard with US layout. I tried other layouts and it was messing all up.

I then tried to run it with Python3, and after I added some parenthesis on the print and I got an error that hopefully will be of any help.

Code: Select all

[email protected]:~ $ sudo python barcode1.py
Traceback (most recent call last):
  File "barcode1.py", line 20, in <module>
    if ord(c) > 0:
TypeError: ord() expected string of length 1, but int found
= [email protected]:~ $ = A1088203 
bash: =: command not found
I am guessing the issue has to do with the buffer somehow, but I am clueless at the moment, so I would gladly appreciate some hints or helps from you guys.

In the meantime I will keep trying on my side as well.

Cheers

Return to “Add-ons”

Who is online

Users browsing this forum: No registered users and 7 guests