AdvancedHMI Software
General Category => Support Questions => Topic started by: EE_Kraig on March 07, 2017, 05:13:27 PM
-
If I set the Modbus driver like so:
SwapBytes = False
SwapWords = True
and I read a 32-bit real and convert like so:
string[] values = modbusTCPCom1.Read("30006", 2);
byte[] byteValue = new byte[4];
BitConverter.GetBytes(Convert.ToInt16(values[0])).CopyTo(byteValue, 0);
var floatValue = ConvertByteToFloat(byteValue);
public static float[] ConvertByteToFloat(byte[] array)
{
float[] floatArr = new float[array.Length / 4];
for (int i = 0; i < floatArr.Length; i++)
{
if (BitConverter.IsLittleEndian)
{
Array.Reverse(array, i * 4, 4);
}
floatArr[i] = BitConverter.ToSingle(array, i * 4);
}
return floatArr;
}
All is great. But, I have a couple of TIME variables that then get broken. If I swap the bytes and words settings, then the TIME vars are correct and the float is all messed up.
Can I have my cake and eat it too or am I going to have to manipulate one of them in code? Maybe a tweak in the Modbus driver?
-
Actually, the SwapBytes then messes up my HOA. So it looks like a driver change to how it handles Reals would be nice. Not too worried about the Time datatype since I should have made those Reals anyway.
Thanks!
-
Did you try reading you float like this:
modbusTCPCom1.Read("F30006")
-
Did you try reading you float like this:
modbusTCPCom1.Read("F30006")
10 = 0x41200000
SwapBytes=True
SwapWords=False
2.33625E-41
0x00004120
HOA works
SwapBytes=False
SwapWords=True
1.634774E-19
0x20410000
HOA broke
SwapBytes=True
SwapWords=True
10
0x41200000
HOA works
TIME works too.
Thanks!
-
Sorry to raise an old thread but...
how would one know to place an 'F' in front of the Modbus address to read as a float? Is there a document somewhere that shows this and maybe other switches?
Thanks
Eric
-
how would one know to place an 'F' in front of the Modbus address to read as a float? Is there a document somewhere that shows this and maybe other switches?
http://advancedhmi.com/documentation/index.php?title=ModbusRTUCom
http://advancedhmi.com/forum/index.php?topic=765.msg3647#msg3647