URRobotClient.cs 5.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using URSoldering.Common;

namespace URSoldering.DeviceLibrary
{
    public class URRobotClient
    {
        private static URTcpClient listenClient = null;  
        private static int Port = 30003;
        public static string  LastMoveCMD = "";
        public static void StartListen(string ip)
        { 
            listenClient = new URTcpClient();
            listenClient.DefaultDataLength = 1078;
            listenClient.ReviceSleepMS = 5;
            listenClient.connect(ip, Port, HandleMessage); 
        }
        public static void StopListen()
        {
            if (listenClient != null)
            {
                listenClient.close();
            }
        }
        public static bool IsConnected()
        {
            if (listenClient != null)
            {
                return listenClient.IsConnected();
            }return false;
        }
        public static string LogName
        {
            get { return "【UR端口:" + Port + "】"; }
        }
        private static double BitToDouble(byte[] data)
        {
            double f = BitConverter.ToDouble(data, 0);
            return f;
        }
        private static int BitToInt(byte[] data)
        {
            int f = BitConverter.ToInt32(data, 0);
            return f;
        }
        private static double  BitToFloat(string hexString)
        {
            uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
            byte[] floatVals = BitConverter.GetBytes(num);
            double f = BitConverter.ToDouble(floatVals, 0);
            return f;
        }
        private static string ByteToStr(byte[] reviceData)
        {
            string strFromat = "{0:X2}";
            string reviceMsg = "";
            foreach (byte data in reviceData)
            {
                reviceMsg = reviceMsg + " " + String.Format(strFromat, data);
            }return reviceMsg;
        }
       
        private static void HandleMessage(byte[] reviceData)
        {
            if (LastMoveCMD.Equals("").Equals(false))
            {
                listenClient.sendLine(LastMoveCMD);
            }
            StopListen();
            try
            {
                //string reviceMsg = ByteToStr(reviceData);
                //LogUtil.info("Read data:【" + reviceMsg + "】  ");
                byte[] msgSizeArray = reviceData.Skip(0).Take(4).ToArray();
                byte[] timeArray = reviceData.Skip(4).Take(8).ToArray();
                byte[] qTargetArray = reviceData.Skip(12).Take(48).ToArray();
                byte[] qdTargetArray = reviceData.Skip(60).Take(48).ToArray();
                byte[] qddArray = reviceData.Skip(108).Take(48).ToArray();
                byte[] iTargetArray = reviceData.Skip(156).Take(48).ToArray();

                int messageSize = BitToInt(reviceData.Skip(0).Take(4).ToArray().Reverse<byte>().ToArray());
                int maxdouble = (messageSize - 4) / 8;
                List<double> doubleList = new List<double>();
                doubleList.Add(messageSize);
                if (maxdouble > 61)
                {
                    for (int i = 0; i < maxdouble; i++)
                    {
                        if (i >= 55 && i <= 61)
                        {
                            if (reviceData.Length >= (12 + i * 8))
                            {
                                byte[] data = reviceData.Skip(4 + i * 8).Take(8).ToArray().Reverse<byte>().ToArray();
                                double value = BitToDouble(data);
                                //LogUtil.info("第【"+(i+1)+"】个double["+ByteToStr(data) +"]值:"+value);
                                doubleList.Add(value);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            doubleList.Add(0);
                        }
                    }
                    if (doubleList.Count > 61)
                    {
                        string spilt = ",";
                        double x = doubleList[56] * 1000;
                        double y = doubleList[57] * 1000;
                        double z = doubleList[58] * 1000;
                        double rx = doubleList[59] * 1;
                        double ry = doubleList[60] * 1;
                        double rz = doubleList[61] * 1;
                        URPointValue newp = new URPointValue(x, y, z, rx, ry, rz);
                        URRobotControl.LastPoint = newp;
                        string reviceMsg = ByteToStr(reviceData);
                        //LogUtil.info("Read data:【" + reviceMsg + "】  ");
                        LogUtil.URLInfo(  LogName+"length[" + messageSize + "],data长[" + reviceData.Length + "]坐标"+newp.ToShowStr());
                    }
                }
                else
                {
                    string reviceMsg = ByteToStr(reviceData);
                    LogUtil.URLError(LogName + "Read data:【" + reviceMsg + "】  ");
                    LogUtil.URLError(LogName + "无法获取坐标,数据长度不正确");
                }
            }catch(Exception ex)
            {
                LogUtil.URLError(LogName+ "HandleMessage出错:" + ex.ToString());
                StopListen();
            }
        }

    }
}