C# Barcode scanner

24,245

Solution 1

Richard,

It is important to know that typically barcode scanners support multiple interfaces that fall into two categories. Many have an option that makes the barcode scanner appear as a keyboard and whenever you scan data the text is entered into your application at the insertion point. The manufacturer of the scanner may refer to this as Keyboard, Keyboard Wedge, HID Keyboard or simply HID mode, however the last one in this list is technically not accurate as there are other HID interfaces besides keyboard.

The second category is often referred to as application mode. There are several different interfaces that support application mode, such as IBM Scanner, HID POS Scanner, etc. Each of these interfaces represent follow a specific hardware specification. You must make sure that the mode that your scanner is in matches the SDK that you are using to interact with the scanner.

If you are using .NET Framework, you may find POS for .NET useful as it abstracts the barcodes scanner away from the software in a way that allows you to use scanners from multiple manufacturers without changes in your application. In this case, you will need to acquire an OPOS Service Object from the scanner manufacturer to use with POS for .NET. See POS for .NET 1.14.1 Download page for more information: https://www.microsoft.com/en-us/download/details.aspx?id=55758

Terry Warwick Microsoft

Solution 2

If you're just starting out, samples within SDKs are the best place to get started.

UWP apps to handle barcode are best explained in the universal samples at --> https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/BarcodeScanner

A Win32 C# .net sample can be found in the Pos For .Net 1.14 SDK mentioned earlier.

Solution 3

Most barcode scanners are "HID" devices, which means that they write the data of the barcode (the small numbers) like you would do manually with your keyboard, they're also recognized as a keyboard by most operating systems. So the easiest way is just having a textbox. Make sure the focus is automatically on the textbox before the scan, and if you want it to automatically do something, make sure to have an event listening for an enter keydown. (Most HID scanners press an enter right after the scan is complete.)

Solution 4

It also depends on what form your application takes. If you have the option of uploading a picture or accessing the camera, you can pull an image into your code and then use one of many SDK's to read the barcode out of the image.

I have just implemented this using a web application, and I used the ZXing SDK, which is a free port to .Net and is available via NuGet.

https://github.com/micjahn/ZXing.Net

Solution 5

Use the class SerialPort. It can listen your ports and then when you will use your scan the program will read it.

 while (spPort.BytesToRead > 0)
 {
     carac = (char)spPort.ReadByte();

     if (carac != 08)
         m_mystring += carac;
 }

Here is an example of how you can read it. And this is the link to the class : https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx

Share:
24,245
Richard Teunen
Author by

Richard Teunen

Updated on March 21, 2022

Comments

  • Richard Teunen
    Richard Teunen about 2 years

    I am currently writing a program where I wish to use a barcode scanner to scan a bar code into a system, then use this information to make certain decisions.

    How can I get C# to react when I use the bar code scanner? Do I need certain DLLs or APIs to use a bar code reader? I can create bar codes but need a way for C# to read them externally and import them into the program.

    • Mohammad
      Mohammad almost 6 years
      a barcode scanner will automaticly pass data to your program when you scan a barcode , so if there is a textbox , when you scan it , barcode will be parsed into that textbox.
    • Richard Teunen
      Richard Teunen almost 6 years
      Are there any special references i have to make in my program or can i simply plug the bar code scanner in to my laptop? sorry for the many questions just want to make sure i have everything i need
    • Mohammad
      Mohammad almost 6 years
      you don't even need any reference or api , if scanner is connected to the laptop or any other device ,it will work automatically.
    • hatchet - done with SOverflow
      hatchet - done with SOverflow almost 6 years
      The scanner should act like a keyboard (that types the scan code). See stackoverflow.com/questions/615036/…
    • EdSF
      EdSF almost 6 years
      Typically barcodes represent something, perhaps a UPC/GTIN/EAN, etc., which end of day is alphanumeric data. In such cases think of a scanner as "some input device" (much like a keyboard). So, as above, if it's a text box, a text file, etc., it will "type" the data into it.
  • Mohammad
    Mohammad almost 6 years
    @RichardTeunen it would great if you set my answer to correct if it answered your question correctly :)
  • Jan Macháček
    Jan Macháček almost 3 years
    Doesn't work for scanner not set to emulate keyboard, but to work with COM port.