URRobotManager.cs 4.2 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 URRobotManager
    {
        private static URTcpClient listenClient = null; 
        public static void StartListen()
        { 
            listenClient = new URTcpClient();
            listenClient.DefaultDataLength = 1024;
            listenClient.ReviceSleepMS = 5;
            listenClient.connect("192.168.10.10", 30003, HandleMessage);
        }
        public static void StopListen()
        {
            if (listenClient != null)
            {
                listenClient.close();
            }
        }
        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)
        {

            //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);
                for (int i = 0; i < maxdouble; i++)
                {
                    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;
                    }
                }
                if (doubleList.Count > 100)
                {
                    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;

                    LogUtil.info("数据长度:"+messageSize+",data长度:"+reviceData.Length+"。实际坐标:" + x + spilt + y + spilt + z + spilt + rx + spilt + ry + spilt + rz + spilt);
                }
                else
                {
                    string reviceMsg = ByteToStr(reviceData);
                    LogUtil.info("Read data:【" + reviceMsg + "】  ");
                    LogUtil.info("无法获取坐标,数据长度不正确");
                }
            }catch(Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }
        }
    }
}