2

I have this snippet of code which works fine on Windows and Mac OS X using Python 2.7.6. I am using the cython-hidapi intereface to read data from a Voltcraft VC870 power meter. To get the data, if first need to send a feature report to the device as thus:

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

On Windows 8.1 and Mac OS X Mavericks, this works fine and a non-negative value is returned. However, this same code on Linux returns a negative number. I believe this means the device did not understand the feature report as sent.

The code from hid (cython-hidapi) that is called is as follows:

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

I am not very familiar with C types or byte level processing. I initially suspected that the line:

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

was the culprit as chr(0x80) outputs out different values accross my systems. But I wonder if anyone can shed any more light as to what's wrong here and suggest a fix.

oche
  • 939
  • 10
  • 19
  • I've just started playing with cython-hidapi for testing purposes, and I think I know what your issue may be. The buff needs to start with the feature report id, which is the first parameter passed to get_feature_report, and I don't know that 0x00 is correct here. What exactly are you trying to do with this device? – hanetzer Jan 18 '16 at 06:01
  • You are right and thanks for commenting. I was later advised to use 0x05 for the first char, as the first char in the feature id has to be the length of the data packet. It was strange because the wrong char (0x00) worked on Windows but not on Linux. I was simply trying to connect to a serial device and retrieve some I'm not a hidapi expert and had adapted some code from an example I found onlineperiodically updated data from it. – oche Jan 18 '16 at 13:10
  • Sometimes 'worked' really didn't work. The first byte should be the report ID, and the remainder the actual data. – hanetzer Jan 18 '16 at 13:12

0 Answers0