Search code examples
c#wpfqr-codebarcode-scanner

QR Code Scanner Integration with C# WPF


I am writing one WPF application(MVVM) which needs to read data from QR Code Images using QR Code Scanner(Model: Symbol DS4308 - USB to connect with desktop). I googled for the same and identified that we can read QR Data using Textbox and it works. But my application won't have a textbox to capture the QR Code data but I need to get the scanner to provide the Event once scanning is done.

I tried with notepad and scanner writes the scanned values to it when scanning QR Code.

Is there any generic open source libraries to get the scanner events?


Solution

  • I haven't worked with the 4308 specifically, but I program Symbol scanners at work so I'm familiar with them.

    First of all, forget about keyboard mode. It's handy for scanning directly into documents etc but it sounds like you want to read the scanner directly. If so, go to the DS4308 data sheet and scan in the barcode in section 6-6 for "Simple COM Port Emulation". This will cause the scanner to now appear as a serial device, which you can then read with the C# SerialPort class:

    this.Scanner = new SerialPort(this.SymbolPort);
    this.Scanner.BaudRate = 9600;
    this.Scanner.DataReceived += Scanner_DataReceived;
    this.Scanner.Open();
    .
    .
    .
    private void Scanner_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        var len = this.Scanner.BytesToRead;
        var bytes = new byte[len];
        this.Scanner.Read(bytes, 0, len);
        var str = Encoding.ASCII.GetString(bytes);
        // do something with str here
    }
    

    If you want to make your app really user friendly then you can also determine which COM port to use programatically using the device PID and VID. To get the PID/VID just go to device manager and check the properties for the device. To find the COM port use the code on this SO post, although that will search all devices installed on the system irrespective of whether or not they're actually plugged in so you'll want to cross-check it against SerialPort.GetPortNames().