ABBRobotServer.cs
4.7 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
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 ClientKeepSecond = 10;
private static int ServerPort = 21;
public static string ErrorInfo = "";
private static string MoveOK = "point ok";
private static string FreeOK = "free ok";
private static string LockOK = "lock ok";
private static string MoveCMD_1 = "move";
private static string MoveCMD_2 = "move2";
public static Dictionary<string, Client> ClientMap = new Dictionary<string, Client>();
//最后一次软件控制移动到的位置
public static string LastSendPoint = "";
public delegate void OpEnd(string result);
private static event OpEnd OnMoveEnd;
private static object LockObj = new object();
public static void StartServer(int port)
{
if (!IsStart)
{
ClientMap = new Dictionary<string, Client>();
if (tcpserver == null)
{
tcpserver = new TcpServer();
}
IsStart = true;
tcpserver.Start(port);
LogUtil.info( "ABBRobotServer在端口[" + port + "]监听!");
tcpserver.ReviceMsgEvent += tcp_ReviceMsgEvent;
}
}
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("处理料仓消息【" + 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.debug("收到【" + add + "】的消息【" + msg + "】");
if (msg.ToUpper().Contains("ERROR"))
{
}else if(msg.ToUpper().Contains("MOVE")&& msg.ToUpper().Contains("OK"))
{
OnMoveEnd?.Invoke(msg);
}
else if (msg.ToUpper().Contains("MOVE2") && msg.ToUpper().Contains("OK"))
{
OnMoveEnd?.Invoke(msg);
}
return false;
}
public static void MoveTo(string robotIp, string PointName, string moveType="L" , double targetSpeed = 100, OpEnd AfterMove = null)
{
ABBRobotServer.OnMoveEnd = AfterMove;
SendMovePoint(robotIp, MoveCMD_1,PointName,moveType,targetSpeed.ToString());
}
public static void Move2To(string robotIp, string PointName, string moveType = "L", double targetSpeed = 100, OpEnd AfterMove = null)
{
ABBRobotServer.OnMoveEnd = AfterMove;
SendMovePoint(robotIp, MoveCMD_2, PointName, moveType, targetSpeed.ToString());
}
private static void SendMovePoint(string robotIp, string param1,string param2,string param3,string param4)
{
lock (LockObj)
{
if (ClientMap.ContainsKey(robotIp))
{
Client client = ClientMap[robotIp];
string str = param1 + "," + param2 + "," + param3 + "," + param4+"\r\n";
LastSendPoint = param2;
SendStrToClient(client, str);
}
}
}
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;
}
}
}