|
|
@@ -250,79 +250,7 @@ namespace BPASmartClient.Modbus |
|
|
|
return default(object); |
|
|
|
} |
|
|
|
|
|
|
|
#region 180项目调用 |
|
|
|
public int GetAddress(string address, string target) |
|
|
|
{ |
|
|
|
if (address == null) return -1; |
|
|
|
if (address.Length > 0) |
|
|
|
{ |
|
|
|
if (address.ToUpper().Contains("D") && address.Length == 5) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
string head = "4" + (Convert.ToInt32(address.Substring(1, 1)) - 1).ToString(); |
|
|
|
string tail = address.Substring(2, 3); |
|
|
|
address = head + tail; |
|
|
|
return Convert.ToInt32(address); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
//打印日志 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return -1; |
|
|
|
} |
|
|
|
public object Read(string address, ushort len, string target, byte slaveAddress = 1) |
|
|
|
{ |
|
|
|
if (address == null || tcpClient == null) return default(object); |
|
|
|
ushort startAddress = (ushort)GetAddress(address, target); |
|
|
|
CommandType commandType = CommandType.HoldingRegisters; |
|
|
|
try |
|
|
|
{ |
|
|
|
if (address.ToUpper().Contains("D")) |
|
|
|
{ |
|
|
|
commandType = CommandType.HoldingRegisters; |
|
|
|
return master.ReadHoldingRegisters(slaveAddress, startAddress, len); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
MessageLog.GetInstance.ShowEx($"读取地址:【{address}:= {startAddress}】,读取类型:【{commandType.ToString()}】出错,{ex.ToString()}"); |
|
|
|
ExceptionHandling(ex); |
|
|
|
} |
|
|
|
return default(object); |
|
|
|
} |
|
|
|
|
|
|
|
public void Write<T>(string address, T value, string target, byte slaveAddress = 1) |
|
|
|
{ |
|
|
|
if (address == null || tcpClient == null) return; |
|
|
|
ushort startAddress = (ushort)GetAddress(address, target); |
|
|
|
CommandType commandType = CommandType.Coils; |
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
if (address.ToUpper().Contains("D")) |
|
|
|
{ |
|
|
|
commandType = CommandType.HoldingRegisters; |
|
|
|
if (value is ushort ushortValue) |
|
|
|
{ |
|
|
|
master.WriteSingleRegister(slaveAddress, startAddress, ushortValue); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
MessageLog.GetInstance.ShowEx($"写入地址:【{address}:= {startAddress}】,写入类型:【{commandType.ToString()}】出错,{ex.ToString()}"); |
|
|
|
ExceptionHandling(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Write<T>(string address, T value, byte slaveAddress = 1) |
|
|
|