few questions concerning usb barcode scanner - c#

this is probably an old question, but I've read through articles about usb barcode scanner and still can't make my mind about how to use it.
I'm using an USB barcode scanner, and to my understanding there are two methods to use it - as keyboard wedge and serial port.
correct me if i'm wrong, but serial port might be easier to use (because there is an distinction between the scanner's input and the keyboard's) but you must hold info about the port, which might be problematic since I can't tell which port the scanner will get on clients' computers. Is that correct?
Second question- if I will go with the keyboard wedge option, I need to tell the scanner's input from the keyboard's. In that case I need to focus on a textbox, and know when the scanner's input begins and ends. There is an option to configure the scanner to send special character at the start/end of the stream. If I don't have control over the client's scanner type and configuration, how can I tell that the specific input is the scanner's?
Thanks ahead

If you have a serial (RS-232) scanner you have to configure in which port number your program listen , usually through the serial port you have some advanced command that you can send to the scanner for enable or disable reading, and so on ...
A Scanner connected through USB can be configured in wedge mode and you have to autofocus on the textBox that you want to fill, optionally some model of scanner can be configured for reading barcode with a prefix or/and a suffix char. It depends on your scanner model .

Have you considered Microsoft POS for .Net? The framework allows you to subscribe to events in C# and then you don't have to worry about setting focus on TextBoxes or the user clicking whilst scanning, etc.
The scanner to be set to IBM-USB and (I think) has to support "Control Objects" to use it with the POS for .Net stuff. An example of one that works well is a Datalogic QuickScan 6500.

Related

Differentiating input from 2 separate keyboards

All,
Apologies in advance if this is an ill-informed question with lack of detailed examples of what I have tried to do so far - all the material I am finding on this topic seems to be out of scope of what I'm trying to do, or its just going right over my head (Learning resources on this are welcome!!).
What I am essentially trying to do is build a small program that will differentiate the input device used to create a user input on my Lenovo Win10 Machine that can run in the background and trigger scripts when certain conditions are met. In my case the input devices are a generic wireless/USB keyboard, and a USB Barcode scanner that acts as a HID Keyboard (The Barcode Scanners plugs directly into the machine, not via a keyboard).
The reason I need to differentiate the input is for example, if I have a script run every time an asterisk is printed from a scanned barcode I want the user to still be able to type an asterisk from the keyboard without triggering the script - Im restricted to using Code 128 barcodes so I also cant bypass this issue by adding characters to the barcodes to trigger the scripts that are not present on the keyboard.
In pseudo-code its essentially
start:
if inputdevice == keyboard 1
then
System.Diagnostics.Process.Start(myBatchFileName);
else
goto start;
Initially I tried to do this in VB, using some code I had previously used to listen to a COM Port to record output from an arduino -
Dim barcodescanner As SerialPort
barcodescanner = New SerialPort()
barcodescanner.PortName = "COMx"
barcodescanner.BaudRate = 9600
barcodescanner.Parity = Parity.None
barcodescanner.DataBits = 8
barcodescanner.StopBits = StopBits.One
Dim barcode As String
barcodescanner.Open()
barcode = barcodescanner.ReadLine()
barcodescanner.Close()
-however for reasons beyond me I cannot successfully use a virtual COM port for the scanner and record data this way, I also tried to create a virtual COM port using drivers from http://www.ftdichip.com/FTDrivers.htm but this was to no avail.
From some investigation I found some C# Classes, namely; InputEventArgs, InputDevice, RoutedEventArgs, that seem as if they could be used to resolve a device ID from an Input/Event that may allow be to harness some information I could use to flesh out and build the pseudo IF THEN ELSE loop above - but im struggling to use the classes and actually build any working code.
Does anyone have any suggestions as to how to do this?
In a perfect world if I could create a virtual COM port for the barcode scanner and listen to it using VB that would be ideal - but if not could anyone suggest a method of doing this in C# or point me to another method?
The problem is that a keyboard-emulating scanner and a keyboard work the same way and Windows does not allow you to find out which device actually sent the keystroke.
One way would be (if possible for your scanner):
Configure the scanner to work as COM port emulation (not possible with all scanners, only industrial ones support this normally)
Install the manufacturer's virtual COM port driver if necessary
Then you can actually open a COM port and communicate with the scanner as desired - you then, however, need to put the scanned strings into text fields yourself if required.
Second possible solution (if available for your device):
Configure the scanner to transmit a certain prefix or suffix along with the barcode content (most scanners can be configured to send a barcode identifier before the content, which is highly unlikely to be typed by the user)
Also configure the scanner to send CRLF or CR as last character
When you receive CRLF or CR, check whether the input starts with that prefix/ends with that suffix
If it does, remove the prefix/suffix from the text box and react to the "*" as desired.

How can i restrict windows form textbox to read input only from my USB barcode scanner?

