How to read temperature on usb device with HIDAPI?

1.5k views Asked by At

I have to recover the value of temperature from a usb device TEMPer and I found the code on this blogpost:

import java.io.IOException;

import org.apache.log4j.Logger;

import com.codeminders.hidapi.HIDDevice;
import com.codeminders.hidapi.HIDManager;


public class Foo {
 static final int VENDOR_ID = 3141;
 static final int PRODUCT_ID = 29697;
 static final int BUFSIZE = 2048;
 static final long READ_UPDATE_DELAY_MS = 1000;
 public static String myTemperature = "0.00";
 private static Logger log = Logger.getLogger(Foo.class.getName());

static float raw_to_c(final int rawtemp) {
 final float temp_c = rawtemp * (125.f / 32000.f);
 return temp_c;
}

static float c_to_u(final float deg_c, final 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;
  }
}

public static void readDevice(final int vendorId, final int productId) {

  System.out.println("In readDevice");
  final HIDDevice dev;

  try {
    final HIDManager hid_mgr = HIDManager.getInstance();
    log.debug("vendorId =" +vendorId + " productID =" + productId);
    dev = hid_mgr.openById(vendorId, productId, null);      



    final byte[] temp = new byte[] { (byte) 0x01, (byte) 0x80, (byte) 0x33, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };

    final int res = dev.write(temp);

    try {
      final byte[] buf = new byte[BUFSIZE];
      final 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'));
      myTemperature = Float.toString( c_to_u(raw_to_c(rawtemp), 'C'));

      try {
        Thread.sleep(READ_UPDATE_DELAY_MS);
      } catch (final InterruptedException e) {
        e.printStackTrace();
      }
    } finally {
      dev.close();
      hid_mgr.release();
    }
  } catch (final IOException e) {
    e.printStackTrace();
  }

}

}

My class returns this error code:

com.codeminders.hidapi.HIDDeviceNotFoundException at com.codeminders.hidapi.HIDManager.openById(HIDManager.java:114) at Foo.readTemperature(Foo.java:44) at StartApplication.main(StartApplication.java:96)

I have verified if my usb probe is a HID device and yes it is.

Solution found! ==> It's my Linux which doesn't saw usb devices others than a classic storage key. So now it's working!

I just have now a second problem, in this code I never pass after the

 int n = dev.read(buf);

And this line is essential to have a good result. And operations with bytes aren't really easy for me...

Solution n°2 found! ==> All I need is to add the following line before the write and the read:

dev.disableBlocking();

Last problem I think: I don't read any bytes when I do this:

int n = dev.read(buf);

n is all the time equals to 0...

0

There are 0 answers