Monday, 15 April 2013

android - NFC logger battery voltage -


I have a data logger () and I try to get the battery level command (0xAA) as a result of trying to get the battery voltage But the result is wrong (negative number) Anyone knows what is the problem?

Mystic code:

  Public stable double CMDGetBattery level (tag tag, NFCV NFC bag) throws IOException {byte comGetBatLvl = new (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) Placeholder for 0x00, // UID tag; System ArrayCopy (tag.getid (), 0, comgatbeltll, 2, 8); Byte [] AnswerData = nfcvTag.transceive (comGetBatLvl); Int batCode = AnswerData [1]; // 3V battery double battel vl = (batcoda * 6.32) + 1.62; // 1.5V battery // double batail vl = (batcoda * 3.35) + 860; Comeback bat }   

There are two problems with your code:

  1. You are using the wrong workmanship between the obtained byte value and an integer value.

    Bytes in Java (as well as integer) are signed bytes. Although the logger treats though unsigned though the byte behaves. So when your byte value is in negative range (upper bit = 1), this signal bit will be extended when you assign a value to an integer variable. To put the byte value to your unsigned integer representation, you need to reduce the extended signature bits:

      int batcode = replyData [1] & amp; 0x0FF;  
  2. The other problem is that you are the units used in your formula. The value of 6.32 millilent is, while the value is 1.62 volt. Therefore, you should use

      Double Battel vl = (6.32d * batcode / 1000.0d) + 1.62d;  

    or

      Double Battel vl = (6.32d * batcode) + 1620.0d to get value in Voltage; 


No comments:

Post a Comment