I've downloaded the javahidapi and the source code. Running the sample program to list the connected devices gives me this:
Output
HIDDeviceInfo [path=USB_0c45_7401_0x7f8733426a10,
vendor_id=3141,
product_id=29697,
serial_number=,
release_number=1,
manufacturer_string=RDing,
product_string=TEMPer1V1.2,
usage_page=65280,
usage=1,
interface_number=-1]
HIDDeviceInfo [path=USB_0c45_7401_0x7f8733426c90,
vendor_id=3141,
product_id=29697,
serial_number=,
release_number=1,
manufacturer_string=RDing,
product_string=TEMPer1V1.2,
usage_page=1,
usage=6,
interface_number=-1]
From the information above I was able to extract the following constants into the Java code:
Java
static final int VENDOR_ID = 3141;
static final int PRODUCT_ID = 29697;
static final int BUFSIZE = 2048;
The main method of my Java class load the HID library from the classpath and call the readDevice() method.
Java
public static void main(String[] args) {
ClassPathLibraryLoader.loadNativeHIDLibrary();
readDevice();
}
The two methods below are ported from the code mentioned in the blog entry above. These are for converting the raw temperature reading to Celsius and another method to convert Celsius to Fahrenheit and Kelvin, depending on the parameters.
Java
static float raw_to_c(int rawtemp)
{
float temp_c = rawtemp * (125.f / 32000.f);
return temp_c;
}
static float c_to_u(float deg_c, char unit)
{
if (unit == 'F')
return (deg_c * 1.8f) + 32.f;
else if (unit == 'K')
return (deg_c + 273.15f);
else
return deg_c;
}
The method below does the actual reading of the device values. To read the device, a command is sent to the device first (by writing to the device), then the response is read.
Java
private static void readDevice()
{
HIDDevice dev;
try
{
HIDManager hid_mgr = HIDManager.getInstance();
dev = hid_mgr.openById(VENDOR_ID, PRODUCT_ID, null);
byte[] temp = new byte[] {
(byte)0x01, (byte)0x80, (byte)0x33, (byte)0x01,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
};
int res = dev.write(temp);
try
{
byte[] buf = new byte[BUFSIZE];
int n = dev.read(buf);
int rawtemp = (buf[3] & (byte)0xFF) + (buf[2] << 8);
if ((buf[2] & 0x80) != 0) {
/* return the negative of magnitude of the temperature */
rawtemp = -((rawtemp ^ 0xffff) + 1);
}
System.out.println("temp = " + c_to_u(raw_to_c(rawtemp), 'C'));
try
{
Thread.sleep(READ_UPDATE_DELAY_MS);
} catch(InterruptedException e)
{
e.printStackTrace();
}
} finally
{
dev.close();
hid_mgr.release();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
The code above only retrieves the temperature reading. The TEMPer1 can also report humidity readings, this is something I've not looked into yet. Running this code produces the following output:
Output
temp = 25.4375
As described in the blog entry I've referenced at the top, there are two HID devices exposed, these are returned in a non-deterministic order. My code doesn't make any attempt to grab the correct device, so sometimes if the 'keyboard' device is grabbed, the code locks up. To fix this, all that's requires is to enumerate all the devices and get the correct one based on the usage_page, but it's not something I've done.
-i