cython-hidapi,send_feature_report或chr()的问题

时间:2015-05-30 16:16:30

标签: python linux cython chr hidapi

我有这段代码可以在Windows和Mac OS X上使用Python 2.7.6正常工作。我正在使用cython-hidapi界面从Voltcraft VC870功率计读取数据。要获取数据,首先需要向设备发送功能报告,如下所示:

buf = [0x00, 0x80, 0x25, 0x00, 0x00, 0x03]
res = hid.device().send_feature_report(buf);

在Windows 8.1和Mac OS X Mavericks上,此工作正常,并返回非负值。但是,Linux上的相同代码返回负数。我相信这意味着设备无法理解已发送的功能报告。

调用hid(cython-hidapi)的代码如下:

def send_feature_report(self, buff):
  '''Accept a list of integers (0-255) and send them to the device'''
  # convert to bytes
  if sys.version_info < (3, 0):
      buff = ''.join(map(chr, buff))
  else:
      buff = bytes(buff)
  cdef hid_device * c_hid = self._c_hid
  cdef unsigned char* cbuff = buff # covert to c string
  cdef size_t c_buff_len = len(buff)
  cdef int result
  with nogil:
    result = hid_send_feature_report(c_hid, cbuff, c_buff_len)
  return result

我不熟悉C类型或字节级处理。我最初怀疑这条线:

  buff = ''.join(map(chr, buff))

是罪魁祸首,因为chr(0x80)在我的系统中输出不同的值。但是我想知道是否有人可以更清楚地了解这里的错误并建议修复。

0 个答案:

没有答案