[英]Why isn't my barcode scanner listed as a /dev/ device?
我正在做一个可以读取二维码的项目。 我购买了一个小型 USB 条形码二维码扫描仪,并将其连接到我的系统,我可以扫描二维码并将其发送到我正在编写的应用程序,但我必须知道 /dev/ 设备/端口名称,因为每次断开连接并重新连接时,它都可以更改分配给它的 /dev/ 设备/端口名称。
我曾尝试使用 pyusb,但它返回的只是设备信息。 我没有看到任何关于它代表哪个 /dev/ 的细节。
使用 PySerial:
import serial
from serial.tools import list_ports
for port in serial.tools.list_ports.comports():
if port.vid is not None and port.pid is not None:
# It's a USB port on a platform that supports the extended info
# Do something with it.
print("Port={},VID={:#06x},PID={:#06x}".format(port.device, port.vid, port.pid))
但这并没有看到连接到 /dev/ttys002 的条形码阅读器,这很奇怪,因为 PyUSB 可以看到它。 事实上,它只能看到另外两个设备(蓝牙传入端口和我的无线 airpods),但它不会打印这些设备,因为 port.vid 和 port.pid 对两者都是“none”。
使用 PyUSB:
import usb
import usb.backend.libusb1 as libusb1
import libusb1 as libusb
# find our device
dev = usb.core.find(idVendor=0xad93, idProduct=0x4002, backend=libusb1.get_backend())
print (dev)
输出:
DEVICE ID ad93:4002 on Bus 020 Address 038 =================
bLength : 0x12 (18 bytes)
bDescriptorType : 0x1 Device
bcdUSB : 0x110 USB 1.1
bDeviceClass : 0x0 Specified at interface
bDeviceSubClass : 0x0
bDeviceProtocol : 0x0
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0xad93
idProduct : 0x4002
bcdDevice : 0x100 Device 1.0
iManufacturer : 0x1 YK
iProduct : 0x2 YK-2D PRODUCT HID KBW
iSerialNumber : 0x3 APP-000000000
bNumConfigurations : 0x1
CONFIGURATION 1: 200 mA ==================================
bLength : 0x9 (9 bytes)
bDescriptorType : 0x2 Configuration
wTotalLength : 0x22 (34 bytes)
bNumInterfaces : 0x1
bConfigurationValue : 0x1
iConfiguration : 0x0
bmAttributes : 0x80 Bus Powered
bMaxPower : 0x64 (200 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 0x81: Interrupt IN ==========================
bLength : 0x7 (7 bytes)
bDescriptorType : 0x5 Endpoint
bEndpointAddress : 0x81 IN
bmAttributes : 0x3 Interrupt
wMaxPacketSize : 0x8 (8 bytes)
bInterval : 0x1
我想要做的就是将供应商 ID 和设备 ID 传递给一个函数,并让它返回它所连接的 /dev/tty 端口,这样我就可以设置一个线程来在扫描二维码时读取/捕获二维码。
这两天我一直在努力解决这个问题,我已经没有多少头发可以拔了!
哦,我正在使用 Mac OS Mojave 10.14.6 和 Python 3.7.4。
编辑:
我可以使用此代码读取二维码。
f=open("/dev/ttys002")
print (f.read())
第二个更新:我发现条形码扫描仪将自己表示为 HID 设备(键盘)。 我查看了 pyhid 和 libusb/hdiapi,我能够找到设备,但我不知道如何在 python 中读取它。 这是我正在使用的代码:
import hid
# get list of HID devices connected to this computer
h=hid.enumerate()
print ("HID info=", h,"\n")
# get path of first item
item=0
path=h[item]['path']
vid=h[item]['vendor_id']
pid=h[item]['product_id']
print ("Path=%s Vid=%s Pid=%s" % (path, vid, pid) + "/n")
# open the device
try:
# using path works on raw, not non-raw
# d=hid.Device(path=path)
# print "Opened device=", path
d=hid.Device(vid=vid, pid=pid)
print ("Opened device=", vid, pid, "\n")
# -- TBD
#r=d.get_feature_report(1, 1024)
#print "Report=", r
# loop rading the keyboard....
done=0
while(done==0):
r=d.read(10)
for c in r:
x=ord(c)
print ("read=",x,"\n")
except Exception as e:
print ("Exception, e=", e)
print ("Done!")
如果可以的话,我还将向其中添加 macos 和 hid 标签,因此也许其他可以提供帮助的人会看到这一点。
如果您的设备是 /dev/ttys002,它可能会出现在PySerial 中
import serial.tools.list_ports
for LPI in serial.tools.list_ports.comports():
print()
print('device: %s' % LPI.device)
print('name: %s' % LPI.name)
print('description: %s' % LPI.description)
print('hwid: %s' % LPI.hwid)
print('vid: %s' % LPI.vid)
print('pid: %s' % LPI.pid)
print('serial_number: %s' % LPI.serial_number)
print('location: %s' % LPI.location)
print('manufacturer: %s' % LPI.manufacturer)
print('product: %s' % LPI.product)
print('interface: %s' % LPI.interface)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.