Interface barcode scanner with arduino yun

C or Python? Both will get the job done. Which are you more comfortable with?

I've been doing C for MANY years, so that would be my first choice. However, C needs a lot more development environment support. It's difficult to get the compiler environment running on the Yun, so that probably means cross-compiling it on another computer, then copying it to the Yun for running/testing. That can also be a big job if you don't have that environment already set up. While it's more work to set up, the advantage is that you get fast compiled code. But it's more work to maintain.

For Python, the necessary environment is already well supported by the Yun. It's an interpreted language, so there is no need to compile it: just edit your code, then run it. The downside is that being interpreted, it won't be as fast as a compiled language. But on the other hand, there is already support for a lot of the Yun features, including interacting with the Bridge library.

I'm an old C coder, but in spite of that, I'm using the Yun as an excuse to learn Python. To me, at least, it seems the way to go.

Python solution:

sonnyyu:
Re: Using USB Keyboard

Using USB Keyboard - #6 by sonnyyu - Arduino Yún - Arduino Forum

USB keyboard support should be fixed in the latest image

Using USB Keyboard - #27 by federicofissore - Arduino Yún - Arduino Forum

Make USB Keyboard works with Arduino, by combine both code.

Using USB Keyboard - #22 by sonnyyu - Arduino Yún - Arduino Forum

Dear Mr.Sonnyyu,

I ran the command, ./evdev.py '/dev/input/event1' and got the expected result.

Please clarify where I have to insert the below code. Should I insert it in evdev.py program or somewhere else?

#!/usr/bin/python
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
bc = bridgeclient()
bc.put('to_arduino',keyboard_code)

And How can I interface this bridgeclient with arduino program. I wish to get the resulted keyboard_code in serial terminal of arduino software.

Kindly help.

sridevi:
And How can I interface this bridgeclient with arduino program. I wish to get the resulted keyboard_code in serial terminal of arduino software.

The Python code is using "put" to write a value to the key "to_arduino"

On the Arduino side, you need to use the bridge library to "get" the value from that same key. You could do something like this (I've not tried running it yet, so there may be issues with the code):

#include <Bridge.h>

#define BUFF_LEN 20

void setup() {
  Bridge.begin();
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect.
}

void loop() {
  char keyboard_code[BUFF_LEN];
  int code_len;
  
  // Get the string that was last "put" by the Python code
  code_len = Bridge.get("to_arduino", keyboard_code, BUFF_LEN);

  // Print out the length of the code and the string.
  Serial.print("keyboard_code is ");
  Serial.print(code_len);
  Serial.print("chars long: ");
  Serial.println(keyboard_code);  
}

You will no doubt want something more sophisticated. This will try to read values from the "to_arduino" key and print it as fast as possible, whether or not the Python code has actually changed it. You will want to add some sort of handshaking between the Arduino code and the Python code so that the Python code can let the Arduino side know that there is new data:

  • Have the Arduino side look for a changed value before printing it. The downside to this is you wouldn't be able to pass the same code twice
  • Have the Arduino side clear out the "to_arduino" key by using "put" to set a blank value. Then, if the "get" value is not blank, print it then clear it again
  • Have the Python side "put" another value in a key like "ready" to indicate new data, which gets set after updating the "to_arduino" key. The Arduino side uses "get" to read this value, and if it signals ready it reads the "to_arduino" key then clears the ready value.
  • Have the Python side increment a value and store it in a "ready" key after it stores a new value in the "to_arduino" key. If the "Arduino" side sees a different value than before in the "ready" key, it can then read the "to_arduino" key.

I want to get this keyboard_code value in vb.net program. Is it possible to send any variable to visual studio program which is in my PC. PC and Arduino yun are in same network connection via ethernet. Connection establishment is ok. How to send and receive data in both sides? Any ideas?

Pyhon provides some libraries to create TCP/IP connections.

Take a look here TcpCommunication - Python Wiki

Thanks. I made it. I used yunServer, yunClient in Arduino side and webRequest, webResponse in VB.Net side.

I'm starting a new project, and a quick search immediately turned up this post. I had forgotten about this thread. Thank you sir, this worked like a charm with my old Symbol scanners! I am once again indebted to you. :smiley:

sonnyyu:

opkg update

opkg install kmod-hid
opkg install kmod-input-evdev
opkg install kmod-input-core
opkg install kmod-usb-hid




The barcode scanner is at 



/dev/input/event1




Now we need compile c program "barcode"

micagordon:
What bar code scanner resource did you use?

I use a pair of Symbol LS2104 handheld USB scanners that I bought used off of eBay a couple years ago: two for $20.

These act like USB keyboards, so whatever is scanned is "typed" into whatever application is running. They Python code I wrote accesses the underlying HID device directly, captures events, and converts them to strings that are sent to the sketch.

Now I only have a .net barcode scanner library

Wow, that is an expensive package! :o

I'm a bit confused... is that a package that takes an image (like from a camera) and then decodes the barcode? If so, I have no experience with such a package. I've only worked with scanner hardware that handles all of the decoding internally. (The only exception is the time I wrote a routine to take the light/dark output from a pen style scanner, and extracted the timing and wide/narrow pulse data and decoded that. It worked OK with a clean crisp code, but it was not very robust of tolerant of poor print quality.)