Hi all, I’ve to interface my project with a laser gun hardware to read an existing barcode.
I’ve seen that it’s very simple to create a barcode scanner using camera in mobile apps, but i need to use my program on PC, using this USB hardware.
Before I proceed on purchase that laser gun I’d like to ask if anyone tried to do this before, and can tell me how difficult can be. I looked in the asset store, but I didn’t find anything I can use.
So, how can Unity3d interface with this kind of hardware?
Edit: some more in-depth wisdom after testing parameters…
Has to be in “Keyboard wedge” mode to be read from Input.inputString
Transmission speed is an issue. From my big booklet o’ settings were 2 barcodes for transmission speed settings (under Interface - KBW). Options were 0 and 25 and 25 fixed the problem. I’m not 100% certain what caused it (I suspect dropped bytes [eg only half character getting there in time]) but characters would appear randomly capitalized or not, and numbers would either be numbers or their corresponding symbols on the keyboard. These are NOT reading errors as readings are 100% accurate outside Unity. With the 25 speed setting, readings are accurate inside Unity as well.
I know this is a late answer, but I’ve been doing some work with Barcode Scanners where I needed a reliable way to determine the input from the Barcode Scanner and to differentiate it from other keyboard input. I was also limited to using the old unity input system, so there may be a way to determine the input device using the new one, but I didn’t look into it.
__
In order to differentiate the input from the scanner (in Windows btw), you can read the RawInput which allows you to associate a specific device with an input. This repo has a project that allows you to do just that: GitHub - malaybaku/RawInputSharpUnity: Wrapper library for RawInput.Sharp to use on Unity. , although you can do it yourself using pInvoke, I would recommend using that project as it has an already-working solution for working around the caveats.
__
With the raw input set up, you now can associate raw keycodes with a device (my device had the product name of “Wasp Barcode” so it was easy to tell which inputs were coming from it), and all you need to do is convert those raw keys into characters or a string. This stackoverflow question has insights on how to do that: c# - Convert keycode to char/string - Stack Overflow
__
I ended up doing something similar Ivan Petrov’s answer using KeyCodeToUnicode and it worked well. There were some other small kinks to work out with the scanner I was using, but in the end I was able to reliably get the input from the scanner and could avoid having to have an input field or something selected prior to the user scanning. The app could tell when the scanner was used and do something with the scanned info regardless of what the user was doing or even if the application was in the background.
This is not an unity related question in any way, ask on forums related to .Net / C# / forums related to this hardware (does it come with some API?) , because that’s how you are going to access it.
if you want to create a barcode scanner, why don’t you go to some barcode reader creation website. you will find easy way to create a barcode reader in Visual Studio with a few steps.