ABBRobotServer.cs
7.5 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ABBRobotTest
{
public class ABBRobotServer
{
private static TcpServer tcpserver = null;
public static bool IsStart = false;
private static int ABBServerPort = ConfigAppSettings.GetIntValue(Setting_Init.ABBServerPort );
public static string ErrorInfo = "";
internal static string Cmd_movep = "movep";
internal static string Cmd_moveget = "moveget";
internal static string Cmd_moveput = "moveput";
internal static string Cmd_validateP = "validateP";
public static Dictionary<string, Client> ClientMap = new Dictionary<string, Client>();
//最后一次软件控制移动到的位置
public static string LastSendPoint = "";
public delegate void OpEnd(string result);
private static event OpEnd OnCmdEnd;
private static object LockObj = new object();
public static void StartServer( )
{
try
{
if (ABBServerPort <= 0)
{
ABBServerPort = 21;
}
if (!IsStart)
{
ClientMap = new Dictionary<string, Client>();
if (tcpserver == null)
{
tcpserver = new TcpServer();
}
IsStart = true;
tcpserver.Start(ABBServerPort);
tcpserver.AcceptClientEvent += Tcpserver_AcceptClientEvent;
LogUtil.info("ABBRobotServer在端口[" + ABBServerPort + "]监听!");
tcpserver.ReviceMsgEvent += tcp_ReviceMsgEvent;
}
}catch(Exception ex)
{
LogUtil.error("ABBRobotServer启动[" + ABBServerPort + "]监听错误:" + ex.ToString());
}
}
private static void Tcpserver_AcceptClientEvent(Client client)
{
SaveRobotClient(client);
}
private static object MapLock = "";
private static void SaveRobotClient( Client client)
{
lock (MapLock)
{
IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
string ip = clientipe.Address.ToString();
if (ClientMap.ContainsKey(ip))
{
ClientMap.Remove(ip);
}
ClientMap.Add(ip, client);
}
}
public static void StopServer()
{
try
{
if (IsStart)
{
IsStart = false;
tcpserver.Stop();
}
}
catch (Exception ex)
{
LogUtil.error("关闭 监听出错:" + ex.ToString());
}
}
private static void tcp_ReviceMsgEvent(Client client, string msg)
{
try
{
if (String.IsNullOrEmpty(msg))
{
return;
}
ProcessMsg(client, msg);
}
catch (Exception ex)
{
LogUtil.error("处理ABB消息【" + msg + "】出错:" + ex.ToString());
}
}
private static bool ProcessMsg(Client client, string msg)
{
IPEndPoint clientipe = (IPEndPoint)client.ClientSocket.RemoteEndPoint;
string add = clientipe.Address.ToString();
// string[] msgArray = msg.Split(cmd_spilt);
msg = msg.Replace("\r", "") ;
LogUtil.info("Revice[" + add + "]:[" + msg + "]");
msg = msg.Replace(";", "");
string OkStr = "OK";
if (msg.Contains("ERROR"))
{
ErrorInfo = msg;
}
else if(msg.Contains(Cmd_movep)&& msg.Contains(OkStr))
{
OnCmdEnd?.Invoke(msg);
}
else if (msg.Contains(Cmd_moveput) && msg.Contains(OkStr))
{
OnCmdEnd?.Invoke(msg);
} else if (msg.Contains(Cmd_moveget) && msg.Contains(OkStr))
{
OnCmdEnd?.Invoke(msg);
}else if (msg.Contains(OkStr))
{
OnCmdEnd?.Invoke(msg);
}
return false;
}
public static bool MoveToP(string robotIp, string PointName, string moveType="L" , double targetSpeed = 100, OpEnd AfterCmd = null)
{
ABBRobotServer.OnCmdEnd = AfterCmd;
return SendMovePoint(robotIp, Cmd_movep, PointName,moveType,targetSpeed.ToString());
}
public static bool MoveToPut(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
{
ABBRobotServer.OnCmdEnd = AfterCmd;
return SendMovePoint(robotIp, Cmd_moveput, PointName, moveType, targetSpeed.ToString());
}
public static bool MoveToGet(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
{
ABBRobotServer.OnCmdEnd = AfterCmd;
return SendMovePoint(robotIp, Cmd_moveget, PointName, moveType, targetSpeed.ToString());
}
public static bool SendCmd(string robotIp, string moveCmd,string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterCmd = null)
{
ABBRobotServer.OnCmdEnd = AfterCmd;
if (moveCmd.Equals(Cmd_moveget) || moveCmd.Equals(Cmd_movep) || moveCmd.Equals(Cmd_moveput)||moveCmd.Equals(Cmd_validateP))
{
return SendMovePoint(robotIp, moveCmd, PointName, moveType, targetSpeed.ToString());
}
return false;
}
public static bool ValidateP(string robotIp, string pointName, OpEnd AfterCmd = null)
{
ABBRobotServer.OnCmdEnd = AfterCmd;
return SendMovePoint(robotIp, Cmd_validateP, pointName);
}
private static bool SendMovePoint(string robotIp, string param1, string param2, string param3 = "L", string param4 = "10")
{
Client client = null;
lock (LockObj)
{
if (ClientMap.ContainsKey(robotIp))
{
client = ClientMap[robotIp];
}
}
if (client != null)
{
string str = param1 + "," + param2 + "," + param3 + "," + param4 + "";
LastSendPoint = param2;
LogUtil.info("Send [" + robotIp + "] : [" + str + "]");
SendStrToClient(client, str);
return true;
}
return false;
}
private static bool SendStrToClient(Client client, string sendMsg)
{
if (client != null && client.ClientSocket.Connected)
{
byte[] sendBuffer = new byte[1024];
sendBuffer = Encoding.UTF8.GetBytes(sendMsg);
client.ClientSocket.Send(sendBuffer);
return true;
}
return false;
}
internal static bool RobotIsConnect(string ip)
{
if (ClientMap.ContainsKey(ip))
{
return true;
}
return false;
}
}
}