help float converting

  • Thread starter Thread starter Yasin Cepeci
  • Start date Start date
Y

Yasin Cepeci

I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
 
Yasin,

Unfortunately, there is nothing in the framework that will help you with
this. I would say use the BitConverter class, but it will not convert the
byte array using the modbus protocol. It uses the IEEE format for floating
point numbers.

You will have to write this yourself.

Hope this helps.
 
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 
O I See but How can I turn the converting proccess below reverse. in other
words convert to float from hex or binary(32bit).
10101010101011111101101010101010--->xxx.xxxxxF; to float value:)





haber iletisinde sunlari said:
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 
Yasin said:
O I See but How can I turn the converting proccess below reverse. in other
words convert to float from hex or binary(32bit).
10101010101011111101101010101010--->xxx.xxxxxF; to float value:)

Us the BitConverter.ToString method to convert four bytes of a byte
array into a float value.
haber iletisinde sunlari said:
I ve get float data from serial port. I ve taken it in the form of hex by
modbus protocol. I know it is float but I couldnt convert it
there is a few sample data below;

B3 33 43 34 = 180.699997
33 33 43 33 = 179.199997
B3 33 43 34= 180.699997
CC CD 43 33=179.800003

But how can I found it. I couldnt resolve it.
It looks as if you just need to reorder the bytes and cast to float.

180.70000 -> 33-B3-34-43
179.20000 -> 33-33-33-43
180.70000 -> 33-B3-34-43
179.80000 -> CD-CC-33-43

Code used:

static void Main() {

float[] test = { 180.699997F,
179.199997F,
180.699997F,
179.800003F };
foreach (float ff in test) {
byte[] bytes = BitConverter.GetBytes(ff);
Console.WriteLine("{0:F5} -> {1}",
ff, BitConverter.ToString(bytes));
}
} // end Main()

rossum
 
Back
Top