将浮点数转换为 dart 中的 modbus 寄存器
Convert float to modbus registers in dart
有人帮忙将此方法转换为 Dart 代码吗?我需要将浮点数转换为 Modbus 寄存器。我有这个easymodbus的方法class:
public static int[] ConvertFloatToRegisters(float floatValue)
{
byte[] floatBytes = BitConverter.GetBytes(floatValue);
byte[] highRegisterBytes =
{
floatBytes[2],
floatBytes[3],
0,
0
};
byte[] lowRegisterBytes =
{
floatBytes[0],
floatBytes[1],
0,
0
};
int[] returnValue =
{
BitConverter.ToInt32(lowRegisterBytes,0),
BitConverter.ToInt32(highRegisterBytes,0)
};
return returnValue;
}
此代码应与您的 C# 示例执行相同的操作,但在 Dart 中:
import 'dart:typed_data';
void main() {
print(convertFloatToRegisters(43554.975)); // [8954, 18218]
}
Int32List convertFloatToRegisters(double floatValue) {
final floatBytes = ByteData(4)..setFloat32(0, floatValue, Endian.little);
final highRegisterBytes = ByteData(4)
..setUint8(0, floatBytes.getUint8(2))
..setUint8(1, floatBytes.getUint8(3));
final lowRegisterBytes = ByteData(4)
..setUint8(0, floatBytes.getUint8(0))
..setUint8(1, floatBytes.getUint8(1));
final returnValue = Int32List(2);
returnValue[0] = lowRegisterBytes.getInt32(0, Endian.little);
returnValue[1] = highRegisterBytes.getInt32(0, Endian.little);
return returnValue;
}
根据代码的作用,我认为可以简化为:
import 'dart:typed_data';
void main() {
print(convertFloatToRegisters(43554.975));
}
Int32List convertFloatToRegisters(double floatValue) {
final floatBytes = ByteData(4)..setFloat32(0, floatValue);
final returnValue = Int32List(2);
returnValue[0] = floatBytes.getUint16(2);
returnValue[1] = floatBytes.getUint16(0);
return returnValue;
}
针对稍后发布的双示例进行了更新
import 'dart:typed_data';
void main() {
print(convertDoubleToRegisters(43554.975)); // [13107, 13107, 17503, 16613]
}
Int32List convertDoubleToRegisters(double doubleValue) {
final doubleBytes = ByteData(8)..setFloat64(0, doubleValue);
final returnValue = Int32List(4);
returnValue[0] = doubleBytes.getUint16(6);
returnValue[1] = doubleBytes.getUint16(4);
returnValue[2] = doubleBytes.getUint16(2);
returnValue[3] = doubleBytes.getUint16(0);
return returnValue;
}
那这个呢:
/// <summary>
/// Converts 64 Bit double prec Value to four ModbusRegisters
/// </summary>
/// <param name="doubleValue">double value which has to be converted into four registers</param>
/// <returns>Register values</returns>
public static int[] ConvertDoubleToRegisters(double doubleValue)
{
byte[] doubleBytes = BitConverter.GetBytes(doubleValue);
byte[] highRegisterBytes =
{
doubleBytes[6],
doubleBytes[7],
0,
0
};
byte[] highLowRegisterBytes =
{
doubleBytes[4],
doubleBytes[5],
0,
0
};
byte[] lowHighRegisterBytes =
{
doubleBytes[2],
doubleBytes[3],
0,
0
};
byte[] lowRegisterBytes =
{
doubleBytes[0],
doubleBytes[1],
0,
0
};
int[] returnValue =
{
BitConverter.ToInt32(lowRegisterBytes,0),
BitConverter.ToInt32(lowHighRegisterBytes,0),
BitConverter.ToInt32(highLowRegisterBytes,0),
BitConverter.ToInt32(highRegisterBytes,0)
};
return returnValue;
}
有人帮忙将此方法转换为 Dart 代码吗?我需要将浮点数转换为 Modbus 寄存器。我有这个easymodbus的方法class:
public static int[] ConvertFloatToRegisters(float floatValue)
{
byte[] floatBytes = BitConverter.GetBytes(floatValue);
byte[] highRegisterBytes =
{
floatBytes[2],
floatBytes[3],
0,
0
};
byte[] lowRegisterBytes =
{
floatBytes[0],
floatBytes[1],
0,
0
};
int[] returnValue =
{
BitConverter.ToInt32(lowRegisterBytes,0),
BitConverter.ToInt32(highRegisterBytes,0)
};
return returnValue;
}
此代码应与您的 C# 示例执行相同的操作,但在 Dart 中:
import 'dart:typed_data';
void main() {
print(convertFloatToRegisters(43554.975)); // [8954, 18218]
}
Int32List convertFloatToRegisters(double floatValue) {
final floatBytes = ByteData(4)..setFloat32(0, floatValue, Endian.little);
final highRegisterBytes = ByteData(4)
..setUint8(0, floatBytes.getUint8(2))
..setUint8(1, floatBytes.getUint8(3));
final lowRegisterBytes = ByteData(4)
..setUint8(0, floatBytes.getUint8(0))
..setUint8(1, floatBytes.getUint8(1));
final returnValue = Int32List(2);
returnValue[0] = lowRegisterBytes.getInt32(0, Endian.little);
returnValue[1] = highRegisterBytes.getInt32(0, Endian.little);
return returnValue;
}
根据代码的作用,我认为可以简化为:
import 'dart:typed_data';
void main() {
print(convertFloatToRegisters(43554.975));
}
Int32List convertFloatToRegisters(double floatValue) {
final floatBytes = ByteData(4)..setFloat32(0, floatValue);
final returnValue = Int32List(2);
returnValue[0] = floatBytes.getUint16(2);
returnValue[1] = floatBytes.getUint16(0);
return returnValue;
}
针对稍后发布的双示例进行了更新
import 'dart:typed_data';
void main() {
print(convertDoubleToRegisters(43554.975)); // [13107, 13107, 17503, 16613]
}
Int32List convertDoubleToRegisters(double doubleValue) {
final doubleBytes = ByteData(8)..setFloat64(0, doubleValue);
final returnValue = Int32List(4);
returnValue[0] = doubleBytes.getUint16(6);
returnValue[1] = doubleBytes.getUint16(4);
returnValue[2] = doubleBytes.getUint16(2);
returnValue[3] = doubleBytes.getUint16(0);
return returnValue;
}
那这个呢:
/// <summary>
/// Converts 64 Bit double prec Value to four ModbusRegisters
/// </summary>
/// <param name="doubleValue">double value which has to be converted into four registers</param>
/// <returns>Register values</returns>
public static int[] ConvertDoubleToRegisters(double doubleValue)
{
byte[] doubleBytes = BitConverter.GetBytes(doubleValue);
byte[] highRegisterBytes =
{
doubleBytes[6],
doubleBytes[7],
0,
0
};
byte[] highLowRegisterBytes =
{
doubleBytes[4],
doubleBytes[5],
0,
0
};
byte[] lowHighRegisterBytes =
{
doubleBytes[2],
doubleBytes[3],
0,
0
};
byte[] lowRegisterBytes =
{
doubleBytes[0],
doubleBytes[1],
0,
0
};
int[] returnValue =
{
BitConverter.ToInt32(lowRegisterBytes,0),
BitConverter.ToInt32(lowHighRegisterBytes,0),
BitConverter.ToInt32(highLowRegisterBytes,0),
BitConverter.ToInt32(highRegisterBytes,0)
};
return returnValue;
}