Thursday 23 February 2017

RDING Temper in Python on Windows



I got this device for a few bucks on eBay:
http://www.ebay.ca/itm/USB-Thermometer-Sensor-Temperature-Data-Logger-Tester-For-PC-Laptop-Mac-Computer-/272558575206?hash=item3f75c1ce66:g:gugAAOxy8QZST4PN

And it seems to work decently... It came with really awful windows software and the only real way to get data from it (short of using the HID keyboard typing excel) was to get their pretty awful software to write out a CSV every second and then compile and delete the data files it made... The software would also do weird things, crash, and leave weird dialogs on the screen...  I built a program to deal with most of these issues until I had a chance to get the data from USB properly.

I found info on using pywinusb.hid to grab info from HID devices and I also found this module which did not seem to work... I think it's meant for Linux, I did not troubleshoot it very much..

As I was playing with the pywinusb raw example I found out that it was able to get temperature readings while the temper application was running, so I decided to see if I could make this work, and I did...

Here is the code if anyone wants to read the temperature from your module.

This is my first time doing any direct USB stuff so it may not be perfect, but it seems to work... If you have any suggestions leave them in the comments.

======================= temper.py ===========================
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
"""
Handling raw data inputs example
"""
from time import sleep
from msvcrt import kbhit

import pywinusb.hid as hid

def sample_handler(data):
    # For debugging purposes or getting data from other sensors
    #print("Raw data: {0}".format(data))
    temp = str(data).split(',')
    temp = int(temp[3]) + int(temp[4])/256.0
    temperature = round(temp,2)
    print temperature
    # You can put your own code here to do whatever you want with the temp readings!



def raw_test():
    # locate the proper device based on it's Vendor and Product ID
    temper = hid.HidDeviceFilter(vendor_id = 0x0c45, product_id = 0x7401).get_devices()
 
    # Only continue if the device is found, otherwise return an error below
    if temper:
        while True:
        device = temper[0]
        try:
            device.open()
            #Setup the callback for when data comes in from the device
            device.set_raw_data_handler(sample_handler)
            # Keep going at this main loop until the device is unplugged
            while device.is_plugged():
                # Keep device open and rx new events...
                # We have to make a request for the temperature from the device
                report = device.find_output_reports()[ 0 ]
                # This is the command to request temperature
                report[ 0xff000001 ].value = 0x01,0x80,0x33,0x01,0x00,0x00,0x00,0x00
                # Send the command
                report.send()

                # Wait this long between requests... You can make this shorter or longer based on your needs
                sleep(1)
            return
        finally:
            # close the device if it's been removed
            device.close()
    else:
        # The device wasn't found, return an error and exit
        print("Can't find a Temper device")
#
if __name__ == '__main__':
    # first be kind with local encodings
    import sys
    if sys.version_info >= (3,):
        # as is, don't handle unicodes
        unicode = str
        raw_input = input
    else:
        # allow to show encoded strings
        import codecs
        sys.stdout = codecs.getwriter('mbcs')(sys.stdout)
# Start the main loop/listener
raw_test()
=================================== SNIP ==================

1 comment:

  1. Thank you for the info. I think we will try it for one of our clients.

    Paul Quinn
    Planet Access
    pquinn@placs.net
    817-829-0418

    ReplyDelete