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?