简体   繁体   中英

Using an HID bar code scanner in visual basic .net

I am using visual basic 2019 and I need to use a bar code scanner as an HID device, not as a keyboard wedge. Is there a form control like the RS-232 serial port control that I can add to a form and place code in event handling routines to handle received data?

I'm not an expert in this particular area, but I did find a few classes that could be helpful to you:

Windows.Devices.HumanInterfaceDevice.HidDevice

and

System.IO.Ports.SerialPort

There are instructions and examples on the msdn pages. You should be able to create an instance of one of these classes and use it to communicate with your hardware.

There is a SerialPort control you can find in the toolbox. You will also need to include Imports System.IO.Ports in your form.

Then it's just a matter of configuring the port with calls such as:

    SerialPortScanner.BaudRate = "19200"
    SerialPortScanner.ReadTimeout = 5000
    SerialPortScanner.DataBits = 8
    SerialPortScanner.Parity = Parity.None
    SerialPortScanner.Open()
    SerialPortScanner.DiscardOutBuffer()    'start fresh
    SerialPortScanner.DiscardInBuffer()

Finally you read bytes with something like:

    CharBuff = SerialPortHCT.ReadByte()

Of course, you will need to loop on reading bytes and know when to stop (assuming you have to read them as bytes and don't have the luxury of having a scanner that sends delimited strings.

The tricky part is if you have to controller the scanner with non-printable characters. I have done this by making some constants like this:

    Const ACK As Byte = 6   ' ACKnowledge ASCII Code

Then you can write out the byte like this:

    SerialPortScanner.Write({ACK}, 0, 1) ' Send ACK byte

There are other settings and controls, but that's the basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM