This commit is contained in:
GukSang.Jin
2026-01-06 19:55:29 +08:00
parent c9a55ecd6f
commit 32052ccec2

View File

@@ -506,10 +506,12 @@ namespace WindowsFormsApp6
// 读取初始重量D4202个寄存器浮点数单位g精度0.01g
ushort[] initialWeightReg = _modbusMaster.ReadHoldingRegisters(slaveId, 420, 2);
double initialWeight = ConvertRegistersToDouble(initialWeightReg);
initialWeight = ModbusUshortToFloat(initialWeightReg[1], initialWeightReg[0]);
// 读取浸润后重量D4222个寄存器浮点数单位g精度0.01g
ushort[] afterWeightReg = _modbusMaster.ReadHoldingRegisters(slaveId, 422, 2);
double afterWeight = ConvertRegistersToDouble(afterWeightReg);
afterWeight = ModbusUshortToFloat(afterWeightReg[1], afterWeightReg[0]);
// 读取浸润时间D4021个寄存器整数单位
ushort[] soakTimeReg = _modbusMaster.ReadHoldingRegisters(slaveId, 402, 1);
@@ -520,8 +522,9 @@ namespace WindowsFormsApp6
int hangTime = ConvertSingleRegisterToInt(hangTimeReg[0]);
// 读取运行速度D4101个寄存器整数单位mm/min
ushort[] runSpeedReg = _modbusMaster.ReadHoldingRegisters(slaveId, 410, 1);
int runSpeed = ConvertSingleRegisterToInt(runSpeedReg[0]);
ushort[] runSpeedReg = _modbusMaster.ReadHoldingRegisters(slaveId, 310, 2);
double runSpeed = ConvertRegistersToDouble(runSpeedReg);
runSpeed = ModbusUshortToFloat(runSpeedReg[1], runSpeedReg[0]);
// 使用反射获取Form2的私有字段
var sampleDataTableField = form2Instance.GetType()
@@ -552,7 +555,7 @@ namespace WindowsFormsApp6
AfterWeight = afterWeight,
SoakTime = soakTime,
HangTime = hangTime,
RunSpeed = runSpeed
RunSpeed = (int)runSpeed
});
// 新试样的索引
@@ -1111,7 +1114,7 @@ namespace WindowsFormsApp6
try
{
// 读取吸水时间D2001个寄存器整数单位
ushort[] timeRegisters = _modbusMaster.ReadHoldingRegisters(slaveId, 200, 1);
ushort[] timeRegisters = _modbusMaster.ReadHoldingRegisters(slaveId, 212, 2);
// 读取吸芯高度D4542个寄存器浮点数单位mm精度0.01mm
ushort[] heightRegisters = _modbusMaster.ReadHoldingRegisters(slaveId, 454, 2);
@@ -1144,11 +1147,11 @@ namespace WindowsFormsApp6
// 将寄存器值转换为实际数据
// 吸水时间:单个寄存器,整数,单位:秒
int wickingTime = ConvertSingleRegisterToInt(timeRegisters[0]);
int wickingTime = timeRegisters[0];
//wickingTime = ModbusUshortToInt(timeRegisters[1], timeRegisters[0]);
// 吸芯高度2个寄存器浮点数单位mm精度0.01mm
double wickingHeight = ConvertRegistersToDouble(heightRegisters);
wickingHeight = ModbusUshortToFloat(heightRegisters[1], heightRegisters[0]);
// 检查数据有效性
if (wickingTime <= 0 || double.IsNaN(wickingHeight) || double.IsInfinity(wickingHeight))
{
@@ -1265,6 +1268,39 @@ namespace WindowsFormsApp6
}
}
private float ModbusUshortToFloat(ushort highReg, ushort lowReg)
{
byte[] floatBytes = new byte[4];
// 高寄存器→高2字节低寄存器→低2字节与读取时的顺序一致
floatBytes[0] = (byte)(highReg >> 8);
floatBytes[1] = (byte)highReg;
floatBytes[2] = (byte)(lowReg >> 8);
floatBytes[3] = (byte)lowReg;
if (BitConverter.IsLittleEndian)
{
Array.Reverse(floatBytes);
}
return BitConverter.ToSingle(floatBytes, 0);
}
private int ModbusUshortToInt(ushort highReg, ushort lowReg)
{
byte[] floatBytes = new byte[4];
// 高寄存器→高2字节低寄存器→低2字节与读取时的顺序一致
floatBytes[0] = (byte)(highReg >> 8);
floatBytes[1] = (byte)highReg;
floatBytes[2] = (byte)(lowReg >> 8);
floatBytes[3] = (byte)lowReg;
if (BitConverter.IsLittleEndian)
{
Array.Reverse(floatBytes);
}
return BitConverter.ToInt32(floatBytes, 0);
}
/// <summary>
/// 获取Form3下一个要填充的位置试样索引和测试次数
/// 返回:(试样索引, 测试次数) - 都是1-based