using HslCommunication.Core; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; namespace HslCommunication.ModBus { /// /// ModBus的异步状态信息 /// internal class ModBusState { #region Constructor /// /// 实例化一个对象 /// public ModBusState( ) { hybirdLock = new SimpleHybirdLock( ); ConnectTime = DateTime.Now; HeadByte = new byte[6]; } #endregion /// /// 连接的时间 /// public DateTime ConnectTime { get; private set; } /// /// 远端的地址 /// public IPEndPoint IpEndPoint { get; internal set; } /// /// 远端的Ip地址 /// public string IpAddress { get; internal set; } /// /// 工作套接字 /// public Socket WorkSocket = null; /// /// 消息头的缓存 /// public byte[] HeadByte = new byte[6]; /// /// 消息头的接收长度 /// public int HeadByteReceivedLength = 0; /// /// 内容数据缓存 /// public byte[] Content = null; /// /// 内容数据接收长度 /// public int ContentReceivedLength = 0; /// /// 回发信息的同步锁 /// internal SimpleHybirdLock hybirdLock; /// /// 指示客户端是否下线,已经下线则为1 /// private int isSocketOffline = 0; /// /// 判断当前的客户端是否已经下线,下线成功的话,就返回True /// /// public bool IsModbusOffline( ) { int tmp = System.Threading.Interlocked.CompareExchange( ref isSocketOffline, 1, 0 ); return tmp == 0; } /// /// 清除原先的接收状态 /// public void Clear( ) { Array.Clear( HeadByte, 0, 6 ); HeadByteReceivedLength = 0; Content = null; ContentReceivedLength = 0; } } }