Robotics StackExchange | Archived questions

Publish scanned barcode and then read by the subscriber node

This is pretty simple, but I am new to this and I can't seem to find a way to do it. I have a USB barcode scanner (NOT QR code) and when I plug it into my Raspberry PI and scan a barcode it just prints the barcode number and the message: command not found. I want to be able to scan the barcode then the publisher sees it and publishes to a node, and then the subscriber sees that node and displays the barcode scanned.

Thank you in advance!

EDIT I forgot to mention this is done in Python. I've tried many different ways and followed many tutorials but I cannot find a way to do it.

Asked by Gheryla1 on 2016-10-18 14:23:36 UTC

Comments

Answers

Most of the bardcode readers I've seen emulate a keyboard; which is probably why you're seeing the input come is as a command followed by a newline, and of course a random bardcode is not a valid command, so when the shell tries to execute it you get command not found

Getting the barcode scanner to NOT act like a keyboard (or at least not send input to your terminal) means interacting with the linux kernel's input subsystem. This post seems to discuss the correct way to do the setup and get an exclusive lock on the barcode scanner, but it's in ruby.

It looks like python-evdev provides the same functionality in Python, and even includes a grab() method so that you don't have to handle the process of getting an exclusive lock manually.

Asked by ahendrix on 2016-10-19 11:10:12 UTC

Comments

Why would I want to get the barcode scanner to not act like a keyboard though? In my case it's pretty simple and I don't need to make it exclusive. I just want to be able to send it through the Publisher node and then the Subscriber to display it.

Asked by Gheryla1 on 2016-10-19 17:48:44 UTC

Scenario: Open roscore in terminal Open Publisher in new terminal Open Listener in new terminal Now it doesn't matter if I need to open a new terminal or not for the barcode scanner. I just need to get this to send.

Asked by Gheryla1 on 2016-10-19 17:49:40 UTC

If the barcode scanner is acting as a keyboard and you don't capture its input explicitly from your publisher, then the scanned barcode will go to whichever window currently has focus, which means you'll have to keep the window with the publisher on top for the entire time that you're using it.

Asked by ahendrix on 2016-10-19 18:15:55 UTC

So I guess the best way would be to capture it in publisher. Well the good way. Do you think using the raw_input() would work in this case? I was reading and came across that, but I am not sure how it works in this case.

Asked by Gheryla1 on 2016-10-19 21:06:50 UTC

So raw_input() worked!! I am able to get the scanned barcode number in the Subscriber node. The only problem is that I need to have the Publisher window on top which you mentioned...

So how can I get the scanned barcode in publisher without having the window on top?

Asked by Gheryla1 on 2016-10-20 12:29:24 UTC

You can get the input events from the barcode scanner by using python-evdev , and you can get input events even when the window is not on top by capturing capturing the device, as I described in my original answer.

Asked by ahendrix on 2016-10-20 18:36:47 UTC

Thank you, I will look into it this week.

Asked by Gheryla1 on 2016-10-24 12:42:40 UTC