首次提交lilili
This commit is contained in:
@ -304,7 +304,52 @@ namespace DH.Devices.PLC
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写单独地址 Dint 值- 待测试
|
||||
/// </summary>
|
||||
/// <param name="address"></param>
|
||||
/// <param name="writeValue"></param>
|
||||
/// <param name="waitForReply"></param>
|
||||
/// <returns></returns>
|
||||
public override bool WriteDInt1(string address, int writeValue, bool waitForReply = true)
|
||||
{
|
||||
// 1. 地址格式解析
|
||||
var match = Regex.Match(address, @"^([A-Za-z]+)(\d+)$");
|
||||
if (!match.Success)
|
||||
{
|
||||
Console.WriteLine($"地址格式错误: {address}");
|
||||
return false;
|
||||
}
|
||||
|
||||
string prefix = match.Groups[1].Value; // 提取字母部分(如"D"或"HD")
|
||||
int baseAddr = int.Parse(match.Groups[2].Value); // 提取数字部分(如100)
|
||||
|
||||
// 2. 数值拆分为两个16位值(大端序)
|
||||
byte[] bytes = BitConverter.GetBytes(writeValue);
|
||||
short high = BitConverter.ToInt16(bytes, 2); // 高16位
|
||||
short low = BitConverter.ToInt16(bytes, 0); // 低16位
|
||||
|
||||
// 3. 构造高低位地址
|
||||
string highAddress = $"{prefix}{baseAddr + 1}"; // 如"D101"或"HD101"
|
||||
string lowAddress = $"{prefix}{baseAddr}"; // 如"D100"或"HD100"
|
||||
|
||||
// 4. 写入操作(带重试)
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TcpNet.Write(highAddress, high).IsSuccess) continue;
|
||||
Thread.Sleep(10);
|
||||
return TcpNet.Write(lowAddress, low).IsSuccess;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"写入失败: {ex.Message}");
|
||||
Thread.Sleep(50 * (i + 1));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// 写单独地址 float 值
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user