Transfer floats from Arduino to Java

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jftheoret
 
Posts: 46
Joined: Thu Dec 30, 2010 3:59 pm

Transfer floats from Arduino to Java

Post by jftheoret »

I'm not sure if this is really an Arduino question or a C++ vs Java question, but here it goes...

I am transferring data from Arduino via serial port to a Java application on a Mac. I am using the Modbus protocol, and need to transfer floats. As the Modbus requires INTs, I have to split the float (32 bits) into two ints like this:

Code: Select all

void floatToInts(float f, int *msb, int *lsb) {
  byte byte_array[4];
  memcpy( byte_array, &f, sizeof(f) );
  memcpy( lsb, &byte_array[2], 2);
  memcpy( msb, &byte_array[0], 2);
}
At the other end, I receive my data correctly (the Ints). Example: if my float is 36.6, it gets split into two ints: 0x4212 as LSB 0x6666 as MSB. That is exactly what I read in my Java application.

However, I am having problems reassembling them into a float in Java.

I have found the Float.intBitsToFloat function, but I have not had any great success with it. Here is how I use it:

// LSB is in index 0 and MSB is in index 1
Float.intBitsToFloat(res.getRegisterValue(0) << 16 | res.getRegisterValue(1))

This works if my MSB is zero (in the case, for example, where I have a float of 36.5, I have LSB = 0x4212 and MSB = 0x000). However, if the MSB is non zero, it gives out junk...

Anybody ever encounter this and have a solution?

Thanks

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Transfer floats from Arduino to Java

Post by adafruit_support_bill »

Are you using Modbus protocol on the serial line between the Arduino and your Java app?

User avatar
jftheoret
 
Posts: 46
Joined: Thu Dec 30, 2010 3:59 pm

Re: Transfer floats from Arduino to Java

Post by jftheoret »

adafruit_support wrote:Are you using Modbus protocol on the serial line between the Arduino and your Java app?
Yes I am. It is Modbus Serial RTU from Arduino to Java. Arduino is the slave and Java is master.

User avatar
jftheoret
 
Posts: 46
Joined: Thu Dec 30, 2010 3:59 pm

Re: Transfer floats from Arduino to Java

Post by jftheoret »

But I think I just solved it.

I'm not sure I understand, but here's what seems to work:

Code: Select all

	public static float toFloat(int[] iv) {
		
		int i = iv[0] << 16 | iv[1];
		
		return Float.intBitsToFloat(i);
	}

// somewhere in my code:
			int[] datai = new int[] { res.getRegisterValue(0), res.getRegisterValue(1) };
			
			System.out.println("Converted value is" + toFloat(datai));
To me it seems identical to

Code: Select all

Float.intBitsToFloat(res.getRegisterValue(0) << 16 | res.getRegisterValue(1));
but there must be some sort of operator priority or data trunking...

User avatar
jftheoret
 
Posts: 46
Joined: Thu Dec 30, 2010 3:59 pm

Re: Transfer floats from Arduino to Java

Post by jftheoret »

Please forgive... (*kicks himself in the head*).

Operator priority. Bitwise OR has priority over shift. This works:

Code: Select all

Float.intBitsToFloat((res.getRegisterValue(0) << 16) | res.getRegisterValue(1));

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”