Commit c029b2c1 几米阳光

修改UR机器人连接模块

1 个父辈 4ba556c0
...@@ -10,6 +10,7 @@ namespace URSoldering.DeviceLibrary ...@@ -10,6 +10,7 @@ namespace URSoldering.DeviceLibrary
{ {
public class URRobotClient public class URRobotClient
{ {
public static bool IsSpitPackage = true ;
public delegate void SafetModePro(string mode); public delegate void SafetModePro(string mode);
public static event SafetModePro SafetModeFun; public static event SafetModePro SafetModeFun;
public delegate void RobotModePro(string mode); public delegate void RobotModePro(string mode);
...@@ -19,8 +20,9 @@ namespace URSoldering.DeviceLibrary ...@@ -19,8 +20,9 @@ namespace URSoldering.DeviceLibrary
public static string LastMoveCMD = ""; public static string LastMoveCMD = "";
public static void StartListen(string ip) public static void StartListen(string ip)
{ {
LastReviceDataTime = DateTime.Now;
listenClient = new URTcpClient(); listenClient = new URTcpClient();
listenClient.DefaultDataLength = 1078; listenClient.DefaultDataLength = 1108;
listenClient.ReviceSleepMS = 5; listenClient.ReviceSleepMS = 5;
listenClient.connect(ip, Port, HandleMessage); listenClient.connect(ip, Port, HandleMessage);
} }
...@@ -68,7 +70,15 @@ namespace URSoldering.DeviceLibrary ...@@ -68,7 +70,15 @@ namespace URSoldering.DeviceLibrary
reviceMsg = reviceMsg + " " + String.Format(strFromat, data); reviceMsg = reviceMsg + " " + String.Format(strFromat, data);
}return reviceMsg; }return reviceMsg;
} }
private static DateTime LastReviceDataTime = DateTime.Now;
public static bool IsTimeOut()
{
TimeSpan span = DateTime.Now - LastReviceDataTime;
if (span.TotalSeconds > 1)
{
return true;
}return false;
}
private static void HandleMessage(byte[] reviceData) private static void HandleMessage(byte[] reviceData)
{ {
if (LastMoveCMD.Equals("").Equals(false)) if (LastMoveCMD.Equals("").Equals(false))
...@@ -76,7 +86,10 @@ namespace URSoldering.DeviceLibrary ...@@ -76,7 +86,10 @@ namespace URSoldering.DeviceLibrary
listenClient.sendLine(LastMoveCMD); listenClient.sendLine(LastMoveCMD);
LastMoveCMD = ""; LastMoveCMD = "";
} }
if (!IsSpitPackage)
{
StopListen(); StopListen();
}
try try
{ {
byte[] msgSizeArray = reviceData.Skip(0).Take(4).ToArray(); byte[] msgSizeArray = reviceData.Skip(0).Take(4).ToArray();
...@@ -116,6 +129,7 @@ namespace URSoldering.DeviceLibrary ...@@ -116,6 +129,7 @@ namespace URSoldering.DeviceLibrary
} }
if (doubleList.Count > 61) if (doubleList.Count > 61)
{ {
LastReviceDataTime = DateTime.Now;
double x = doubleList[56] * 1000; double x = doubleList[56] * 1000;
double y = doubleList[57] * 1000; double y = doubleList[57] * 1000;
double z = doubleList[58] * 1000; double z = doubleList[58] * 1000;
...@@ -140,7 +154,11 @@ namespace URSoldering.DeviceLibrary ...@@ -140,7 +154,11 @@ namespace URSoldering.DeviceLibrary
else else
{ {
string reviceMsg = ByteToStr(reviceData); string reviceMsg = ByteToStr(reviceData);
LogUtil.URLError(LogName + "length[" + messageSize + "],data长[" + reviceData.Length + "]无法获取坐标,数据长度不正确"); LogUtil.URLError(LogName + "length[" + messageSize + "],data长[" + reviceData.Length + "]无法获取坐标,关闭重新连接");
if (IsSpitPackage)
{
StopListen();
}
} }
} }
catch(Exception ex) catch(Exception ex)
...@@ -201,5 +219,19 @@ namespace URSoldering.DeviceLibrary ...@@ -201,5 +219,19 @@ namespace URSoldering.DeviceLibrary
return ""; return "";
} }
} }
public static void SendCMD(string moveCmd)
{
try
{
if (listenClient != null)
{
listenClient.send(moveCmd);
}
}catch(Exception ex)
{
LogUtil.URSError(LogName+"发送命令【"+moveCmd+"】出错:"+ex.ToString());
}
}
} }
} }
...@@ -224,6 +224,11 @@ namespace URSoldering.DeviceLibrary ...@@ -224,6 +224,11 @@ namespace URSoldering.DeviceLibrary
if (!URRobotClient.IsConnected()) if (!URRobotClient.IsConnected())
{ {
URRobotClient.StartListen(RobotIp); URRobotClient.StartListen(RobotIp);
}else if (URRobotClient.IsTimeOut())
{
LogUtil.URSError( URRobotClient.LogName+ "接收数据超时,断开后重连" );
URRobotClient.StopListen();
URRobotClient.StartListen(RobotIp);
} }
} }
} }
...@@ -301,6 +306,10 @@ namespace URSoldering.DeviceLibrary ...@@ -301,6 +306,10 @@ namespace URSoldering.DeviceLibrary
} }
private static void ModeProcess(string message) private static void ModeProcess(string message)
{ {
if (IsRun)
{
return;
}
string msg = message.ToLower().Replace(REV_RobotMode + ": ", "").ToUpper().Trim(); string msg = message.ToLower().Replace(REV_RobotMode + ": ", "").ToUpper().Trim();
if (msg.Equals(URStatus.POWER_ON)) if (msg.Equals(URStatus.POWER_ON))
{ {
...@@ -332,6 +341,10 @@ namespace URSoldering.DeviceLibrary ...@@ -332,6 +341,10 @@ namespace URSoldering.DeviceLibrary
private static void SafetyProcess(string message) private static void SafetyProcess(string message)
{ {
if (IsRun)
{
return;
}
string msg = message.ToLower().Replace(REV_SafetyMode + ": ", "").ToUpper().Trim(); string msg = message.ToLower().Replace(REV_SafetyMode + ": ", "").ToUpper().Trim();
if (msg.Equals(URStatus.SFETY_POWER_OFF) || msg.Equals(URStatus.ROBOT_EMERGENCY_STOP)) if (msg.Equals(URStatus.SFETY_POWER_OFF) || msg.Equals(URStatus.ROBOT_EMERGENCY_STOP))
......
...@@ -17,10 +17,10 @@ namespace URSoldering.DeviceLibrary ...@@ -17,10 +17,10 @@ namespace URSoldering.DeviceLibrary
{ {
public delegate void ByteHandleMessage(byte[] data); public delegate void ByteHandleMessage(byte[] data);
public int DefaultDataLength = 1078; public int DefaultDataLength = 1024;
public int ReviceSleepMS = 100; public int ReviceSleepMS = 100;
private Socket m_clientSocket = null; private Socket m_clientSocket = null;
private byte[] m_receiveBuffer = new byte[1078]; private byte[] m_receiveBuffer = new byte[1024];
private string LogName = ""; private string LogName = "";
private ByteHandleMessage byteOnReceived; private ByteHandleMessage byteOnReceived;
...@@ -211,19 +211,10 @@ namespace URSoldering.DeviceLibrary ...@@ -211,19 +211,10 @@ namespace URSoldering.DeviceLibrary
} }
} }
int headSize = 4;//包头长度 固定4 int headSize = 4;//包头长度 固定4
byte[] surplusBuffer = null;//不完整的数据包,即用户自定义缓冲区
/// <summary>
/// 接收客户端发来的数据
/// </summary>
/// <param name="connId">每个客户的会话ID</param>
/// <param name="bytes">缓冲区数据</param>
/// <returns></returns>
private int OnReceive(byte[] bytes) private int OnReceive(byte[] bytes)
{ {
try try
{ {
//bytes 为系统缓冲区数据
//bytesRead为系统缓冲区长度
int bytesRead = bytes.Length; int bytesRead = bytes.Length;
if (bytesRead > 0) if (bytesRead > 0)
{ {
...@@ -247,26 +238,25 @@ namespace URSoldering.DeviceLibrary ...@@ -247,26 +238,25 @@ namespace URSoldering.DeviceLibrary
{ {
byte[] byteSub = new byte[totalLen - haveRead]; byte[] byteSub = new byte[totalLen - haveRead];
//把剩下不够一个完整的数据包存起来 //把剩下不够一个完整的数据包存起来
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); byteSub = surplusBuffer.Skip(haveRead).Take(totalLen - haveRead).ToArray();
surplusBuffer = byteSub; surplusBuffer = byteSub;
totalLen = 0; totalLen = 0;
break; break;
} }
//如果够了一个完整包,则读取包头的数据 //如果够了一个完整包,则读取包头的数据
byte[] headByte = new byte[headSize]; byte[] headByte = new byte[headSize];
Buffer.BlockCopy(surplusBuffer, haveRead, headByte, 0, headSize);//从缓冲区里读取包头的字节
headByte = surplusBuffer.Skip(haveRead).Take(headSize).ToArray().Reverse<byte>().ToArray(); headByte = surplusBuffer.Skip(haveRead).Take(headSize).ToArray().Reverse<byte>().ToArray();
int bodySize = BitConverter.ToInt32(headByte, 0);//从包头里面分析出包体的长度 int bodySize = BitConverter.ToInt32(headByte, 0);//从包头里面分析出包体的长度
if (bodySize <= 0) if (bodySize <= 0)
{ {
LogUtil.URLError(LogName + "解析错误:长度:" + bodySize); LogUtil.URLError(LogName + "解析错误:长度:" + bodySize);
surplusBuffer = null;//设置空 回到原始状态
totalLen = 0;//清0
} }
//这里的 haveRead=等于N个数据包的长度 从0开始;0,1,2,3....N
//如果自定义缓冲区拆解N个包后的长度 大于 总长度,说最后一段数据不够一个完整的包了,拆出来保存
if (haveRead + bodySize > totalLen) if (haveRead + bodySize > totalLen)
{ {
byte[] byteSub = new byte[totalLen - haveRead]; byte[] byteSub = new byte[totalLen - haveRead];
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); byteSub = surplusBuffer.Skip(haveRead).Take(totalLen - haveRead).ToArray();
surplusBuffer = byteSub; surplusBuffer = byteSub;
break; break;
} }
...@@ -295,6 +285,9 @@ namespace URSoldering.DeviceLibrary ...@@ -295,6 +285,9 @@ namespace URSoldering.DeviceLibrary
} }
return 1; return 1;
} }
private int defaultBodySize = 1108;
private byte[] surplusBuffer = null;//不完整的数据包,即用户自定义缓冲区
private void ReceiveCallBack(IAsyncResult ar) private void ReceiveCallBack(IAsyncResult ar)
{ {
...@@ -303,11 +296,22 @@ namespace URSoldering.DeviceLibrary ...@@ -303,11 +296,22 @@ namespace URSoldering.DeviceLibrary
if (m_clientSocket != null && m_clientSocket.Connected) if (m_clientSocket != null && m_clientSocket.Connected)
{ {
int REnd = m_clientSocket.EndReceive(ar); int REnd = m_clientSocket.EndReceive(ar);
if (URRobotClient.IsSpitPackage)
{
OnReceive(m_receiveBuffer);
Thread.Sleep(ReviceSleepMS);
if (m_clientSocket != null)
{
m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);
}
}
else
{
Task.Factory.StartNew(delegate () Task.Factory.StartNew(delegate ()
{ {
byteOnReceived(m_receiveBuffer); byteOnReceived(m_receiveBuffer);
}); });
//m_clientSocket.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null); }
} }
} }
catch (Exception ex) catch (Exception ex)
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!