在Windows 10系统上使用扫描仪读取条形码QR码

时间:2018-12-20 20:48:25

标签: python-3.x usb barcode-scanner

我已通过USB将条形码/二维码扫描仪连接到Windows 10 pc。 扫描仪已在系统上注册。

现在,我想编写一个Python应用程序,以使用扫描仪读取条形码和二维码。

但是我无法阅读代码,结果得到一个对象。

我该怎么办,哪里出错了???

可以从扫描仪访问完整的参数。

#importing modules
import usb
import usb.core
import usb.util
import usb.backend.libusb1

#device vid / pid
VID = 0x2010 
PID = 0x7638

#access the device registry
backend = usb.backend.libusb1.get_backend(find_library=lambda X: "C:\ProgramData\Anaconda3\libusb\amd64")

#some tries to get parameters / informations about the device
usb_error = usb.USBError('USB-Error', error_code='error', errno=None)
dev = usb.core.find(idVendor=VID, idProduct=PID)
dev.set_configuration()
if not dev:
    raise ValueError ("USB device not found")
    exit(1)

config = dev.configurations
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep = usb.util.find_descriptor(
    intf,
    custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
ep_adr = ep.bEndpointAddress
dev.backend.open_device
adress = dev.address
manufaktur = dev.manufacturer
conf2 = dev.bNumConfigurations
dev_descriptor = dev.bDescriptorType
ser_num = dev.serial_number
busses = usb.busses()
for bus in busses:
    devices = bus.devices
    for dev in devices:
        print ("Device:", dev.filename)
        print ("  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor))
        print ("  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct))

print('cfg: ', str(cfg))
print('Manufacturer: ', str(manufaktur))
print('Serial Number', str(ser_num))
print('EP: ', ep)

print('Descriptor: ', dev_descriptor)

the actual result:

Device: 
  idVendor: 8208 (0x2010)
  idProduct: 30264 (0x7638)
cfg:    CONFIGURATION 1: 400 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x22 (34 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0 
   bmAttributes         :   0x80 Bus Powered
   bMaxPower            :   0xc8 (400 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x1
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x1
     bInterfaceProtocol :    0x1
     iInterface         :    0x0 
      ENDPOINT 0x83: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x8 (8 bytes)
       bInterval        :    0x1
Manufacturer:  USBKey Chip
Serial Number 202730041341
EP:        ENDPOINT 0x83: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :    0x8 (8 bytes)
       bInterval        :    0x1
Descriptor:  1

2 个答案:

答案 0 :(得分:0)

您获得的对象必须是带有结果扫描输出的字符串。尝试扫描到控制台...

您的扫描仪仅将USB键盘模拟为OUTPUT。 扫描仪内部产生的所有软件输出都是一串字节,这是扫描图像的结果。

您想要做的是使用libusb直接与USB硬件通话,这只会为您提供一个模拟的HID键盘。

如果要破解,则需要标记扫描仪板或扫描config images来触发设备上的配置更改。

答案 1 :(得分:0)

我通过 USB 连接的条码扫描器在 Windows 10 中的作用类似于标准输入设备(如键盘)。当在记事本中打开的文本文件上扫描条码时,扫描的数字会出现在文本文件中,就像在键盘上打字一样。

因此,为了在 Windows 10 中读入 Python39 shell 或文件,使用 python 代码“输入(“扫描条形码:”)”并进行扫描。我从扫描仪设备获得了扫描的数字。

C:\Project\Python39>python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>>
>>> input("scan Barcode: ")
scan Barcode: 88H-1010UB-0TV
'88H-1010UB-0TV'
>>>
>>> myBarCodeId = input("Scan barCode please ...")
Scan barCode please ...TARNLK002563
>>>
>>> print(myBarCodeId)
TARNLK002563
>>>
>>> mibar=input("scan me please \n")
scan me please
88H-1010UB-0TV
>>>
>>> print(mibar)
88H-1010UB-0TV
>>>