using HslCommunication.ModBus; namespace DIOController { public class ModBusTcpController : IDIOController { public string Ip { get; set; } public int Port { get; set; } private ModbusTcpNet client; public bool Start(out string errMsg) { errMsg = string.Empty; client = new ModbusTcpNet(Ip, Port); var res = client.ConnectServer(); errMsg = res.Message; return res.IsSuccess; } public bool ReadDIBool(string address, out bool res, out string msg) { var result = client?.ReadBool($"x=2;{address}"); msg = result?.Message; res = result?.Content == true; return result?.IsSuccess == true; } public bool ReadDIBool(string address, int length, out bool[] res, out string msg) { var result = client?.ReadBool($"x=2;{address}", (ushort)length); msg = result?.Message; res = result?.Content; return result?.IsSuccess == true; } public bool ReadDOBool(string address, out bool res, out string msg) { var result = client?.ReadBool(address); msg = result?.Message; res = result?.Content == true; return result?.IsSuccess == true; } public bool ReadDOBool(string address, int length, out bool[] res, out string msg) { var result = client?.ReadBool(address, (ushort)length); msg = result?.Message; res = result?.Content; return result?.IsSuccess == true; } public bool WriteBool(string address, bool value, out string msg) { msg = string.Empty; var res = client?.Write(address, value); msg = res.Message; return res?.IsSuccess == true; } public bool WriteBool(string address, bool[] values, out string msg) { msg = string.Empty; var res = client?.Write(address, values); msg = res.Message; return res?.IsSuccess == true; } public bool Close() { return client?.ConnectClose()?.IsSuccess == true; } public void Dispose() { client?.Dispose(); } } }