using HslCommunication.ModBus; namespace Plugin.DODriverControl.Api { 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 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 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 Close() { return client?.ConnectClose()?.IsSuccess == true; } public void Dispose() { client?.Dispose(); } public bool ReadDIDouble(string address, out double? res, out string msg) { var result = client?.ReadDouble($"x=2;{address}"); msg = result?.Message; res = result?.Content; return result?.IsSuccess == true; } } }