ModBusState.cs
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using HslCommunication.Core;
using System;
using System.Net;
using System.Net.Sockets;
namespace HslCommunication.ModBus
{
/// <summary>
/// ModBus的异步状态信息
/// </summary>
internal class ModBusState
{
#region Constructor
/// <summary>
/// 实例化一个对象
/// </summary>
public ModBusState()
{
hybirdLock = new SimpleHybirdLock();
ConnectTime = DateTime.Now;
HeadByte = new byte[6];
}
#endregion
/// <summary>
/// 连接的时间
/// </summary>
public DateTime ConnectTime { get; private set; }
/// <summary>
/// 远端的地址
/// </summary>
public IPEndPoint IpEndPoint { get; internal set; }
/// <summary>
/// 远端的Ip地址
/// </summary>
public string IpAddress { get; internal set; }
/// <summary>
/// 工作套接字
/// </summary>
public Socket WorkSocket = null;
/// <summary>
/// 消息头的缓存
/// </summary>
public byte[] HeadByte = new byte[6];
/// <summary>
/// 消息头的接收长度
/// </summary>
public int HeadByteReceivedLength = 0;
/// <summary>
/// 内容数据缓存
/// </summary>
public byte[] Content = null;
/// <summary>
/// 内容数据接收长度
/// </summary>
public int ContentReceivedLength = 0;
/// <summary>
/// 回发信息的同步锁
/// </summary>
internal SimpleHybirdLock hybirdLock;
/// <summary>
/// 指示客户端是否下线,已经下线则为1
/// </summary>
private int isSocketOffline = 0;
/// <summary>
/// 判断当前的客户端是否已经下线,下线成功的话,就返回True
/// </summary>
/// <returns></returns>
public bool IsModbusOffline()
{
int tmp = System.Threading.Interlocked.CompareExchange(ref isSocketOffline, 1, 0);
return tmp == 0;
}
/// <summary>
/// 清除原先的接收状态
/// </summary>
public void Clear()
{
Array.Clear(HeadByte, 0, 6);
HeadByteReceivedLength = 0;
Content = null;
ContentReceivedLength = 0;
}
}
}