【发布时间】:2015-04-25 16:19:48
【问题描述】:
好消息是这款便宜的厦门 ELANE.NET 称重传感器通过 USB 通电进入报告 3 模式;不断地以克为单位吐出它当前的重量。
这是它的数据表:
http://www.elane.net/USBscales/List_USB_Digital_Load_Cell_Commands_and_Data_Format_User.pdf
我可以通过标准的pyusb 调用来阅读它。这个样本可以读取刻度...
http://www.orangecoat.com/how-to/read-and-decode-data-from-your-mouse-using-this-pyusb-hack
...如果您将设备查找替换为usb.core.find(idVendor=0x7b7c, idProduct=0x301)
(我还滥用sudo 来运行我的程序,因为我拒绝使用设备权限,而sudo 在Raspberry Pi 上很容易。)
使用标准的pyusb 调用,我可以像这样读取我的体重秤的喷射:
device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
返回一个 6 字节数组:
+--------------------- Report type
| +------------------ Weight Stable (tray not moving)
| | +--------------- grams (not pounds)
| | | +------------ whatever
| | | | +--------- 2 grams
| | | | | +------ 0 x 256 grams
| | | | | |
V V V V V V
[3, 4, 2, 0, 2, 0]
现在,当我尝试向体重秤发送命令时,乐趣就开始了。将当前重量(零重量,又名“去皮”)归零的命令可能是 7 4 2 0 0 0。
如果我使用像 https://github.com/walac/pyusb/blob/master/docs/tutorial.rst 这样的示例代码来查找 ENDPOINT_OUT 端点,并使用这些行中的任何一行写入它,我就无法去皮:
# ep_out.write('\x07\x04\x02\x00\x00\x00', 6)
ep_out.write([0x07, 0x04, 0x02, 0x00, 0x00, 0x00], 6)
(症状是,我可以在我的称重传感器上放一个负载,用上面的.read()线称重,然后去皮,然后当我再次.read()时不会得到零。)
好吧,我们还没死。我们还没有尝试过任何 HIDAPI。所以我apt-get我一些libusbhid-common,一些cython-dev,一些libusb-dev,一些libusb-1.0.0-dev,还有一些libudev-dev,我升级了HIDAPI C示例代码来尝试去皮重:
handle = hid_open(0x7b7c, 0x301, NULL);
buf[0] = 0x07;
buf[1] = 0x04;
buf[2] = 0x02;
res = hid_write(handle, buf, 3);
那是稗子。
为了复制我在 Python 中的一次成功(尽管用 C++ 重写我的应用程序的一小层是多么诱人!),我推出了一些 Cython-hidapi(大概来自 git://github.com/signal11/hidapi.git),并升级了他们的 try.py 示例代码:
h = hid.device()
h.open(0x7b7c, 0x301)
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
res = h.write([0x07, 0x04, 0x02, 0,0,0])
你猜怎么着?最后一行不去皮。但是如果我运行它 3 次,它确实会去皮!
res = h.write([0x07, 0x04, 0x02, 0,0,0])
res = h.write([0x07, 0x04, 0x02, 0,0,0])
res = h.write([0x07, 0x04, 0x02, 0,0,0])
所以,在我编写一个循环调用皮重线直到读取返回零级之前,有人可以检查我的数学并建议一个捷径吗?原始的 pyusb 解决方案也可以很好地工作。
【问题讨论】: