ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Publish scanned barcode and then read by the subscriber node

asked 2016-10-18 14:23:36 -0600

Gheryla1 gravatar image

updated 2016-10-19 08:37:53 -0600

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-10-19 11:10:12 -0600

ahendrix gravatar image

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.

edit flag offensive delete link more


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.

Gheryla1 gravatar image Gheryla1  ( 2016-10-19 17:48:44 -0600 )edit

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.

Gheryla1 gravatar image Gheryla1  ( 2016-10-19 17:49:40 -0600 )edit

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.

ahendrix gravatar image ahendrix  ( 2016-10-19 18:15:55 -0600 )edit

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.

Gheryla1 gravatar image Gheryla1  ( 2016-10-19 21:06:50 -0600 )edit

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?

Gheryla1 gravatar image Gheryla1  ( 2016-10-20 12:29:24 -0600 )edit

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.

ahendrix gravatar image ahendrix  ( 2016-10-20 18:36:47 -0600 )edit

Thank you, I will look into it this week.

Gheryla1 gravatar image Gheryla1  ( 2016-10-24 12:42:40 -0600 )edit

Question Tools

1 follower


Asked: 2016-10-18 14:23:36 -0600

Seen: 614 times

Last updated: Oct 19 '16