LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-04-2009, 12:18 AM   #1
dogfuel
LQ Newbie
 
Registered: Oct 2009
Posts: 9

Rep: Reputation: 0
I never claimed to be that sharp... Python - log scanned barcode characters to file


55 of you may notice I originally tried to solve this with evdev ... this was just me being ignorant ...

I have a barcode scanner that, like most, injects the scanned string and a lf as text from a keyboard. Rather than scan data into an application, I'd like to simply log the time of the scan and barcode data scanned to a file.

I'm so new to this I feel like an idiot but assume I can loop and use the device to distinguish between the "real" keyboard and the scanner (scanner is on /dev/input/event7). My question is how to detect the scan, grab the characters to a file and, if possible, stop the scanned barcode characters from getting to X.

So far:
PHP Code:
import timecodecs
scanner_location 
'/dev/input/event7'  # get the correct one from HAL

while True:
    
scanner_device open(scanner_location,"r")
    
# log stuff to file here - below line for testing/debugging
    
print repr(scanner_device.readline())
    
scanner_device.close()
    
time.sleep(0.1
This detects and captures what I need but not in a human readable form (a byte stream?) ... how to I convert the output (...'\xc8J\x88\xc1\x04\x00\x04\x00\x0...) to information I can use?

Thanks!

Last edited by dogfuel; 10-04-2009 at 01:22 AM.
 
Old 10-06-2009, 09:35 PM   #2
dogfuel
LQ Newbie
 
Registered: Oct 2009
Posts: 9

Original Poster
Rep: Reputation: 0
can anyone help with a place to start?

humor the ignorant ... what is the string ...'\xc8J\x88\xc1\x04\x00\x04\x00\x0... that is output by the code above? Is this an encoding issue?

Thanks!
 
Old 10-07-2009, 12:31 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
I'm so new to this I feel like an idiot ...
Don't worry, happens to me all the time

My guess is that that's raw data (so indeed an encoding issue). How is the scanner connected? Parallel to the keyboard? Or USB? Or serial?

This question might fit better in the programming section. If you agree, you can report your post and ask it to be moved.
 
Old 10-07-2009, 04:42 AM   #4
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 3,896

Rep: Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774
I know less than you but...you would expect a stream of digits. For this purpose it may be worth getting, eg, a pack of soap powder where they print the UPC or whatever they call it beneath the bar code so that you can compare the stream of electronic information with the printed UPC. Having 'the answer' in front of you will result in additional clues.

Quote:
'\xc8J\x88\xc1\x04\x00\x04\x00\x0..
I'd be guessing at ascii-hex 88, c1, 04, 00 ...but that raises a couple of interesting questions

-what about that single zero at the end...is that the representation of 00, or has a character fallen off at the end

-that c8J at the start, specifically the J. Maybe the J signifies a valid start sequence has been detected or something, because you wouldn't be expecting a j in a hex stream.

At some point, the manual for your reader has to be a help, and particularly when it comes to what the reader does with different encodings (maybe you get a code to tell you which encoding it is, maybe it is all 'transparent', I don't know and it probably depends on the reader)
 
Old 10-07-2009, 09:36 AM   #5
dogfuel
LQ Newbie
 
Registered: Oct 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for the help and responses ...

Quote:
My guess is that that's raw data (so indeed an encoding issue). How is the scanner connected? Parallel to the keyboard? Or USB? Or serial?
The scanner is connected via usb - I do have a "keyboard wedge" that will make it appear as ps2 kb input but would prefer not to use this as the PCs I plan to use it with do not have ps2 ports.

Quote:
-what about that single zero at the end...is that the representation of 00, or has a character fallen off at the end

-that c8J at the start, specifically the J. Maybe the J signifies a valid start sequence has been detected or something, because you wouldn't be expecting a j in a hex stream.
I may have been unclear - the actual output per scan is much longer ... I pasted a snippet hopping it would be enough to guess at its form.

Thanks!
 
Old 10-07-2009, 01:06 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 10-09-2009, 08:21 AM   #7
dogfuel
LQ Newbie
 
Registered: Oct 2009
Posts: 9

Original Poster
Rep: Reputation: 0
perhaps I should have clarified ... at the same time that I read the cryptic string from /dev/input/event*, the characters show up on the window with focus. To me this implies that it is a pure encoding issue as there is no software installed on the client to interpret the input.


If I scan an ISBM barcode with the python loop running in a terminal terminal session (A) and the focus on another terminal session (B), I see

In Terminal B
Quote:
9780596100940
which is correct and

and in Terminal A
Quote:
'\xc2K\xcfJD\xc3\x02\x00\x04\x00\x04\x00&\x00\x07\x00\xc2K\xcfJL\xc3\x02\x00\x01\x00\n'
"\xc2K\xcfJ\xafx\x04\x00\x04\x00\x04\x00\x1e\x00\x07\x00\xc2K\xcfJ\xb5x\x04\x00\x01\x00\x02\x00\x01\ x00\x00\x00\xc2K\xcfJ\xb7x\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc2K\xcfJ\xee\x97\x04\x00\x04\x00 \x04\x00\x1e\x00\x07\x00\xc2K\xcfJ\xf5\x97\x04\x00\x01\x00\x02\x00\x00\x00\x00\x00\xc2K\xcfJ\xf6\x97 \x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc2K\xcfJ$\xb7\x04\x00\x04\x00\x04\x00'\x00\x07\x00\xc2K\xc fJ*\xb7\x04\x00\x01\x00\x0b\x00\x01\x00\x00\x00\xc2K\xcfJ*\xb7\x04\x00\x00\x00\x00\x00\x00\x00\x00\x 00\xc2K\xcfJf\xd6\x04\x00\x04\x00\x04\x00'\x00\x07\x00\xc2K\xcfJn\xd6\x04\x00\x01\x00\x0b\x00\x00\x0 0\x00\x00\xc2K\xcfJo\xd6\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc2K\xcfJ\x98\xf5\x04\x00\x04\x00\x 04\x00'\x00\x07\x00\xc2K\xcfJ\x9d\xf5\x04\x00\x01\x00\x0b\x00\x01\x00\x00\x00\xc2K\xcfJ\x9e\xf5\x04\ x00\x00\x00\x00\x00\x00\x00\x00\x00\xc2K\xcfJ\xe6\x14\x05\x00\x04\x00\x04\x00'\x00\x07\x00\xc2K\xcfJ \xed\x14\x05\x00\x01\x00\x0b\x00\x00\x00\x00\x00\xc2K\xcfJ\xee\x14\x05\x00\x00\x00\x00\x00\x00\x00\x 00\x00\xc2K\xcfJ\x154\x05\x00\x04\x00\x04\x00&\x00\x07\x00\xc2K\xcfJ\x1a4\x05\x00\x01\x00\n"
how would I start to determine the encoding? The manual and manufacturer of the scanner have been no help and keep asking why I do not just scan directly into a text box.

Thanks,
joe

Last edited by dogfuel; 10-09-2009 at 09:44 AM.
 
Old 10-09-2009, 10:12 AM   #8
dogfuel
LQ Newbie
 
Registered: Oct 2009
Posts: 9

Original Poster
Rep: Reputation: 0
What is missing? Any help or suggestions greatly appreciated!

I tried the chardet module to deduce encoding with no luck ... I then tried all standard encodings without being able to interpret the line.

This has me really lost - it may be obvious to someone who knows what they're doing but I am stumped!

Thanks,
Joe
 
  


Reply

Tags
input, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
log scanned barcodes (posing as keystrokes) using python and evdev.py dogfuel Linux - Software 0 10-03-2009 03:05 AM
Regarding replacing characters in log file rajaniyer123 Linux - General 4 04-12-2008 10:32 AM
Python - Removing first x characters Mithrilhall Programming 3 01-16-2007 09:52 AM
Removing a colon and all characters before it, in a python list tangle Programming 4 09-12-2006 09:48 PM


All times are GMT -5. The time now is 09:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration