Unfortunately my every day job has gotten most of my time lately and I haven’t been able to make much progress on anything. But today I had some time to play around with my new cubieboard based Fileserver and the old Microsoft Kinect I had laying around for a while now (I’ll write about that in the next post). And while I was playing around I got reminded of a very powerful Python library called numpy, which helped me solve a minor problem I still had with my TEMPer USB driver. Previously I had a problem with reading temperatures below zero, as the TEMPer protocol uses a signed integer as a datatype for the temperature and Python doesn’t have a native one Byte signed integer. But luckily numpy remedies the problem with its own datatypes, among them a one Byte signed integer (int8). So I shortly turned away from my Kinect/cubieboard endavour to fix the TEMPer driver. The solution is simple, I add numpy to my imports and convert the retrieved temperature value to numpys int8, so the final code looks like this:
import sys, os, time, usb, numpy VENDOR_ID = 0x0c45 PRODUCT_ID = 0x7401 INTERFACE1 = 0x00 INTERFACE2 = 0x01 reqIntLen=8 reqBulkLen=8 endpoint_Int_in=0x82 #/* endpoint 0x81 address for IN */ endpoint_Int_out=0x00 #/* endpoint 1 address for OUT */ uTemperatura = [ 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 ] uIni1 = [ 0x01, 0x82, 0x77, 0x01, 0x00, 0x00, 0x00, 0x00 ] uIni2 = [ 0x01, 0x86, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00 ] calibration = 0 def setup_libusb_access(): for bus in usb.busses(): for dev in bus.devices: if dev.idVendor == VENDOR_ID and dev.idProduct == PRODUCT_ID: print "Found Temper" return dev else: print "searching for Temper Devices..." print "no devices found" return None def ini_control_transfer(connection): question = [0x01, 0x01] # Detaching Kernel drivers on interfaces if any are present try: connection.detachKernelDriver(INTERFACE1) except: print "couldn't detach Kernel driver for interface1" try: connection.detachKernelDriver(INTERFACE2) except: print "couldn't detach Kernel driver for interface2" # claim interfaces try: connection.claimInterface(INTERFACE1) connection.claimInterface(INTERFACE2) except: print "couldn't claim one of the interfaces" r = connection.controlMsg(requestType=0x21, request=0x09, value=0x0201, index=INTERFACE1, buffer=question) if r < 0: print "Control Message failed" return False return True def control_transfer(connection, question): r = connection.controlMsg(requestType=0x21, request=0x09, value=0x0200, index=INTERFACE2, buffer=question) return def interrupt_read(connection): r = connection.interruptRead(endpoint_Int_in, reqIntLen) return def interrupt_read_temperature(connection): answer = connection.interruptRead(endpoint_Int_in, reqIntLen) #debug output print "read_temperature answer:" print answer tmp = numpy.int8(answer[2]) temperature = (answer[3] & 0xFF) + (tmp << 8) temperature += calibration tempC = temperature * (125.0 / 32000.0) return tempC def usb_release_interface(con, iface): try: con.releaseInterface() except: pass def usb_close(con): con.reset() def main(argv): temper = setup_libusb_access() if (temper == None): sys.exit(17) connection = temper.open() connection.reset() ini_control_transfer(connection) control_transfer(connection, uTemperatura ) interrupt_read(connection) control_transfer(connection, uIni1 ) interrupt_read(connection) control_transfer(connection, uIni2 ) interrupt_read(connection) interrupt_read(connection) while 1: try: control_transfer(connection, uTemperatura ) tempc = interrupt_read_temperature(connection) print time.time() print str(tempc)+" C" time.sleep(1) except KeyboardInterrupt: print "Execution Interrupted by User" break usb_release_interface(connection, INTERFACE1) usb_release_interface(connection, INTERFACE2) usb_close(connection) return 0 if __name__ == "__main__": main(sys.argv)
Notice the line that says:
tmp = numpy.int8(answer[2])
That’s all I needed to fix the temperature readout for negative values.
Now on to writing the next post…
Stunning story there. What occurred after? Take care!