OpenProtocolNet.cs
4.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
namespace HslCommunication.Profinet.OpenProtocol
{
/// <summary>
/// 开放以太网协议
/// </summary>
public class OpenProtocolNet : NetworkDoubleBase<OpenProtocolMessage, RegularByteTransform>
{
#region Constructor
/// <summary>
/// 实例化一个默认的对象
/// </summary>
public OpenProtocolNet()
{
}
/// <summary>
/// 使用指定的IP地址来初始化对象
/// </summary>
/// <param name="ipAddress">Ip地址</param>
/// <param name="port">端口号</param>
public OpenProtocolNet(string ipAddress, int port)
{
}
#endregion
#region Override NetworkDoubleBase
/// <summary>
/// 连接上服务器后需要进行的初始化操作
/// </summary>
/// <param name="socket">网络套接字</param>
/// <returns>是否初始化成功,依据具体的协议进行重写</returns>
protected override OperateResult InitializationOnConnect(Socket socket)
{
// 此处的 revison 应该等于多少?
OperateResult<string> open = ReadCustomer(1, 0, 0, 0, null);
if (!open.IsSuccess) return open;
if (open.Content.Substring(4, 4) == "0002")
return OperateResult.CreateSuccessResult();
else
return new OperateResult("Failed:" + open.Content.Substring(4, 4));
}
#endregion
/// <summary>
/// 自定义的命令读取
/// </summary>
/// <param name="mid"></param>
/// <param name="revison"></param>
/// <param name="stationId"></param>
/// <param name="spindleId"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public OperateResult<string> ReadCustomer(int mid, int revison, int stationId, int spindleId, List<string> parameters)
{
if (parameters != null) parameters = new List<string>();
OperateResult<byte[]> command = BuildReadCommand(mid, revison, stationId, spindleId, parameters);
if (!command.IsSuccess) return OperateResult.CreateFailedResult<string>(command);
OperateResult<byte[]> read = ReadFromCoreServer(command.Content);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<string>(read);
return OperateResult.CreateSuccessResult(Encoding.ASCII.GetString(read.Content));
}
#region Object Override
/// <summary>
/// 返回表示当前对象的字符串
/// </summary>
/// <returns>字符串信息</returns>
public override string ToString()
{
return $"OpenProtocolNet[{IpAddress}:{Port}]";
}
#endregion
/// <summary>
/// 构建一个读取的初始报文
/// </summary>
/// <param name="mid"></param>
/// <param name="revison"></param>
/// <param name="stationId"></param>
/// <param name="spindleId"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static OperateResult<byte[]> BuildReadCommand(int mid, int revison, int stationId, int spindleId, List<string> parameters)
{
if (mid < 0 || mid > 9999) return new OperateResult<byte[]>("Mid must be between 0 - 9999");
if (revison < 0 || revison > 999) return new OperateResult<byte[]>("revison must be between 0 - 999");
if (stationId < 0 || stationId > 9) return new OperateResult<byte[]>("stationId must be between 0 - 9");
if (spindleId < 0 || spindleId > 99) return new OperateResult<byte[]>("spindleId must be between 0 - 99");
int count = 0;
if (parameters != null)
parameters.ForEach(m => count += m.Length);
StringBuilder sb = new StringBuilder();
sb.Append((20 + count).ToString("D4"));
sb.Append(mid.ToString("D4"));
sb.Append(revison.ToString("D3"));
sb.Append('\0');
sb.Append(stationId.ToString("D1"));
sb.Append(spindleId.ToString("D2"));
sb.Append('\0');
sb.Append('\0');
sb.Append('\0');
sb.Append('\0');
sb.Append('\0');
if (parameters != null)
for (int i = 0; i < parameters.Count; i++)
{
sb.Append(parameters[i]);
}
sb.Append('\0');
return OperateResult.CreateSuccessResult(Encoding.ASCII.GetBytes(sb.ToString()));
}
}
}