简体   繁体   中英

c# communicating with an usb port

I'm trying to contact an usb barcode scanner as a serial port, using his virtual COM port name and the Serial Port class.

The reason I'm trying to do it this way is to avoid using the scanner as a keyboard wedge since I need the possibility to scan even when the form is hidden.

Problem is that when I connect the barcode scanner to the usb port, and I run the "GetPortNames" function, I get an empty list. Any suggestions why? Thanks

Typically many USB barcode scanners will just appear as virtual keyboard devices - not virtual COM ports. That would explain why it doesn't appear.

Does your scanner support some kind of serial emulation mode? You may need to configure it or install different drivers to enable this. Does it appear as a serial port in Device Manager? (Look under the Ports tree)

As an aside:

Take a look at this question if you find that the 'keyboard mode' is the only one supported: Reading a barcode using a USB barcode scanner along with ignoring keyboard data input while scanner product id and vendor id are not known

This question refers to this article with a large amount of good information relating to keyboard hooks and filtering out the scanned barcodes: http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/

I cannot guarantee that it will work with your specific device, but I've seen code that solved a similar problem once. The solution was looking for the port name in the registry.

The code for it can be found in the open source project ez430chronosnet ; a .NET library that can access TI's EZ430 Chronos watch via its USB dongle.

You'll want to look at the PortName.cs file that looks for the name in the registry (it will at least point you to the right keys) and the Chronos.cs file that does the actual port opening with SerialPort .

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