diff --git a/Modbus.Net/ModBus.Net/BaseProtocal.cs b/Modbus.Net/ModBus.Net/BaseProtocal.cs index 9e41617..940c413 100644 --- a/Modbus.Net/ModBus.Net/BaseProtocal.cs +++ b/Modbus.Net/ModBus.Net/BaseProtocal.cs @@ -83,5 +83,17 @@ namespace ModBus.Net } } + /// + /// 协议连接开始 + /// + /// + public abstract bool Connect(); + + /// + /// 协议连接断开 + /// + /// + public abstract bool Disconnect(); + } } \ No newline at end of file diff --git a/Modbus.Net/ModBus.Net/ModBus.Net.csproj b/Modbus.Net/ModBus.Net/ModBus.Net.csproj index 9dd7969..0af31ab 100644 --- a/Modbus.Net/ModBus.Net/ModBus.Net.csproj +++ b/Modbus.Net/ModBus.Net/ModBus.Net.csproj @@ -73,10 +73,12 @@ + + Code diff --git a/Modbus.Net/ModBus.Net/ModbusProtocal.cs b/Modbus.Net/ModBus.Net/ModbusProtocal.cs index 794122d..23a453c 100644 --- a/Modbus.Net/ModBus.Net/ModbusProtocal.cs +++ b/Modbus.Net/ModBus.Net/ModbusProtocal.cs @@ -41,7 +41,15 @@ namespace ModBus.Net { public abstract class ModbusProtocal : BaseProtocal { + public override bool Connect() + { + return ProtocalLinker.Connect(); + } + public override bool Disconnect() + { + return ProtocalLinker.Disconnect(); + } } #region 读PLC数据 diff --git a/Modbus.Net/ModBus.Net/ProtocalLinker.cs b/Modbus.Net/ModBus.Net/ProtocalLinker.cs index 9d085e8..46fcf4d 100644 --- a/Modbus.Net/ModBus.Net/ProtocalLinker.cs +++ b/Modbus.Net/ModBus.Net/ProtocalLinker.cs @@ -14,6 +14,17 @@ namespace ModBus.Net { get { return _baseConnector.IsConnected; } } + + public bool Connect() + { + return _baseConnector.Connect(); + } + + public bool Disconnect() + { + return _baseConnector.Disconnect(); + } + /// /// 发送并接收数据 /// diff --git a/Modbus.Net/ModBus.Net/SimenseStructDefinition.cs b/Modbus.Net/ModBus.Net/SimenseStructDefinition.cs new file mode 100644 index 0000000..48c96df --- /dev/null +++ b/Modbus.Net/ModBus.Net/SimenseStructDefinition.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ModBus.Net +{ + public struct TodClockStatus + { + public bool KV + { + get + { + var pos = 15; + return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos); + } + set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 15, value); } + } + + public byte K0_4 + { + get + { + var pos = 0; + var byteValue = ValueHelper.Instance.GetByte(ValueHelper.Instance.GetBytes(TodValue), ref pos); + return (byte)(byteValue%64/4); + } + set + { + var pos = 0; + var byteValue = ValueHelper.Instance.GetByte(ValueHelper.Instance.GetBytes(TodValue), ref pos); + byteValue = (byte)(byteValue - (byteValue%128/4) + value); + TodValue = (ushort)(TodValue%128 + byteValue*128); + } + } + + public bool ZNA + { + get + { + var pos = 5; + return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos); + } + set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 5, value); } + } + + public byte UA + { + get + { + var pos = 3; + var low = ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; + var high = ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0; + high *= 2; + return (byte) (high + low); + } + set + { + TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 3, value % 2 >= 1); + TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 4, value / 2 >= 1); + } + } + public bool UZS + { + get + { + var pos = 2; + return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos); + } + set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 2, value); } + } + + public bool ESY + { + get + { + var pos = 1; + return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos); + } + set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 1, value); } + } + + public bool SYA + { + get + { + var pos = 0; + return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos); + } + set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 0, value); } + } + + public ushort TodValue { get; set; } + } +} diff --git a/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs b/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs index 7d10a20..fc1d0cb 100644 --- a/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs +++ b/Modbus.Net/ModBus.Net/SimenseTcpProtocal.cs @@ -32,14 +32,13 @@ namespace ModBus.Net _tdpuSize = tdpuSize; _ip = ip; connectTryCount = 0; - Connected(); } public override byte[] SendReceive(params object[] content) { while (!ProtocalLinker.IsConnected) { - Connected(); + Connect(); } return base.SendReceive(content); } @@ -49,7 +48,7 @@ namespace ModBus.Net if (!ProtocalLinker.IsConnected) { if (connectTryCount > 10) return null; - Connected(); + Connect(); } return base.SendReceive(unit, content); } @@ -59,16 +58,29 @@ namespace ModBus.Net return base.SendReceive(unit, content); } - protected void Connected() + public override bool Connect() { connectTryCount++; ProtocalLinker = new SimenseTcpProtocalLinker(_ip); - var inputStruct = new CreateReferenceSimenseInputStruct(_tdpuSize, _taspSrc, _tsapDst); - var outputStruct = - (CreateReferenceSimenseOutputStruct)ForceSendReceive(this[typeof(CreateReferenceSimenseProtocal)], inputStruct); - if (!ProtocalLinker.IsConnected) return; - var inputStruct2 = new EstablishAssociationSimenseInputStruct(0x0101, _maxCalling, _maxCalled, _maxPdu); - var outputStruct2 = (EstablishAssociationSimenseOutputStruct)SendReceive(this[typeof(EstablishAssociationSimenseProtocal)], inputStruct2); + if (ProtocalLinker.Connect()) + { + var inputStruct = new CreateReferenceSimenseInputStruct(_tdpuSize, _taspSrc, _tsapDst); + var outputStruct = + (CreateReferenceSimenseOutputStruct) + ForceSendReceive(this[typeof (CreateReferenceSimenseProtocal)], inputStruct); + if (!ProtocalLinker.IsConnected) return false; + var inputStruct2 = new EstablishAssociationSimenseInputStruct(0x0101, _maxCalling, _maxCalled, _maxPdu); + var outputStruct2 = + (EstablishAssociationSimenseOutputStruct) + SendReceive(this[typeof (EstablishAssociationSimenseProtocal)], inputStruct2); + return true; + } + return false; + } + + public override bool Disconnect() + { + return ProtocalLinker.Disconnect(); } } } diff --git a/Modbus.Net/ModBus.Net/TcpProtocalLinker.cs b/Modbus.Net/ModBus.Net/TcpProtocalLinker.cs index bc5b3e9..29898bf 100644 --- a/Modbus.Net/ModBus.Net/TcpProtocalLinker.cs +++ b/Modbus.Net/ModBus.Net/TcpProtocalLinker.cs @@ -18,7 +18,7 @@ namespace ModBus.Net protected TcpProtocalLinker(string ip, int port) { - _baseConnector = new TcpConnector(ip, port, 10000); + _baseConnector = new TcpConnector(ip, port, 30); } } } \ No newline at end of file diff --git a/Modbus.Net/ModBus.Net/ValueHelper.cs b/Modbus.Net/ModBus.Net/ValueHelper.cs index 8bdf33a..8fdf975 100644 --- a/Modbus.Net/ModBus.Net/ValueHelper.cs +++ b/Modbus.Net/ModBus.Net/ValueHelper.cs @@ -46,7 +46,7 @@ namespace ModBus.Net #region Factory - protected static ValueHelper _Instance = null; + protected static ValueHelper _Instance; /// /// ValueHelper单例的实例