I'm developing a application where user needs to get input from Barcode scanner.
Its a usb type which scans and returns data in seconds.
I tried to stop getting input from Keyboard by many techniques
Made my textBox read only But failed to get input from reader
Managed keypress event But barcode reader actually acts as a Keyboard so i couldnt able to get required output
I need to Know is there any way to limit user with a particular time for providing input in textBox of windows forms or restrict user from providing input only from particular port?
I use code 128symbology for generating barcode.
Related: How do I read input from a USB HID device?
Related article on CodeProject: http://www.codeproject.com/Articles/17123/Using-Raw-Input-from-C-to-handle-multiple-keyboard
Article on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645543(v=vs.85).aspx
This looks like a possible duplicate, but I'll summarize here to avoid link rot:
Since you indicate your barcode scanner is a USB device that behaves as an HID Keyboard, and you want your program's textbox to accept input from only this one particular device, you can use the HID Raw Input technique. However, this requires processing the raw HID reports from the device.
During initialization, your software registers for Raw Input from an HID input device having a specific Top Level Collection (TLC).
Use GetRawInputDeviceList to enumerate the available raw input devices, and use GetRawInputDeviceInfo to find each device's name. Within the name string you should be able to match the USB VID/PID values, so you can identify whether a keystroke message comes from the barcode scanner. Be sure to save the matching device handle hDevice discovered by this enumeration.
Extend your windowproc to handle WM_INPUT messages by GetMessage and GetRawInputData, and filter out messages from the standard keyboard by matching the raw input handle against the previously determined device handle of the barcode scanner.

C# Application to read USB at Backend and Only my application read the usb and no other

I want to create an application in C# to read two usb barcode scanner. Since the barcode scanner are as HID Keyboard device, my application read the barcode scanner, but if another application is in focus it also receive the input data. Can we write a program to read input and only our application will read and no other.
I believe you need to global hook the keyboard, then suppress desired keys from your application.
There is a great article with complete explanation about Keyboard Global hook, you can find it here:
http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
As bar-code readers only send specific key events, you can simply add them to your hook list and set e.Handled=true to suppress the key event for other applications (assuming you will use the code from the article).

Keyboard Emulating barcode reader

I'm using the Microsoft.PointOfService libraries to interact with POS hardware. Although documentation about compatible hardware is thin on the ground, I've read in places that all keyboard emulating barcode scanners should be compatible.
so now I have my barcode scanner (Wasp WSR-455), which emulates the keyboard, and I was wondering if anybody here knows how to make the posdm tool recognise it, or another way I can program against a barcodeScanned event.
D.R
You don't bother with that, nor can you, when they emulate a keyboard. Either you or the user has to make sure the focus is always on a specific text box so the scanner output goes to the right place. This certainly removes a great deal of flexibility in your UI, you'd really do need a scanner that you can connect to directly over, say, a USB port. They usually emulate a serial port, you'd use the SerialPort class.

"Symbol" brand barcode scanner and c#

I have a "Symbol" brand barcode scanner (USB) and I am trying to capture the data it scans from a barcode into my application (which is running as a service -- with no textbox control, of course).
The catch is that : Whenever you scan something, it acts like a keyboard and outputs the digits to anything that has focus (i.e notepad, word, etc).
My question is: How do I perform this barcode scan in the background and put it in a variable that I can use in my in C#.
So far, the only api's that I've found are for the .NET CF and I need this to be a
windows service.
Basically, I want to be able to send certain keystrokes to an application if the barcode = "123456789-0111" without interfering with the current window that has focus. After my application reads the barcode in then it will look for a certain program (launch it and set focus) and send keystrokes. I am using Code 128.
The HID mode will be your best best.
Put the barcode reader in HID mode and make your service capture the reader. When data comes in take a look at it and if it is one of your special barcodes you can act on it.
If the data is not one of your special barcodes then inject them as keydown/keyup windows messages so that it will seem to work in the same fashion as in keyboard emulation mode.
Put the scanner into USB mode. This is generally done by scanning a series of barcodes in your manual or printed out from the software provided from symbol.
With the scanner plugged in/docked connect to windows update. You'll get an update specific to that scanner. It won't work if if the scanner is not plugged in.
The scanner should map to a com port. This has typically been com 2 on the machines I've set it up on.
Download JustinIO. For my scanner, useful COM settings: Baud Rate: 9600, ByteSize 8, StopBits: 1 Parity: 0
Create a new CommPort instance. Set up a thread that does something like:
StringBuilder sb;
byte[] b;
do
{
b = commPort.Read(1);
sb.Append(Encoding.ASCII.GetChars(b));
Thread.Sleep(20); //symbol is slow...
} while(b.Length > 0);
Note: that's much crunched down from the code that I'm actually using so you'll need to modify it for your application.
Send the string to your application via whatever method seems appropriate.
The scanner is configured as HID (human interface device) and emulating keyboard.
To access it through Symbol's API, you probably have to disactivate current communication mode and set it to native one - should be documented in the scanner manual. Then you can access it directly without input field.
PS. I have no Symbol's scanner, but this is common to many devices.
It really depends upon the barcode scanner's api, and that is specific to the vendor. You'll need to get the the api for the scanner from the vendor, if you can't find it on their site you should just email their support, though you might find you have to buy the api.
First..
Ignore the .NET CF info for now as it probably pertains to the actual handheld unit. The mode of operation, pretending to be a keyboard, is referred to classically as a 'keyboard wedge'.
Second..
What kind of barcode symbologies are you attempting to use? And for what purposes? Or, mor directly,...What do the barcodes look like when scanned? (Do they contain special leading and trailing characters?)
Third... keep it simple... avoid the API unless it is necessary...
In the past when I've had to interface with barcode scanners, I've used a very simple approach in that my data entry form has the property of KeyPreview true and contains a method to detect and direct input originating from the scanner (by examining starting and trailing characters) to the appropriate control. Most of the scanners I've dealt with were programmable (usually via barcodes) to pass a custom set of characters to signal beginning and end of scanned input.
I hope that helps....
Here is a CodeProject sample that demonstrates how to set up global mouse and keyboard hooks in C#:
http://www.codeproject.com/KB/cs/globalhook.aspx
You could add this code to your console application, and then just monitor all keyboard input, looking for the specific codes you need. No API fuss or muss.

Categories

Resources