修改
This commit is contained in:
@@ -402,21 +402,21 @@ namespace CleanMachineHMI
|
||||
// ushort[] tmp_1 = new ushort[] { readBytArr_D[plcItemDeviationAddress], readBytArr_D[plcItemDeviationAddress + 1] };
|
||||
|
||||
#region 方法一 :Short数组转换为int数据 20201014 孙亚龙
|
||||
////低位 short 的字节数组。
|
||||
//byte[] tmpByt_1 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress]);
|
||||
////高位 short 的字节数组。
|
||||
//byte[] tmpByt_2 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress + 1]);
|
||||
////组合为int32的字节数组。
|
||||
// byte[] tmpByt_3 = new byte[] { tmpByt_1[0], tmpByt_1[1], tmpByt_2[0], tmpByt_2[1] };
|
||||
//低位 short 的字节数组。
|
||||
byte[] tmpByt_1 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress]);
|
||||
//高位 short 的字节数组。
|
||||
byte[] tmpByt_2 = BitConverter.GetBytes(readBytArr_D[plcItemDeviationAddress + 1]);
|
||||
//组合为int32的字节数组。
|
||||
byte[] tmpByt_3 = new byte[] { tmpByt_1[0], tmpByt_1[1], tmpByt_2[0], tmpByt_2[1] };
|
||||
|
||||
|
||||
////readBytArr_D[plcItemDeviationAddress + 1] << 16 +
|
||||
//value = BitConverter.ToInt32(tmpByt_3, 0).ToString();
|
||||
//readBytArr_D[plcItemDeviationAddress + 1] << 16 +
|
||||
value = BitConverter.ToInt32(tmpByt_3, 0).ToString();
|
||||
#endregion
|
||||
|
||||
#region 方法二 :Short数组转换为int数据 20201014 孙亚龙
|
||||
//相较而言 使用位移的算法效率更高一些。
|
||||
value = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
|
||||
// value = ((readBytArr_D[plcItemDeviationAddress + 1] << 16) + readBytArr_D[plcItemDeviationAddress]).ToString();
|
||||
|
||||
|
||||
#endregion
|
||||
@@ -1108,7 +1108,25 @@ namespace CleanMachineHMI
|
||||
{
|
||||
return false;
|
||||
}
|
||||
master.WriteSingleRegister(slaveAddress, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
|
||||
|
||||
|
||||
if (item_D.PlcItemDataLength == 2)
|
||||
{
|
||||
var _value = int.Parse(writeValue);
|
||||
var bytes = BitConverter.GetBytes(_value);
|
||||
var v1 = BitConverter.ToUInt16(bytes, 0);
|
||||
var v2 = BitConverter.ToUInt16(bytes, 2);
|
||||
master.WriteMultipleRegisters(slaveAddress, Convert.ToUInt16(item_D.PlcItemModbusAddress), new ushort[] { v1, v2 });
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
master.WriteSingleRegister(slaveAddress, Convert.ToUInt16(item_D.PlcItemModbusAddress), ushort.Parse(writeValue));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
case PlcItemTypeDesc.X:
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user