在 使用modbus tcp 协议读取plc 数据的时候会有大小端问题,大小端数据排列有四种情况:
1.Float Big-endian 字节顺序:A B C D
2. Float Little-endian 字节顺序:BADC
3. Float Big-endian byte swap 字节顺序:DCBA
4.Float Little-endian byte swap 字节顺序:CDAB
///ABCD
value = ModbusClient.ConvertRegistersToFloat(intArray, RegisterOrder.HighLow);
///CDAB
var value1 = ModbusClient.ConvertRegistersToFloat(intArray, RegisterOrder.LowHigh);
//BADC
if (intArray.Length != 2)
throw new ArgumentException("Input Array length invalid - Array langth must be '2'");
byte[] doubleBytes = {
lowRegisterBytes[1],
lowRegisterBytes[0],
highRegisterBytes[1],
highRegisterBytes[0]
};
var value2 = BitConverter.ToSingle(doubleBytes, 0);
//DCBA
if (intArray.Length != 2)
throw new ArgumentException("Input Array length invalid - Array langth must be '2'");
byte[] doubleBytes1 = {
highRegisterBytes[1],
highRegisterBytes[0],
lowRegisterBytes[1],
lowRegisterBytes[0]
};
var value3 = BitConverter.ToSingle(doubleBytes1, 0);
以上为方便自己查阅 所记录,