Commit c029b2c1 几米阳光

修改UR机器人连接模块

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