using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;


namespace SimensCNC
{
  public  class ETH_S7SimensLib
    {
        public static string cncIP;
        int receiveData;

        byte[] sendCommands;

        byte[] buffer;//发送报文容器
        byte[] datas;//接收报文容器

        static IAsyncResult connResult = null;

        Func<byte[], int, object> func;

        public ETH_S7SimensLib()
        {

        }

        public Socket socket { get; set; }

        /// <summary>
        /// 链接函数
        /// </summary>
        public bool ETH_S7SimensConnect(string adrip, ushort port, int overtime)
        {
            try
            {
                cncIP = adrip;
                IPAddress ip = IPAddress.Parse(adrip);
                int ports = Convert.ToInt32(port);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                connResult = socket.BeginConnect(ip, ports, null, null);
                socket.ReceiveTimeout = 2000;
                Thread.Sleep(5000);
                connResult.AsyncWaitHandle.WaitOne(5, true);  //等待2秒
                if (connResult.IsCompleted)
                {
                    HandShark();
                    return true;
                }
                else
                {

                    return false;
                }
            }
            catch
            {
                return false; ;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 断开设备连接
        /// </summary>
        /// <param name="no">设备编号</param>
        public void ETH_S7SimensDisConnect(string no)
        {
            try
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch
            {

            }
            finally
            {

            }
     
        }

        /// <summary>
        /// 三次握手
        /// </summary>
        void HandShark()
        {
            socket.Send(ETH_S7SimensCommands.FIRST_HAND_SHANK);   
            buffer = new byte[1024];
            receiveData = socket.Receive(buffer);
            socket.Send(ETH_S7SimensCommands.SENCOND_HAND_SHARK);
            buffer = new byte[1024];
            receiveData = socket.Receive(buffer);
            socket.Send(ETH_S7SimensCommands.THREE_HAND_SHARK);
            buffer = new byte[1024];
            receiveData = socket.Receive(buffer);
        }

        /// <summary>
        /// 获取CNCIP
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetCNCIP(string no,ref object result)
        {
            if (cncIP != null)
            {
                result = cncIP;
                return true;
            }
            else
            {
                return false;
            };
        }

        /// <summary>
        /// 获取采集时间
        /// </summary>
        /// <returns></returns>
        public bool ETH_S7SimensCollectTime(ref object result)
        {
            if (socket.Connected == true)
            {
                result = DateTime.Now; ;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取心跳包
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetKeepAlive(string no,ref object result)
        {
            if (connResult.IsCompleted == true)
            {
                result = true;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <param name="no"></param>
        public bool ETH_S7SimensGetVerVison(ref object result)
        {
            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.READ_VER_INFO, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 定义机床指纹
        /// </summary>
        public bool ETH_S7SimensGetCNCID(ref object result)
        {
            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.CNC_ID, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
       
        }

        /// <summary>
        /// 获取机床类型
        /// </summary>
        public bool ETH_S7SimensCNCType(ref object result)
        {
            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.CNC_TYPE, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return true;
            }
           finally
            {

            }
         
        }

        /// <summary>
        /// 操作模式
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetModeInfo(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalusisModeData);
                return GetData(ETH_S7SimensCommands.CNC_MODE, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 运行状态
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetStateInfo(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalusisStatusData);
                return GetData(ETH_S7SimensCommands.CNC_STATUS, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取加工件数
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetProductCounts(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_PRODUCTS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 进给设定速度
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetSetFeedSpeed(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_FEEDSETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 进给实际速度
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetActFeedSpeed(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_FEEDACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 主轴设定速度
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetSetSpSpeed(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_SPINDLESETSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 主轴实际速度
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetActSpSpeed(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_SPINDLEACTSPEED, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 主轴倍率
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetSFeed(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_SPINDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 进给倍率
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimenstGetFeedRate(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_FEEDRATE, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        ///循环时间
        /// </summary>
        /// <param name="socket"></param>
        public bool ETH_S7SimenstGetCycTime(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.RUN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        ///剩余时间
        /// </summary>
        /// <param name="socket"></param>
        public bool ETH_S7SimenstGetRemTime(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.REMAIN_TIME, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }


        /// <summary>
        /// 获取加工程序名称
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetPrgName(ref object result)
        {

            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.PROGRAM_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }

        }

        /// <summary>
        /// 加工程序内容
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetPrgContent(ref object result)
        {
            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.CurrentPro, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return true;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取轴名称
        /// 同时是获取机床参数的方法
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetAxisName(ref object result)
        {
            try
            {
                func = new Func<byte[], int, string>(AnalysisStrData);
                return GetData(ETH_S7SimensCommands.AXIS_NAME, (int)DataTypeEnum.DATATYPE_STRING, func, ref result);
            }
            catch
            {
                return true;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取机械坐标
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public  bool ETH_S7SimensGetMacPos(ref object result)
        {
            try
            {
                List<object> pos = new List<object>();
                foreach (var s in ETH_S7SimensCommands.posflag)
                {
                    func = new Func<byte[], int, object>(AnalysisDoubleData);

                    ETH_S7SimensCommands.MACHINE_POS[26] = s;
                    GetData(ETH_S7SimensCommands.MACHINE_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
                    pos.Add(result);
                }
                if (pos != null)
                {
                    result = string.Join("/", pos);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {

            }
          
        }


        /// <summary>
        /// 获取相对坐标，工件坐标
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetRelPos(ref object result)
        {
            try
            {
                List<object> pos = new List<object>();
                foreach (var s in ETH_S7SimensCommands.posflag)
                {
                    func = new Func<byte[], int, object>(AnalysisDoubleData);

                    ETH_S7SimensCommands.RELATIVELY_POS[26] = s;

                    GetData(ETH_S7SimensCommands.RELATIVELY_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
                    pos.Add(result);
                }
                if (pos != null)
                {
                    result = string.Join("/", pos);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {

            }

        }

        /// <summary>
        /// 获取剩余坐标
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetRemPos(ref object result)
        {
            try
            {
                List<object> pos = new List<object>();
                foreach (var s in ETH_S7SimensCommands.posflag)
                {
                    func = new Func<byte[], int, object>(AnalysisDoubleData);

                    ETH_S7SimensCommands.REMAIN_POS[26] = s;

                    GetData(ETH_S7SimensCommands.REMAIN_POS, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
                    pos.Add(result);
                }
                if (pos != null)
                {
                    result = string.Join("/", pos);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }


        /// <summary>
        /// 获取刀具号T
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetToolNum(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisInt32Data);
                return GetData(ETH_S7SimensCommands.TOOL_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取报警号
        /// </summary>
        /// <param name="no"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetAlarmMsgNum(ref object result)
        {
            try
            {

                func = new Func<byte[], int, object>(AnalysisInt32Data);
                return GetData(ETH_S7SimensCommands.CNC_ALARM_NUM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取报警信息
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetAlarmMsg(byte numflag, ref object result)
        {
            try
            {
                ETH_S7SimensCommands.CNC_ALARM[26] = numflag;
                func = new Func<byte[], int, object>(AnalysisAlarm);
                return GetData(ETH_S7SimensCommands.CNC_ALARM, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
            }catch
            {
                return false;
            }
            finally
            {

            }

        }

        /// <summary>
        /// 获取刀具半径补偿编号
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetDNum(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisInt32Data);
            return GetData(ETH_S7SimensCommands.TOOL_D_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func,ref result);
        }

        /// <summary>
        /// 获取刀具长度补偿编号
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetHNum(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisInt32Data);
            return GetData(ETH_S7SimensCommands.TOOL_H_NUMBER, (int)DataTypeEnum.DATATYPE_INT, func, ref result);
        }

        /// <summary>
        /// 获取长度补偿X
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetLengthX(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisDoubleData);
            return GetData(ETH_S7SimensCommands.TOOL_X_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
        }

        /// <summary>
        /// 获取长度补偿Z
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetLengthZ(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisDoubleData);
             return GetData(ETH_S7SimensCommands.TOOL_Z_LENGTH, (int)DataTypeEnum.DATATYPE_DOUBLE, func,ref result);
        }

        /// <summary>
        /// 获取刀具磨损半径
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetRadius(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisDoubleData);
            return GetData(ETH_S7SimensCommands.TOOL_RADIU, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
        }

        /// <summary>
        /// 获取刀沿位置
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetEDG(ref object result)
        {
            func = new Func<byte[], int, object>(AnalysisDoubleData);
            return GetData(ETH_S7SimensCommands.TOOL_EDG, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
        }

        /// <summary>
        /// 获取母线电压
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetVoltage(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVE_VOLTAGE, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取电机功率
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetLoad(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_LOAD1, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }

        }


        public bool ETH_S7SimensGetSPLoad1(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }

        }

        public bool ETH_S7SimensGetSPLoad2(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_SPLOAD2, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }

        }
        /// <summary>
        /// 获取电机温度
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetTemper(ref object result,byte flag)
        {
            try
            {
                ETH_S7SimensCommands.DRIVER_TEMPER[22] = flag;
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 获取电流
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public bool ETH_S7SimensGetCurrent(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_CURRENT, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }
        //public static byte[] R_Driver ={
        //    0x03, 0x00, 0x00, 0x1d,    //12+17=29
        //    0x02, 0xf0, 0x80, 0x32, 0x01,
        //    0x00, 0x00, 0x00, 0x14,
        //    0x00, 0x0c,  //10+2
        //    0x00, 0x00,
        //    0x04,
        //    0x01,
        //    0x12, 0x08, 0x82, 0xa1, 0x00, 0x25, 0x00, 0x01, 0x82, 0x01,//倒数第三位R驱动器下标，如R37[1],0X03=R37[2];倒数第五位为地址，如25为十进制37，第七位为轴，依次递增切换轴
        //    0x03, 0x00, 0x00, 0x07, 0x02, 0xf0, 0x00
        //};
        /// <summary>
        /// 获取驱动器
        /// </summary>
        /// <param name="no">无意义</param>
        /// <param name="adrFlag"></param>
        /// <param name="axisFlag">byte[] aixsflag = new byte[] { 0xa1, 0xa2, 0xa3 };//s,,x,y轴标识 </param>
        /// <param name="flag">下标标识</param>
        /// <param name="result"></param>
        /// <returns></returns>】
        public bool ETH_S7SimensGetRDriver(byte adrFlag, byte axisFlag,byte flag, ref object result)
        {
            try
            {
                ETH_S7SimensCommands.R_Driver[24] = adrFlag;
                ETH_S7SimensCommands.R_Driver[22] = axisFlag;
                ETH_S7SimensCommands.R_Driver[26] = flag;
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.R_Driver, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }catch
            {
                return false;
            }
            finally
            {

            }
        }
        /// <summary>
        /// 读取R变量
        /// </summary>
        /// <returns></returns>
        public bool ETH_S7SimensGetRValue(byte[] flag, ref object result)
        {
            try
            {
                ETH_S7SimensCommands.CNC_READ_R[26] = flag[0];
                ETH_S7SimensCommands.CNC_READ_R[25] = flag[1];
                func = new Func<byte[], int, object>(AnalysisDoubleData);
                return GetData(ETH_S7SimensCommands.CNC_READ_R, (int)DataTypeEnum.DATATYPE_DOUBLE, func, ref result);
            }
            catch
            {
                return true;
            }
          finally
            {

            }
        }

        bool GetData(byte[] byteMessage, int dataType,Func<byte[],int,object> func,ref object result)
        {
            socket.Send(byteMessage);
            Thread.Sleep(10);
            buffer = new byte[1024];
             receiveData = socket.Receive(buffer);
        
            //监听到消息
            if (receiveData > 0)
            {
              
                result= func(buffer,receiveData) ;
                return true;
            }
            else
            {
               return false;
            }
        }

       
        /// <summary>
        /// 解析字符串数据
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        string AnalysisStrData(byte[] byteMessage, int length)
        {
          
            datas = new byte[length] ;
            Array.Copy(byteMessage, datas, length);
            var ss=BitConverter.ToString(datas);
            var s=  (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
            if (s=="")
            {
                return "未加工";
            }
            else
            {
                return s;
            }
           


        }
        public bool ETH_S7SimensGetTemper_a1(ref object result)
        {
            try
            {
                func = new Func<byte[], int, object>(AnalysisFloatData);
                return GetData(ETH_S7SimensCommands.DRIVER_TEMPER, (int)DataTypeEnum.DATATYPE_FLOAT, func, ref result);
            }
            catch
            {
                return false;
            }
            finally
            {

            }
        }



        /// <summary>
        /// 解析操作模式
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        object AnalusisModeData(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
                if (datas[24] == 0x02)
                {
                    if (datas[31] == 0x00)
                    {
                        if (datas[25] == 0x00)
                        {
                            return SimensMode.JOG;
                        }
                        else if (datas[25] == 0x01)
                        {
                            return SimensMode.MDA;
                        }
                        else if (datas[25] == 0x02)
                        {
                            return SimensMode.AUTO;
                        }

                        else
                        {
                            return SimensMode.OTHER;
                        }
                    }
                    else if (datas[31] == 0x03)
                    {
                        return SimensMode.REFPOINT;
                    }
                    else
                    {
                        return SimensMode.OTHER;
                    }
                }
                else
                {
                    return SimensMode.OTHER;
                }

            }
            catch
            {
                return SimensMode.OTHER;
            }
        }

        /// <summary>
        /// 运行状态
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        object AnalusisStatusData(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
                if (datas[24] == 0x02)
                {
                    if ((datas[25] == 0x00) && (datas[31] == 0x05)) return SimensStatus.RESET;
                   else if ((datas[25] == 0x02) && (datas[31] == 0x02)) return  SimensStatus.STOP;
                   else if ((datas[25] == 0x01) && (datas[31] == 0x03)) return SimensStatus.START;
                   else if ((datas[25] == 0x01) && (datas[31] == 0x05)) return SimensStatus.SPENDLE_CW_CCW;
                    else return  SimensStatus.OTHER;
                }
                else
                {
                    return  SimensStatus.OTHER;
                }
            }
            catch
            {
                return  SimensStatus.OTHER;
            }
        }

        /// <summary>
        /// 解析小端序float
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        object AnalysisFloatData(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
               //Console.WriteLine(BitConverter.ToString(datas));
                var value = BitConverter.ToSingle(datas.Skip(25).Reverse().ToArray(), 0);
                return value;
            }
            catch(Exception ex)
            {
                return ((int)DataTypeEnum.DATATYPE_ERRO);
            }
        }

        /// <summary>
        /// 解析double 
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        object AnalysisDoubleData(byte[] byteMessage, int length)
        {
            object value = null;
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);;
                if (datas[3] == 33)
                {
                     value = BitConverter.ToDouble(datas.Skip(25).Take(8).ToArray(), 0);
                }
                else
                {
                    value = BitConverter.ToDouble(datas.Skip(0).ToArray(), 0);
                }
                return value;
            }
            catch (Exception ex)
            {
                return ((int)DataTypeEnum.DATATYPE_ERRO);
            }
        }

        /// <summary>
        /// 解析整型数据
        /// </summary>
        object AnalysisInt32Data(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
                var ss = BitConverter.ToString(datas);
                var s = (Encoding.GetEncoding("UTF-8").GetString(datas.Skip(25).ToArray())).Replace("\0", "");
                var value = BitConverter.ToInt16(datas.Skip(25).Take(2).ToArray(), 0);
            

                return value;
            }
            catch (Exception ex)
            {
                return (int)DataTypeEnum.DATATYPE_ERRO;

            }
        }

        /// <summary>
        /// 解析报警号
        /// </summary>
        object AnalysisAlarm(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
                Console.WriteLine(BitConverter.ToString(datas));
                var s = datas.Skip(25).Take(4).ToArray();
                var value = BitConverter.ToInt32(s, 0);


                return value;
            }
            catch (Exception ex)
            {
                return (int)DataTypeEnum.DATATYPE_ERRO;

            }
        }

        object AnalysisWorkSystem(byte[] byteMessage, int length)
        {
            try
            {
                datas = new byte[length];
                Array.Copy(byteMessage, datas, length);
                var s = datas.Skip(39).Take(2).ToArray();
                var value = BitConverter.ToInt16(s, 0);


                return value;
            }
            catch (Exception ex)
            {
                return (int)DataTypeEnum.DATATYPE_ERRO;

            }
        }



    }
}
