HID上位机开发笔记

[复制链接]
 楼主| gaoyang9992006 发表于 2022-11-4 14:27 | 显示全部楼层 |阅读模式
最近参与做了一个扫码设备的上位机软件。
扫码器扫码后读取到数据提交给服务器写入数据库。
那么HID上位机的服务软件该如何写呢?
这里使用Python实现。
相关的资料链接
  1. https://pypi.org/project/hidapi
  2. https://trezor.github.io/cython-hidapi/api.html#device-class
  3. https://trezor.github.io/cython-hidapi/genindex.html
通过PIP的安装方式
  1. pip install hidapi
示例
功能:查找设备,列出所有设备的所有信息

  1. import hid

  2. for device_dict in hid.enumerate():
  3.     keys = list(device_dict.keys())
  4.     keys.sort()
  5.     for key in keys:
  6.         print("%s : %s" % (key, device_dict[key]))
  7.     print()
示例
功能:链接设备、读写操作
  1. try:
  2.     print("Opening the device")

  3.     h = hid.device()
  4.     h.open(0x534C, 0x0001)  # TREZOR VendorID/ProductID

  5.     print("Manufacturer: %s" % h.get_manufacturer_string())
  6.     print("Product: %s" % h.get_product_string())
  7.     print("Serial No: %s" % h.get_serial_number_string())

  8.     # enable non-blocking mode
  9.     h.set_nonblocking(1)

  10.     # write some data to the device
  11.     print("Write the data")
  12.     h.write([0, 63, 35, 35] + [0] * 61)

  13.     # wait
  14.     time.sleep(0.05)

  15.     # read back the answer
  16.     print("Read the data")
  17.     while True:
  18.         d = h.read(64)
  19.         if d:
  20.             print(d)
  21.         else:
  22.             break

  23.     print("Closing the device")
  24.     h.close()

  25. except IOError as ex:
  26.     print(ex)
  27.     print("You probably don't have the hard-coded device.")
  28.     print("Update the h.open() line in this script with the one")
  29.     print("from the enumeration list output above and try again.")

  30. print("Done")




您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2046

主题

16356

帖子

221

粉丝
快速回复 在线客服 返回列表 返回顶部