#define SERIAL_TEST   
//#define SELF_SETHOME
//#define NO_TCS_DEVICE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using QTIOBoardControl;
using MoonControl;
using System.Threading;
using System.IO;
using System.Data;

namespace QTInterface
{
    public class QT	
    {
        public const int plusPerRund = (3600 / 2);
        private const bool ON = false;
        private const bool OFF = true;
        public string m_devices_id;
        private string m_io_port;
        private string m_io_port_2;
        private int m_moon_port;
        private string m_moon_ip;
        private string m_moongray_ip;
        private string m_motor_port;
        private string m_door_ip;

        private string m_left_display_tcs_port;
        private string m_right_display_tcs_port;
        private string m_power_led_tcs_port;
        private string m_status_led_tcs_port;

        public byte m_gray_slave ;       //灰卡电机从站地址

        private int m_Proxpos30mm = 0;          // 鼻子灰卡30mm位置
        private int m_Proxpos20mm = 0;          // 鼻子灰卡40mm位置
        private int m_Proxpos13mm = 0;          // 鼻子灰卡50mm位置
        private int m_Cappos13mm = 0;          //眉部灰卡13mm位置
        private int m_Cappos20mm = 0;          //眉部灰卡20mm位置
        private int m_Cappos30mm = 0;          //眉部灰卡30mm位置
        private int m_GrayHome = 0;             //灰卡电机原点位置
        //private int m_IPDmotor_position = 1;    //旋钮电机扭矩
        //private int m_IPDmotor_PositiveRotation = 100;  //旋钮电机正转速度上限
        //private int m_IPDmotor_ReversingRotation = 100; //旋钮电机反转速度上限

        public byte m_headphone_slave;  //耳机插入电机从站地址
        private int m_headphone_pos = 10;    //耳机插入位置
        private int m_headphone_pos_re = 10;    //耳机回原扭矩
        private ushort m_headphone_acspeed = 10;  //耳机电机驱动+速度
        private ushort m_headphone_respeed = 10;  //耳机电机驱动-速度
        private double m_headphone_TimeOut = 10;    //耳机电机驱动超时时间
        private double m_headphone_StopRange_1 = 10;  //耳机电机停止判断条件一:扭矩限制百分比
        private double m_headphone_StopRange_2 = 10;    //耳机电机停止条件而:扭矩波动范围百分比
        private double m_headphone_Stopspeed = 0;    //耳机电机停止速度
        private double m_headphone_HomeTime = 1;    //电机回原时间



        private ushort m_graycard_speed = 0; //会卡电机速度
        private ushort m_graycard_acspeed = 0; //会卡电机加速度
        private ushort m_graycard_respeed = 0; //灰卡电机减速度

        public byte m_DoorSlave;                 // door电机从站地址

        private int m_DoorWorkPostion = 0;         //door电机测试位置
        private ushort m_DoorSpeed = 500;             //电机速度
        private ushort m_DoorAcspeed = 100;           //电机加速度
        private ushort m_DoorRespeed = 100;            //电机减速度
        private ushort m_DoorHome = 0;                     //门电机原点位置

        private ushort m_Time_out = 5;
        private QTIOBoard m_IoBoard =null;
        private QTIOBoard m_IoBoard_2;
        private TCS m_left_display_tcs;         //左边tcs
        private TCS m_right_display_tcs;        //右tcs
        private TCS m_power_led_tcs;            // 电源 led tcs
        private TCS m_status_led_tcs;            // 状态 led tcs

        private MoonMotor m_MoonMotorRTU;      //耳机电机控制对象
        private MoonMotor m_MoonGrayRTU;           //灰卡电机对象
        private MoonMotor m_MoonDoorRTU;           //door电机对象

        //private MoonMotorTCP m_MoonMotorTCP;      //耳机电机控制对象  /*P1.1接端更换电机未禾川电机 取消此对象*/
        private MoonMotorTCP m_MoonGrayTCP;           //灰卡电机对象
        private MoonMotorTCP m_MoonDoorTCP;           //door电机对象

        private MoonMotor m_Audiomotor;             //耳机电机控制对象

        public byte m_IPDWheelSlave;                   //旋钮电机从站地址

        public MoonMotor m_MoonIPDWheel;               //旋钮电机
        private string m_IPDmotor_port;                 //旋钮电机端口号
        private int m_Motor_PositiveRotation = 10;         //旋钮电机正转速度上限
        private int m_Motor_ReversingRotation = 10;        //旋钮点反转速度上限
        private int m_Motor_Position = 1;                   //旋钮电机扭矩百分比
        private int m_Motor_Reversing = 1;                  //旋钮电机反转扭矩百分比
        private int m_Motor_StoptSpeed = 10;                 //旋钮电机停止速度
        private double m_Torque_Range = 1;                 //扭矩范围
        private int m_Motor_IPD_TimeOut = 10000;            //旋钮电机超时时间，单位 ms
        private int m_Max_count = 10;                       //电机停转条件一最大计数数目
        private int m_Sample_length = 50;                   //电机停转条件二抽样数量
        private int m_Sample_range = 50;                    //电机停转条件二抽样极差

        public static DataTable dt;                  //写csv数据
        public static object Lock = new object();
        public Thread threadReSet;
        bool IsReset = true;
        public Thread threadSaferaster;
        bool Isstop = false;

        bool WorkClosedoor = false;

        public QT()
        {
            m_devices_id = ConfigHelper.Instance.Devices_ID;

            m_io_port = ConfigHelper.Instance.IOPort;
            m_io_port_2 = ConfigHelper.Instance.IOPort_2;
            m_moon_port = int.Parse(ConfigHelper.Instance.MotorPort);
            m_moon_ip = ConfigHelper.Instance.MotorIp;
            m_moongray_ip = ConfigHelper.Instance.MotorGrayIp;
            m_door_ip = ConfigHelper.Instance.MotorDoorIp;

            m_motor_port = ConfigHelper.Instance.Motor_ComPort;

            m_left_display_tcs_port = ConfigHelper.Instance.left_display_tcs_port;
            m_right_display_tcs_port = ConfigHelper.Instance.right_display_tcs_port;
            m_power_led_tcs_port = ConfigHelper.Instance.power_led_tcs_port;
            m_status_led_tcs_port = ConfigHelper.Instance.status_led_tcs_port;
            m_gray_slave = ConfigHelper.Instance.GraySlave;
            m_headphone_slave = ConfigHelper.Instance.HeadSlave;
            m_headphone_TimeOut = ConfigHelper.Instance.HeadTimeOut;
            m_headphone_StopRange_1 = ConfigHelper.Instance.HeadStopRange_1;
            m_headphone_StopRange_2 = ConfigHelper.Instance.HeadStopRange_2;
            m_headphone_Stopspeed = ConfigHelper.Instance.HeadStopSeepd;
            m_headphone_HomeTime = ConfigHelper.Instance.HeadHome;
            
            m_Proxpos30mm = ConfigHelper.Instance.ProxPos30mm;
            m_Proxpos20mm = ConfigHelper.Instance.ProxPos20mm;
            m_Proxpos13mm = ConfigHelper.Instance.ProxPos13mm;
            m_Cappos13mm = ConfigHelper.Instance.CapPos13mm;
            m_Cappos20mm = ConfigHelper.Instance.CapPos20mm;
            m_Cappos30mm = ConfigHelper.Instance.CapPos30mm;
            m_GrayHome = ConfigHelper.Instance.Gray_Home;
            m_DoorHome = ConfigHelper.Instance.Door_Home;
            m_headphone_pos = ConfigHelper.Instance.HeadPos;
            m_headphone_pos_re = ConfigHelper.Instance.HeadPos_re;
            m_headphone_acspeed = ConfigHelper.Instance.HeadAcspeed;
            m_headphone_respeed = ConfigHelper.Instance.HeadRespeed;
            m_graycard_speed = ConfigHelper.Instance.GraySpeed;
            m_graycard_acspeed = ConfigHelper.Instance.GrayAcspeed;
            m_graycard_respeed = ConfigHelper.Instance.GrayRespeed;

            m_DoorSlave = ConfigHelper.Instance.DoorSlave;                 // door电机从站地址
            m_DoorWorkPostion = ConfigHelper.Instance.DoorWorkPostion;         //door电机测试位置
            m_DoorSpeed = ConfigHelper.Instance.DoorSpeed;             //电机速度
            m_DoorAcspeed = ConfigHelper.Instance.DoorAcspeed;           //电机加速度
            m_DoorRespeed = ConfigHelper.Instance.DoorRespeed;            //电机减速度

            m_IPDmotor_port = ConfigHelper.Instance.Motor_IPDWheel;     //旋钮电机端口号
            m_IPDWheelSlave = ConfigHelper.Instance.IPDWheelSlave;     //旋钮电机从站地址
            m_Motor_PositiveRotation = ConfigHelper.Instance.Motor_PositiveRotation;    //旋钮电机正转速度上限
            m_Motor_ReversingRotation = ConfigHelper.Instance.Motor_ReversingRotation;    //旋钮电机反转速度上限
            m_Motor_Position = ConfigHelper.Instance.Motor_Position;    //旋钮电机扭矩百分比
            m_Motor_Reversing = ConfigHelper.Instance.Motor_Reversing;  //旋钮电机反转扭矩百分比
            m_Motor_StoptSpeed = ConfigHelper.Instance.Motor_StoptSpeed;  //旋钮电机停止速度
            m_Motor_IPD_TimeOut = ConfigHelper.Instance.Motor_IPD_TimeOut;  //旋钮电机运动超时时间，单位ms
            m_Torque_Range = ConfigHelper.Instance.Torque_Range;   // 设定扭矩范围

            m_Max_count = ConfigHelper.Instance.Max_count;   // 电机停转条件一最大计数数目
            m_Sample_length = ConfigHelper.Instance.Sample_length;   // 电机停转条件二抽样数量
            m_Sample_range = ConfigHelper.Instance.Sample_range;   // 电机停转条件二抽样极差
        }

        public int Max_count
        {
            get { return m_Max_count; }
            set
            {
                m_Max_count = value;
                ConfigHelper.Instance.Max_count = value;
            }
        }

        public int Sample_length
        {
            get { return m_Sample_length; }
            set
            {
                m_Sample_length = value;
                ConfigHelper.Instance.Sample_length = value;
            }
        }

        public int Sample_range
        {
            get { return m_Sample_range; }
            set
            {
                m_Sample_range = value;
                ConfigHelper.Instance.Sample_range = value;
            }
        }

        public int Motor_IPD_TimeOut
        {
            get { return m_Motor_IPD_TimeOut; }
            set
            {
                m_Motor_IPD_TimeOut = value;
                ConfigHelper.Instance.Motor_IPD_TimeOut = value;
            }
        }

        public int Motor_StoptSpeed
        {
            get { return m_Motor_StoptSpeed; }
            set
            {
                m_Motor_StoptSpeed = value;
                ConfigHelper.Instance.Motor_StoptSpeed = value;
            }
        }

        public double Torque_Range
        {
            get { return m_Torque_Range; }
            set
            {
                m_Torque_Range = value;
                ConfigHelper.Instance.Torque_Range = value;
            }
        }

        public int Motor_Position
        {
            get { return m_Motor_Position; }
            set
            {
                m_Motor_Position = value;
                ConfigHelper.Instance.Motor_Position = value;
            }
        }

        public int Motor_Reversing
        {
            get { return m_Motor_Reversing; }
            set
            {
                m_Motor_Reversing = value;
                ConfigHelper.Instance.Motor_Reversing = value;
            }
        }

        public int Motor_ReversingRotation
        {
            get { return m_Motor_ReversingRotation; }
            set
            {
                m_Motor_ReversingRotation = value;
                ConfigHelper.Instance.Motor_ReversingRotation = value;
            }
        }

        public int Motor_PositiveRotation
        {
            get { return m_Motor_PositiveRotation; }
            set
            {
                m_Motor_PositiveRotation = value;
                ConfigHelper.Instance.Motor_PositiveRotation = value;
            }
        }

        public int Cap_Pos30mm
        {
            get { return m_Cappos30mm; }
            set
            {
                m_Cappos30mm = value;
                ConfigHelper.Instance.CapPos30mm = value;
            }
        }

        public int Gray_Home
        {
            get { return m_GrayHome; }
            set
            {
                m_GrayHome = value;
                ConfigHelper.Instance.Gray_Home = value;
            }
        }

        public ushort Door_Home
        {
            get { return m_DoorHome; }
            set
            {
                m_DoorHome = value;
                ConfigHelper.Instance.Door_Home = value;
            }
        }


        public int Cap_Pos20mm
        {
            get { return m_Cappos20mm; }
            set
            {
                m_Cappos20mm = value;
                ConfigHelper.Instance.CapPos20mm = value;
            }
        }

        public int Cap_Pos13mm
        {
            get { return m_Cappos13mm; }
            set
            {
                m_Cappos13mm = value;
                ConfigHelper.Instance.CapPos13mm = value;
            }
        }

        public int Prox_Pos30mm
        {
            get { return m_Proxpos30mm; }
            set 
            { 
                m_Proxpos30mm = value;
                ConfigHelper.Instance.ProxPos30mm = value;
            }
        }

        public int Prox_Pos20mm
        {
            get { return m_Proxpos20mm; }
            set 
            {
                m_Proxpos20mm = value;
                ConfigHelper.Instance.ProxPos20mm = value;
            }
        }

        public int Prox_Pos13mm
        {
            get { return m_Proxpos13mm; }
            set 
            {
                m_Proxpos13mm = value;
                ConfigHelper.Instance.ProxPos13mm = value;
            }
        }

        public int Headphone_pos
        {
            get { return m_headphone_pos; }
            set 
            {
                m_headphone_pos = value;
                ConfigHelper.Instance.HeadPos = value;
            }
        }

        public int Headphone_pos_re
        {
            get { return m_headphone_pos_re; }
            set
            {
                m_headphone_pos_re = value;
                ConfigHelper.Instance.HeadPos_re = value;
            }
        }

        public ushort Headphone_acspeed
        {
            get { return m_headphone_acspeed; }
            set
            {
                m_headphone_acspeed = value;
                ConfigHelper.Instance.HeadAcspeed = value;
            }
        }

        public ushort Headphone_respeed
        {
            get { return m_headphone_respeed; }
            set
            {
                m_headphone_respeed = value;
                ConfigHelper.Instance.HeadRespeed = value;
            }
        }

        public double Headphone_HeadStopRange_1
        {
            get { return m_headphone_StopRange_1; }
            set
            {
                m_headphone_StopRange_1 = value;
                ConfigHelper.Instance.HeadStopRange_1 = value;
            }
        }

        public double Headphone_HeadStopRange_2
        {
            get { return m_headphone_StopRange_2; }
            set
            {
                m_headphone_StopRange_2 = value;
                ConfigHelper.Instance.HeadStopRange_2 = value;
            }
        }

        public double Headphone_HeadStopSeepd
        {
            get { return m_headphone_Stopspeed; }
            set
            {
                m_headphone_Stopspeed = value;
                ConfigHelper.Instance.HeadStopSeepd = value;
            }
        }
        
        public double Headphone_HeadTimeOut
        {
            get { return m_headphone_TimeOut; }
            set
            {
                m_headphone_TimeOut = value;
                ConfigHelper.Instance.HeadTimeOut = value;
            }
        }

        public double Headphone_HeadHomeTime
        {
            get { return m_headphone_HomeTime; }
            set
            {
                m_headphone_HomeTime = value;
                ConfigHelper.Instance.HeadHome = value;
            }
        }

        public int DoorWorkPostion
        {
            get { return m_DoorWorkPostion; }
            set 
            {
                m_DoorWorkPostion = value;
                ConfigHelper.Instance.DoorWorkPostion = value;
            }
        }

        /// <summary>
        /// 超时时间，单位ms
        /// </summary>
        public ushort Time_out
        {
            get { return m_Time_out; }
            set { m_Time_out = value; }
        }

        // <summary>
        // 获取io板卡对象
        // </summary>
        public QTIOBoard IoBoard
        {
            get
            {
                return m_IoBoard;
            }
        }
       
        // <summary>
        // 获取io板卡对象
        // </summary>
        public QTIOBoard IoBoard_2
        {
            get
            {
                return m_IoBoard_2;
            }
        }

        // <summary>
        // 获取左边display tcs对象
        // </summary>
        public TCS left_display_tcs
        {
            get
            {
                return m_left_display_tcs;
            }
        }

        // <summary>
        // 获取右边display tcs对象
        // </summary>
        public TCS right_display_tcs
        {
            get
            {
                return m_right_display_tcs;
            }
        }

        // <summary>
        // 获取power led tcs对象
        // </summary>
        public TCS power_led_tcs
        {
            get
            {
                return m_power_led_tcs;
            }
        }

        // <summary>
        // 获取状态 led tcs对象
        // </summary>
        public TCS status_led_tcs
        {
            get
            {
                return m_status_led_tcs;
            }
        }


        //// <summary>
        //// 获取耳机电机对象
        //// </summary>
        //public MoonMotorTCP MoonMotorTCP
        //{
        //    get { return m_MoonMotorTCP; }/*P1.1接端更换电机未禾川电机 取消此对象*/
        //}

        public MoonMotorTCP GrayMoonMotorTCP
        {
            get { return m_MoonGrayTCP; }
        }

        public MoonMotorTCP MoonDoorTCP
        {
            get { return m_MoonDoorTCP; }
        }

        public MoonMotor MoonIPDWheel
        {
            get { return m_MoonIPDWheel; }
        }

        // <summary>
        // 获取耳机电机对象
        // </summary>
        public MoonMotor MoonMotorRTU
        {
            get { return m_MoonMotorRTU; }
        }

        public MoonMotor GrayMoonMotorRTU
        {
            get { return m_MoonGrayRTU; }
        }

        public MoonMotor MoonDoorRTU
        {
            get { return m_MoonDoorRTU; }
        }

        public bool UnInit()
        {
            //Isfirst = false;
            //try
            //{
            //    if (threadReSet != null && threadReSet.IsAlive)
            //    {
            //        threadReSet.Abort();
            //    }
            //}
            //catch (Exception ex)
            //{

            //    FileWriter.WriteLog(ex.ToString());
            //}

            if (m_IoBoard != null)
            {
                m_IoBoard.Close();                
            }
            if (m_IoBoard_2 != null)
            {
                m_IoBoard_2.Close();
            }

            if (m_left_display_tcs != null)
            {
                m_left_display_tcs.Close();
            }
            if (m_right_display_tcs != null)
            {
                m_right_display_tcs.Close();
            }
            if (m_power_led_tcs != null)
            {
                m_power_led_tcs.Close();
            }
            if (m_status_led_tcs != null)
            {
                m_status_led_tcs.Close();
            }
            //if (m_MoonMotorTCP != null)
            //{
            //    m_MoonMotorTCP.Close();/*P1.1接端更换电机未禾川电机 取消此对象*/
            //}
            if (m_MoonGrayTCP != null)
            {
                m_MoonGrayTCP.Close();
            }
            if (m_MoonDoorTCP != null)
            {
                m_MoonDoorTCP.Close();
            }
            if (m_MoonIPDWheel != null)
            {
                m_MoonIPDWheel.Close();
            }
            
            return true;
        }

        bool Isfirst = false;
        int iSinit = 0;
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>        
        public bool Init(bool debug = false)
        {
            lock (Lock)
            {
                if (m_IoBoard == null)
                    m_IoBoard = new QTIOBoard(m_io_port);

                if (m_IoBoard_2 == null)
                    m_IoBoard_2 = new QTIOBoard(m_io_port_2);

                if (m_left_display_tcs == null)
                {
                    m_left_display_tcs = new TCS(m_left_display_tcs_port);
                    TCS.left = m_left_display_tcs_port;
                    m_left_display_tcs.ReadRGB();
                }

                if (m_right_display_tcs == null)
                {
                    m_right_display_tcs = new TCS(m_right_display_tcs_port);
                    TCS.right = m_right_display_tcs_port;
                    m_right_display_tcs.ReadRGB();
                    
                }

                if (m_power_led_tcs == null)
                {
                    m_power_led_tcs = new TCS(m_power_led_tcs_port);
                    TCS.power = m_power_led_tcs_port;
                    m_power_led_tcs.ReadRGB();
                    //if (m_power_led_tcs.SetRGB("03") == false)
                    //{
                    //    FileWriter.WriteLog("设置电源灯增益失败");
                    //    return false;  
                    //}
                }

                if (m_status_led_tcs == null)
                {
                    m_status_led_tcs = new TCS(m_status_led_tcs_port);
                    TCS.stust = m_status_led_tcs_port;
                    m_status_led_tcs.ReadRGB();
                    //if (m_status_led_tcs.SetRGB("03") == false)
                    //{
                    //    FileWriter.WriteLog("设置状态灯增益失败");
                    //    return false;
                    //}
                }

                if (m_MoonIPDWheel == null)
                    m_MoonIPDWheel = new MoonControl.MoonMotor(m_IPDmotor_port);
                if (m_devices_id == "1")
                {
                    //if (m_MoonMotorTCP == null)
                    //    m_MoonMotorTCP = new MoonMotorTCP(m_moon_ip, m_moon_port);/*P1.1接端更换电机未禾川电机 取消此对象*/

                    if (m_MoonGrayTCP == null)
                        m_MoonGrayTCP = new MoonMotorTCP(m_moongray_ip, m_moon_port);

                    if (m_MoonDoorTCP == null)
                        m_MoonDoorTCP = new MoonMotorTCP(m_door_ip, m_moon_port);
                }
                else if (m_devices_id == "2")
                {

                }
                else
                {
                    FileWriter.WriteLog("init时设备号错误，没有当前设备ID！");
                    return false;
                }
                bool status = false;
                //if (Isfirst)
                {
                    status = m_IoBoard.Open();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("m_IoBoard 端口:{0}打开失败", m_io_port));
                        return false;
                    }

                    status = m_IoBoard_2.Open();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("m_IoBoard_2 端口:{0}打开失败", m_io_port_2));
                        return false;
                    }

                    status = m_left_display_tcs.OpenPort();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("left_display_tcs 端口:{0}打开失败", m_left_display_tcs_port));
#if (!NO_TCS_DEVICE)
                        //return false;
#endif
                    }

                    status = m_right_display_tcs.OpenPort();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("right_display_tcs 端口:{0}打开失败", m_right_display_tcs_port));
#if (!NO_TCS_DEVICE)
                        //return false;
#endif
                    }

                    status = m_power_led_tcs.OpenPort();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("power_led_tcs 端口:{0}打开失败", m_power_led_tcs_port));
#if (!NO_TCS_DEVICE)
                        //return false;
#endif
                    }

                    //                status = m_status_led_tcs.OpenPort();
                    //                if (status == false)
                    //                {
                    //                    FileWriter.WriteLog(string.Format("status_led_tcs 端口:{0}打开失败", m_status_led_tcs_port));
                    //#if (!NO_TCS_DEVICE)
                    //                    //return false;
                    //#endif
                    //                }

                    status = m_MoonIPDWheel.OpenPort();
                    if (status == false)
                    {
                        FileWriter.WriteLog(string.Format("m_MoonMotor 端口:{0}打开失败", m_moon_port));
                        //return false;
                    }

                    if (m_devices_id == "2")
                    {
                        m_MoonDoorRTU = m_MoonIPDWheel;
                        m_MoonGrayRTU = m_MoonIPDWheel;
                        m_MoonMotorRTU = m_MoonIPDWheel;
                    }
                    if (m_devices_id == "1")
                    {
                        m_MoonMotorRTU = m_MoonIPDWheel;
                        //status = m_MoonMotorTCP.OpenPort();/*P1.1接端更换电机未禾川电机 取消此对象*/
                        //if (status == false)
                        //{
                        //    FileWriter.WriteLog(string.Format("m_MoonMotor 端口:{0}打开失败", m_moon_ip));
                        //    //return false;
                        //}

                        status = m_MoonGrayTCP.OpenPort();
                        if (status == false)
                        {
                            FileWriter.WriteLog(string.Format("m_MoonGrayTCP 端口:{0}打开失败", m_moongray_ip));
                            return false;
                        }

                        status = this.m_MoonDoorTCP.OpenPort();
                        if (status == false)
                        {
                            FileWriter.WriteLog(string.Format("m_MoonDoorTCP 端口:{0}打开失败", m_door_ip));
                            return false;
                        }
                    }
                }
                //this.LBreset();           // 光强板复位

                /*开启光栅检测线程,开门情况下触碰光栅电机停止*/
                try
                {
                    if (threadSaferaster != null && threadSaferaster.IsAlive)
                    {
                        Isstop = true;
                        threadSaferaster.Abort();
                    }
                    else
                    {
                        threadSaferaster = new Thread(Raster_stopTester);
                        threadSaferaster.IsBackground = true;
                        threadSaferaster.Start();
                    }
                }
                catch (Exception ex)
                {

                    FileWriter.WriteLog(ex.ToString());
                }

                if (debug) { return true; }
                #region MyRegion
                status = this.SDCardBack();
                if (status == false)
                {
                    FileWriter.WriteLog("光强版复位失败！");
                    return false;
                }
                else
                {
                    //status = this.GrayCdTestRest();
                    //if (status == false)
                    //{
                    //    return false;
                    //}
                    bool res = true;

                    if (m_IoBoard_2.GrayCardCylinder(ON) == false)
                    {
                        FileWriter.WriteLog("CrayCardCylinder 伸出失败，检查灰卡链接原因");
                        return false;
                    }
                    res = m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out);
                    if (res == false)
                    {
                        FileWriter.WriteLog("CrayCardCylinder 不在工位，检查灰卡感应器");
                        return false;
                    }
                    if (m_devices_id == "2")
                    {
                        status = m_MoonGrayRTU.RTU_MotorGoHome(m_gray_slave);
                        if (status == false)
                        {
                            FileWriter.WriteLog("m_MoonGrayTCP SetMoonsHome fail");
                            return false;
                        }

                        status = this.m_MoonDoorRTU.RTU_MotorGoHome(m_DoorSlave);
                        if (status == false)
                        {
                            FileWriter.WriteLog("m_MoonDoorTCP SetMoonsHome fail");
                            return false;
                        }


                        //status = this.m_MoonMotorRTU.RTU_MotorGoHome(m_headphone_slave);
                        //if (status == false)
                        //{
                        //    FileWriter.WriteLog("m_MoonMotor SetMoonsHome fail");
                        //    return false;
                        //}

                    }
                    else if (m_devices_id == "1")
                    {
                        status = m_MoonGrayTCP.SetMoonsHome(m_gray_slave);
                        if (status == false)
                        {
                            FileWriter.WriteLog("m_MoonGrayTCP SetMoonsHome fail");
                            return false;
                        }

                        status = this.m_MoonDoorTCP.SetMoonsHome(m_DoorSlave);
                        if (status == false)
                        {
                            FileWriter.WriteLog("m_MoonDoorTCP SetMoonsHome fail");
                            return false;
                        }
                        /*灰卡复位前检查灰卡当前位置 如果偏差太大返回false*/
                        double pos = m_MoonGrayTCP.GetMoonsLocalPosition(m_gray_slave);
                        if (Math.Abs(pos - m_GrayHome) > 10)
                        {
                            FileWriter.WriteLog("GrayMotor reset fail.   LocalPosition:" + pos.ToString() + " TargetPosition:" + m_GrayHome.ToString());
                            return false;
                        }
                        ////status = this.m_MoonMotorTCP.SetMoonsHome(m_headphone_slave);
                        //status = this.m_MoonMotorRTU.RTU_MotorGoHome(m_headphone_slave);
                        //if (status == false)
                        //{
                        //    FileWriter.WriteLog("m_MoonMotor SetMoonsHome fail");
                        //    return false;
                        //}
                    }
                    else
                    {
                        return false;
                    }

                    /*耳机电机回原*/
                    if (!AudioMotor_Home())
                    {
                        FileWriter.WriteLog("AudioMotor SetMoonsHome fail");
                        return false;
                    }

                    if (m_IoBoard_2.GrayCardCylinder(OFF) == false)
                    {
                        FileWriter.WriteLog("CrayCardCylinder 缩回失败，检查灰卡链接原因");
                        return false;
                    }
                    if (m_IoBoard.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
                    {
                        FileWriter.WriteLog("CrayCardCylinder 不在原位");
                        return false;
                    }
                }

                bool resu = true;
                if (PowerButtonReset() == false)
                {
                    FileWriter.WriteLog("电源气缸或电源键复位失败");
                    resu = false;
                }
                if (VolumeplusButtonReset() == false)
                {
                    FileWriter.WriteLog("音量键+复位失败");
                    resu = false;
                }
                if (VolumeSubButtonReset() == false)
                {
                    FileWriter.WriteLog("音量键-复位失败");
                    resu = false;
                }
                if (PogoCylinderReset() == false)
                {
                    FileWriter.WriteLog("pogo复位失败");
                    resu = false;
                }
                if (IPDCylinderReset() == false)
                {
                    FileWriter.WriteLog("IPD复位失败");
                    resu = false;
                }
                if (ElectromagnetClose() == false)
                {
                    FileWriter.WriteLog("电磁铁关闭失败");
                    resu = false;
                }
                if (PogoClose() == false)
                {
                    FileWriter.WriteLog("IPD充电关闭失败");
                    resu = false;
                }
                if (CoverBoardRest() == false)
                {
                    FileWriter.WriteLog("盖板抬起失败");
                    resu = false;
                }
                if (PowerCylinderRest() == false)
                {
                    FileWriter.WriteLog("电源气缸缩回失败");
                    resu = false;
                }
                if (PowerKeyRest() == false)
                {
                    FileWriter.WriteLog("电源键缩回失败");
                    resu = false;
                }

                if (m_IoBoard_2.IPDCCylinder(OFF))
                {
                    if (m_IoBoard.StatusCk(m_IoBoard_2.IPDOrigin, Time_out) == false)
                    {
                        FileWriter.WriteLog("IPDOrigin not in Origin location");
                        return false;
                    }
                }
                else
                {
                    FileWriter.WriteLog("IPDOrigin fail");
                    return false;
                }
                if (m_IoBoard_2.PogoPower(OFF))
                {
                    FileWriter.WriteLog("IPdPower success");
                }
                else
                {
                    FileWriter.WriteLog("IPDOrigin fail");
                    return false;
                }

                if (m_IoBoard_2.ElectromagnetPower(OFF))
                {
                    FileWriter.WriteLog("ElectromagnetPower success");
                }
                else
                {
                    FileWriter.WriteLog("ElectromagnetPower fail");
                    return false;
                }
                if (CameraOFF())
                {
                    FileWriter.WriteLog("Camera RGB 气缸 Origin success");
                }
                else
                {
                    FileWriter.WriteLog("Camera RGB 气缸 Origin fail");
                }
                #endregion

#if (SELF_SETHOME)            
            if (m_MoonMotor.SetMoonsHome(m_headphone_slave) == false)
            {
                Console.WriteLine("audio motor fail");
                //return false;
            }
            else if(m_MoonGrayTCP.SetMoonsHome(m_gray_slave) == false)
            {
                Console.WriteLine("gray motor fail");
                //return false;
            }
            else
            {
                Console.WriteLine("2 motor drive success");
            }
            
#endif
                return status;
            }

        }

        /// <summary>
        /// 检测盖板是否盖上，盖上返回true
        /// </summary>
        /// <returns></returns>
        public bool CheckCoverClose()
        {
            return m_IoBoard.CoverClose();
        }

        /// <summary>
        /// 检测两个按键是否按下
        /// </summary>
        /// <returns>true按下按键，否则没有按下</returns>		
        public bool CheckedStart()
        {
            //检测盖板是否已经盖上及start按键已按下
            //if (m_IoBoard.CoverClose() == false)
            //{
            //    //FileWriter.WriteLog("检测盖板状态:盖板未盖上，不能启动测试");//写入到LOG文件
            //    return false;
            //}
            lock (Lock)
            {
                
                //if (IsReset)
                //{
                    if (m_IoBoard.Start() == true)
                    {
                        
                        //Thread.Sleep(5000);
                        //return true;
                        Thread.Sleep(500);     // 等待0.5s在检测一次，避免误操作
                        bool flags_start = m_IoBoard.Start();
                        if (flags_start)
                        {
                            //检测产品到位
                            bool dut_1 = m_IoBoard.CheckDUT_1();
                            bool dut_2 = m_IoBoard_2.CheckDUT_2();
                            if (!dut_1 || !dut_2)
                            {
                                FileWriter.WriteLog("产品检测到位失败：CheckDUT_1==" + dut_1.ToString() + " CheckDUT_2"+dut_2.ToString());
                                return false;
                            }

                            if (!IsReset)
                            {
                                FileWriter.WriteLog("复位失败，重新复位中！");
                                if (this.Reset())
                                {
                                    FileWriter.WriteLog("复位结束，成功");
                                }
                                else
                                {
                                    FileWriter.WriteLog("复位结束，失败");
                                    return false;
                                }

                            }
                        }
                        return flags_start;
                    }
                    else
                    {
                        return false;
                    }
                //}
                //FileWriter.WriteLog("复位没结束，请不要点开始！");
                return false;
            }
        }

        /// <summary>
        /// 检查产品到位
        /// </summary>
        /// <returns></returns>
        public bool check_dut()
        {
            try
            {
                //检测产品到位
                bool dut_1 = m_IoBoard.CheckDUT_1();
                bool dut_2 = m_IoBoard_2.CheckDUT_2();
                if (!dut_1 || !dut_2)
                {
                    FileWriter.WriteLog("产品检测到位失败：CheckDUT_1==" + dut_1.ToString() + " CheckDUT_2" + dut_2.ToString());
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                FileWriter.WriteLog("产品检测到位异常 Exception：" +ex.Message);
            }
            return false;
        }

        public bool CheckedCrayOrigin()
        {
            if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 检测复位按键是否按下
        /// </summary>
        /// <returns>true按下按键，否则没有按下</returns>		
        public bool CheckedReset()
        {
            if (m_IoBoard.Reset() == true)
            {
                Thread.Sleep(500);     // 等待0.5s在检测一次，避免误操作
                return m_IoBoard.Reset();
                //return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检测急停按键是否按下
        /// </summary>
        /// <returns>true按下按键，否则没有按下</returns>		
        public bool CheckedEStop()
        {
            if (m_IoBoard.E_Stop() == true)
            {
                return m_IoBoard.E_Stop();
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 光强版复位
        /// </summary>
        /// <returns></returns>
        public bool LBreset()
        {
            //var status = m_IoBoard.LBreset(ON);
            //Thread.Sleep(2000);
            //status = m_IoBoard.LBreset(OFF);
            //return status;
            return true;
        }

        /// <summary>
        /// 开门
        /// </summary>
        /// <returns></returns>
        public bool OpenDoor()
        {
            for (int a = 0; a < 2; a++)
            {
                if (m_devices_id == "1")
                {
                    if (this.m_MoonDoorTCP.SetMoonsHome(m_DoorSlave))
                    {
                        return true;
                    }
                    else
                    {
                        FileWriter.WriteLog("door motor move 0 fail");
                        return false;
                    }
                }
                else if (m_devices_id == "2")
                {
                    Console.WriteLine("门 RTU_MotorGoHome！");
                    if (this.m_MoonDoorRTU.RTU_MotorGoHome_1(m_DoorSlave))
                    {
                        return true;
                    }
                    else
                    {
                        FileWriter.WriteLog("door motor move 0 fail");
                        return false;
                    }
                }
                else { FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false; }
            }
            return false;
        }

        /// <summary>
        /// 检测光栅
        /// </summary>
        public void Check_GS()
        {
            //Task th = Task.Run(new Action(() =>
            //{
            //    while (true)
            //    {
            //        bool rett = m_IoBoard_2.CheckGS();
            //        //Console.WriteLine("CheckGS():=====" + rett);
            //        if (!rett && this.WorkClosedoor)
            //        {
            //            Console.WriteLine("SetMoonsImmediatelyStop");
            //            bool ret = this.m_MoonDoorTCP.SetMoonsImmediatelyStop(m_DoorSlave);
            //            Console.WriteLine("SetMoonsImmediatelyStop====ret"+ret.ToString());
            //            this.OpenDoor();
            //        }
            //        Thread.Sleep(10);
            //    }
            //}));
        }
        
        /// <summary>
        /// 关门
        /// </summary>
        /// <returns></returns>
        public bool CloseDoor()
        {
            /*如果RGB盖板不在工作位则不允许关门*/
            if (!m_IoBoard_2.StatusCk(m_IoBoard_2.RGBTest, Time_out))
            {
                FileWriter.WriteLog("RGB盖板不在工作位不允许关门"); return false;
            }

            this.WorkClosedoor = true;
            bool result = false;
            Console.WriteLine("CloseDoor");
            if (m_devices_id == "1")
            {
                result = this.m_MoonDoorTCP.MoveLine(m_DoorSlave, m_DoorWorkPostion, m_DoorSpeed, m_DoorAcspeed, m_DoorRespeed,false);
                while (!this.m_MoonDoorTCP.WaitStopTCP(m_DoorSlave,0.1))
                {
                    bool rett = m_IoBoard_2.Check_safe_raster();
                    bool status = m_IoBoard_2.StatusCk(m_IoBoard_2.RGBTest, Time_out);
                    Thread.Sleep(150);
                    rett = m_IoBoard_2.Check_safe_raster();
                    Console.WriteLine("CheckGS():=====" + rett);
                    if ((!rett && this.WorkClosedoor) || (status == false))
                    {
                        if (EsStop != null)
                        {
                            EsStop(true);
                        }
                        Console.WriteLine("SetMoonsImmediatelyStop");
                        bool ret = this.m_MoonDoorTCP.SetMoonsImmediatelyStop(m_DoorSlave);
                        Console.WriteLine("SetMoonsImmediatelyStop====ret" + ret.ToString());
                        FileWriter.WriteLog("触碰到光栅 开门！CheckGS:"+ret);
                        this.OpenDoor();
                    }
                    Thread.Sleep(5);
                }
            }
            else if (m_devices_id == "2")
            {
                result = this.m_MoonDoorRTU.RTU_MotorRun(m_DoorSlave,m_DoorWorkPostion);
            }
            else 
            { 
                FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false;
            }
            Console.WriteLine("CloseDoor  OK");
            this.WorkClosedoor = false;
            FileWriter.WriteLog("door motor move workpostion result:"+result);
            return result;
        }

        public event Action<bool> EsStop;

        /// <summary>
        /// 开门测试的情况下，触发光栅停止所有电机
        /// </summary>
        private void Raster_stopTester()
        {
            while (!Isstop)
            {
                try
                {
                    //1.检查光栅是否触发
                    bool rett = m_IoBoard_2.Check_safe_raster();
                    Thread.Sleep(200);
                    rett = m_IoBoard_2.Check_safe_raster();
                    if (!rett)
                    {
                        //检查门是否到位
                        int door_pos = this.m_MoonDoorTCP.GetMoonsLocalPosition(m_DoorSlave);
                        Console.WriteLine("door_pos:" + door_pos.ToString());
                        if (Math.Abs(door_pos - m_DoorHome) < 10 && Math.Abs(door_pos - m_DoorWorkPostion) > 10)
                        {
                            if (EsStop != null)
                            {
                                 EsStop(true);
                            }
                            //1.停止灰卡电机
                            bool gray = this.m_MoonGrayTCP.SetMoonsImmediatelyStop(m_gray_slave);
                            FileWriter.Write_ERR_Log("停止灰卡电机");
                            //2.停止门电机
                            bool door = this.m_MoonDoorTCP.SetMoonsImmediatelyStop(m_DoorSlave);
                            FileWriter.Write_ERR_Log("停止门电机");
                            //3.停止耳机电机
                            bool audio = this.m_MoonIPDWheel.SetMoonsPosition(m_headphone_slave, 0);
                            FileWriter.Write_ERR_Log("停止耳机电机");
                            //4.停止IPD电机
                            bool ipdWheel = this.m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, 0);
                            FileWriter.Write_ERR_Log("停止IPD电机");
                        }
                    }
                }
                catch (Exception err)
                {
                    FileWriter.Write_ERR_Log(err.ToString());
                }
                Thread.Sleep(80);
            }
        }

        /// <summary>
        /// 状态灯打开红色
        /// </summary>
        /// <returns></returns>
        public bool StatusLightRed()
        {
            return this.IoBoard.TriColorRed(ON);
            //return this.IoBoard.StatusLightRed(ON);
        }

        /// <summary>
        /// 三色灯-蜂鸣器 Open
        /// </summary>
        /// <returns></returns>
        public bool TriColorBuzzer_Open()
        {
            return this.IoBoard.TriColorBuzzer(ON);
        }

        /// <summary>
        /// 三色灯-蜂鸣器 Closed
        /// </summary>
        /// <returns></returns>
        public bool TriColorBuzzer_Close()
        {
            return this.IoBoard.TriColorBuzzer(OFF);
        }

        /// <summary>
        /// 状态灯打开yellow
        /// </summary>
        /// <returns></returns>
        public bool StatusLightYellow()
        {
            bool status = true;
            status = this.IoBoard.TriColorRed(ON);
            status = this.IoBoard.TriColorGreen(ON);
            return status;
        }

        /// <summary>
        /// 状态灯打开绿色
        /// </summary>
        /// <returns></returns>
        public bool StatusLightGreen()
        {
            return this.IoBoard.TriColorGreen(ON);
            //return this.IoBoard.StatusLightGreen(ON);
        }

        /// <summary>
        /// 状态灯关闭
        /// </summary>
        /// <returns></returns>
        public bool StatusLightOff()
        {
            return this.IoBoard.TriColorClose(OFF);
        }

        public bool Ping_VBUS_On()
        {
            return this.m_IoBoard_2.Ping_VBUS(ON);
        }

        public bool Ping_VBUS_Off()
        {
            return this.m_IoBoard_2.Ping_VBUS(OFF);
        }

        public bool Ping_CC_On()
        {
            return this.m_IoBoard_2.Ping_CC(ON);
        }

        public bool Ping_CC_Off()
        {
            return this.m_IoBoard_2.Ping_CC(OFF);
        }

        public bool Ping_GND_On()
        {
            return this.m_IoBoard_2.Ping_GND(ON);
        }

        public bool Ping_GND_Off()
        {
            return this.m_IoBoard_2.Ping_GND(OFF);
        }

        /// <summary>
        /// typc 插入
        /// </summary>
        /// <returns></returns>
        public bool TypeCInsertion()
        {
            if (m_IoBoard_2.TypeCCylinder(ON))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.TypeCTest, Time_out) == false)
                {
                    FileWriter.WriteLog("TypeC not in Test location");

                    m_IoBoard_2.TypeCCylinder(ON);
                    Thread.Sleep(200);
                    if (m_IoBoard_2.StatusCk(m_IoBoard_2.TypeCTest, Time_out) == false)
                    {
                        FileWriter.WriteLog("TypeC not in Test location Again");
                        return false;
                    }
                    return true;
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("TypeCInsertion fail");
                return false;
            }
        }

        /// <summary>
        /// typc 拔出
        /// </summary>
        /// <returns></returns>
        public bool TypeCRest() 
        {
            m_IoBoard_2.TypeCCylinder(OFF);
            m_IoBoard_2.TypeCCylinder(OFF);
            if (m_IoBoard_2.TypeCCylinder(OFF))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.TypeCOrigin, Time_out) == false)
                {
                    FileWriter.WriteLog("TypeC not in Origin location");
                    return false;
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("TypeCRest fail");
                return false;
            }

            //m_IoBoard.TypeCCylinder(OFF);
            //return m_IoBoard.StatusCk(m_IoBoard.TypeCOrigin, Time_out);
        }

        /// <summary>
        /// 打开电磁铁
        /// </summary>
        /// <returns></returns>
        public bool ElectromagnetOpen()
        {
            if (m_IoBoard_2.ElectromagnetPower(ON) == false)
            {
                if (m_IoBoard_2.ElectromagnetPower(ON) == false)
                {
                    return false;
                }
            }
            return true;
            //return m_IoBoard_2.ElectromagnetPower(ON);
        }

        /// <summary>
        /// 关闭电磁铁
        /// </summary>
        /// <returns></returns>
        public bool ElectromagnetClose()
        {
            if (m_IoBoard_2.ElectromagnetPower(OFF) == false)
            {
                if (m_IoBoard_2.ElectromagnetPower(OFF) == false)
                {
                    return false;
                }
            }
            return true;
            //return m_IoBoard_2.ElectromagnetPower(OFF);
        }

        /// <summary>
        /// Pogo通电(快充)
        /// </summary>
        /// <returns></returns>
        public bool PogoOpen()
        {
            bool status_GND = Ping_GND_On();
            bool status_VBUS = Ping_VBUS_On();
            //bool status_CC = Ping_CC_On();
            //if (m_IoBoard_2.PogoPower(ON) == false)
            //{
            //    if (m_IoBoard_2.PogoPower(ON) == false)
            //    {
            //        return false;
            //    }
            //}
            return status_GND&&status_VBUS;
            //return m_IoBoard_2.PogoPower(ON);
        }

        /// <summary>
        /// Pogo断电(快充)
        /// </summary>
        /// <returns></returns>
        public bool PogoClose()
        {
            bool status_GND = Ping_GND_Off();
            bool status_VBUS = Ping_VBUS_Off();
            bool status_CC = Ping_CC_Off();
            //if (m_IoBoard_2.PogoPower(OFF)==false)
            //{
            //    if (m_IoBoard_2.PogoPower(OFF)==false)
            //    {
            //        return false;
            //    }
            //}
            return status_GND && status_VBUS && status_CC;
        }

        /// <summary>
        /// Pogo滑台 伸出
        /// </summary>
        /// <returns></returns>
        public bool PogoCylinderTest()
        {
            if (m_IoBoard_2.PogoCylinder(ON))
            {
                if (m_IoBoard.StatusCk(m_IoBoard.PogoTest, Time_out) == false)
                {
                    FileWriter.WriteLog("Pogo not in Test location");

                    if (m_IoBoard_2.PogoCylinder(ON))
                    {
                        //Thread.Sleep(200);
                        if (m_IoBoard.StatusCk(m_IoBoard.PogoTest, Time_out) == false)
                        {
                            FileWriter.WriteLog("Pogo not in Test location Again");
                            return false;
                        }
                        return true;
                    }
                    else 
                    {
                        FileWriter.WriteLog("PogoRest fail Again");
                    }
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("PogoRest fail");
                return false;
            }
            return true;
        }

        /// <summary>
        /// Pogo滑台 复位
        /// </summary>
        /// <returns></returns>
        public bool PogoCylinderReset()
        {
            //m_IoBoard.PogoCylinder(OFF);
            //m_IoBoard.PogoCylinder(OFF);
            if (m_IoBoard_2.PogoCylinder(OFF))
            {
                return true;
                if (m_IoBoard.StatusCk(m_IoBoard.PogoOrigin, Time_out) == false)
                {
                    FileWriter.WriteLog("Pogo not in Origin location");
                    //加防护
                    if (m_IoBoard_2.PogoCylinder(OFF))
                    {
                        //Thread.Sleep(200);
                        if (m_IoBoard.StatusCk(m_IoBoard.PogoOrigin, Time_out) == false)
                        {
                            FileWriter.WriteLog("Pogo not in Origin location Again");
                            return false;
                        }
                        else
                            return true;
                    }
                    else
                    {
                        FileWriter.WriteLog("PogoCylinderReset fail Again");
                        return false;
                    }
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("PogoCylinderReset fail");
                return false;
            }

            //m_IoBoard.TypeCCylinder(OFF);
            //return m_IoBoard.StatusCk(m_IoBoard.TypeCOrigin, Time_out);
        }

        /// <summary>
        /// 耳机插入
        /// </summary>
        /// <returns></returns>[1]
        public bool AudioInsertion()
        {
            #region P1.1
            return AudioMotor2();
            #endregion

            #region P1
            //if (m_devices_id == "1")
            //{
            //    ////电机控制            
            //    //var status = m_MoonMotorTCP.MoveLine(m_headphone_slave, m_headphone_pos, m_headphone_speed, m_headphone_acspeed, m_headphone_respeed);
            //    //if (status)
            //    //{
            //    //    return true;/*P1.1接端更换电机未禾川电机 取消此对象*/
            //    //}
            //    //else
            //    //{
            //    //    FileWriter.WriteLog("AudioInsertion move fail");
            //    //    return false;
            //    //}
            //    return false;
            //}
            //else if (m_devices_id == "2")
            //{
            //    //电机控制            
            //    var status = m_MoonMotorRTU.RTU_MotorRun(m_headphone_slave, m_headphone_pos);
            //    if (status)
            //    {

            //        return true;
            //    }
            //    else
            //    {
            //        FileWriter.WriteLog("AudioInsertion move fail");
            //        return false;
            //    }
            //}
            //else { FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false; }
            #endregion

            return false;
        }

        /// <summary>
        /// 耳机插入拔出
        /// </summary>
        /// <returns></returns>
        public bool AudioBack()
        {

            #region P1
            ////电机控制
            ////return m_MoonMotor.SetMoonsHome(m_headphone_slave);   

            ////var status = m_MoonMotor.MoveLine(m_headphone_slave, 0, m_headphone_speed, m_headphone_acspeed, m_headphone_respeed);
            //if (m_devices_id == "2")
            //{
            //    var status = m_MoonMotorRTU.RTU_MotorGoHome(m_headphone_slave);
            //    if (status == false)
            //    {
            //        return false;
            //    }
            //    if (status)
            //    {
            //        return true;
            //    }
            //    else
            //    {
            //        FileWriter.WriteLog("AudioBack move fail");
            //        return false;
            //    }
            //}
            //else if (m_devices_id == "1")
            //{
            //    //ushort re_m_headphone_speed = 1500;/*P1.1接端更换电机未禾川电机 取消此对象*/
            //    //ushort re_m_headphone_acspeed = 1500;
            //    //ushort re_m_headphone_respeed = 1500;
            //    //var status = m_MoonMotorTCP.MoveLine(m_headphone_slave, 0, re_m_headphone_speed, re_m_headphone_acspeed, re_m_headphone_respeed);
            //    //if (status)
            //    //{
            //    //    return true;
            //    //}
            //    //else
            //    //{
            //    //    FileWriter.WriteLog("AudioBack move fail");
            //    //    return false;
            //    //}
            //    return false;
            //}
            //else { FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false; }
            #endregion

            #region P1.1
            return AudioMotor_Home();
            #endregion
            
        }

        /// <summary>
        /// Power button 测试
        /// </summary>
        /// <returns></returns>
        public bool PowerButtonTest()
        {
            //m_IoBoard.PowerCylinder(ON);
            //return m_IoBoard.StatusCk(m_IoBoard.PowerTest, Time_out);
            if (m_IoBoard.PowerOutCylinder(ON))
            {
                if (m_IoBoard.PowerCylinder(ON))
                {
                    Thread.Sleep(500);
                    return true;
                }
                else
                {
                    FileWriter.WriteLog("PowerButtonTest not in test location");
                    return false;
                }
            }
            else
            {
                FileWriter.WriteLog("PowerOutCylinder not in test location");
                return false;
            }

        }

        /// <summary>
        /// Power button 复位
        /// </summary>
        /// <returns></returns>[3]
        public bool PowerButtonReset()
        {
            //m_IoBoard.PowerCylinder(OFF);
            //return m_IoBoard.StatusCk(m_IoBoard.PowerOrigin,Time_out);
            if (m_IoBoard.PowerOutCylinder(OFF))
            {
                if (m_IoBoard.PowerCylinder(OFF))
                {
                    Thread.Sleep(500);
                    return true;
                }
                else
                {
                    FileWriter.WriteLog("PowerButtonReset not in org location");
                    return false;
                }
            }
            else
            {
                FileWriter.WriteLog("PowerOutCylinder not in test location");
                return false;
            }

        }

        /// <summary>
        ///  volume button 加测试
        /// </summary>
        /// <returns></returns>
        public bool VolumeplusButtonTest()
        {
            //m_IoBoard.VolAddCylinder(ON);
            //return m_IoBoard.StatusCk(m_IoBoard.VolAddTest, Time_out);

            if (m_IoBoard.VolAddCylinder(ON))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard.VolAddTest, Time_out) == false)
                {
                    if (m_IoBoard.VolAddCylinder(ON))
                    {
                        if (m_IoBoard_2.StatusCk(m_IoBoard.VolAddTest, Time_out) == false)
                        {
                            //res = false;
                            FileWriter.WriteLog("VolumeplusButtonTest reset fail:VolAddCylinder not in Test location");
                            return false;
                        }
                        //Thread.Sleep(500);
                        return true;
                    }
                    //res = false;
                    FileWriter.WriteLog("VolumeplusButtonTest Write fail");
                    return false;
                }
                
                //Thread.Sleep(500);
                return true;
            }
            else
            {
                FileWriter.WriteLog("VolumeplusButtonTest Write fail");
                //加防护
                if (m_IoBoard.VolAddCylinder(ON))
                {
                    if (m_IoBoard_2.StatusCk(m_IoBoard.VolAddTest, Time_out) == false)
                    {
                        //res = false;
                        FileWriter.WriteLog("PowerCylinderTest reset fail:VolAddCylinder not in Test location");
                        return false;
                    }
                
                    //Thread.Sleep(500);
                    return true;

                }
                else
                {
                    FileWriter.WriteLog("VolumeplusButtonTest not in test location Again");
                    return false;
                }
            }
        }

        /// <summary>
        /// volume button加 复位
        /// </summary>
        /// <returns></returns>[4]
        public bool VolumeplusButtonReset()
        {
            //m_IoBoard.VolAddCylinder(OFF);
            //return m_IoBoard.StatusCk(m_IoBoard.VolAddOrigin, Time_out);
            //m_IoBoard.VolAddCylinder(OFF);
            //Thread.Sleep(500);
            //return true;
            if (m_IoBoard.VolAddCylinder(OFF))
            {
                //Thread.Sleep(500);
                return true;

            }
            else
            {
                FileWriter.WriteLog("VolumeplusButtonReset not in org location");
                //加防护
                if (m_IoBoard.VolAddCylinder(OFF))
                {
                    //Thread.Sleep(500);
                    return true;

                }
                else
                {
                    FileWriter.WriteLog("VolumeplusButtonReset not in org location two");
                    return false;
                }
                return false;
            }
        }

        /// <summary>
        ///  volume button减测试
        /// </summary>
        /// <returns></returns>
        public bool VolumeSubButtonTest()
        {
            if (m_IoBoard.VolReduceCylinder(ON))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard.VolReduTest, Time_out) == false)
                {
                    if (m_IoBoard.VolReduceCylinder(ON))
                    {
                        if (m_IoBoard_2.StatusCk(m_IoBoard.VolReduTest, Time_out) == false)
                        {
                            //res = false;
                            FileWriter.WriteLog("VolumeSubButtonTest reset fail:GrayCard not in Test location");
                            return false;
                        }

                        //Thread.Sleep(500);
                        return true;
                    }
                    //res = false;
                    //FileWriter.WriteLog("VolumeSubButtonTest reset fail:GrayCard not in Test location");
                    //return false;
                }
                
                //Thread.Sleep(500);
                return true;
            }
            else
            {
                FileWriter.WriteLog("VolumeSubButtonTest not in test location");
                //加防护
                if (m_IoBoard.VolReduceCylinder(ON))
                {
                    if (m_IoBoard_2.StatusCk(m_IoBoard.VolReduTest, Time_out) == false)
                    {
                        //res = false;
                        FileWriter.WriteLog("VolumeSubButtonTest reset fail:GrayCard not in Test location");
                        return false;
                    }
                    //Thread.Sleep(500);
                    return true;
                }
                else
                {
                    FileWriter.WriteLog("VolumeSubButtonTest not in test location Again");
                    return false;
                }
                return false;
            }
        }

        /// <summary>
        /// volume button减 复位
        /// </summary>
        /// <returns></returns>[5]
        public bool VolumeSubButtonReset()
        {
            if (m_IoBoard.VolReduceCylinder(OFF))
            {
                //Thread.Sleep(500);
                return true;
            }
            else
            {
                FileWriter.WriteLog("VolumeSubButtonReset not in test location");
                //加防护
                if (m_IoBoard.VolReduceCylinder(OFF))
                {
                    //Thread.Sleep(500);
                    return true;
                }
                else
                {
                    FileWriter.WriteLog("VolumeSubButtonReset not in test location Again");
                    return false;
                }
                return false;
            }
        }

        /// <summary>
        /// IPD旋钮气缸 复位
        /// </summary>
        /// <returns></returns>[5]
        public bool IPDCylinderReset()
        {
            if (m_IoBoard_2.IPDCCylinder(OFF))
            {
                //Thread.Sleep(500);
                return true;
            }
            else
            {
                FileWriter.WriteLog("IPDCylinderReset not in test location");
                //加防护
                if (m_IoBoard_2.IPDCCylinder(OFF))
                {
                    //Thread.Sleep(500);
                    return true;
                }
                else
                {
                    FileWriter.WriteLog("IPDCylinderReset not in test location Again");
                    return false;
                }
                return false;
            }
        }

        /// <summary>
        /// IPD旋钮气缸 伸出
        /// </summary>
        /// <returns></returns>[5]
        public bool IPDCylinderTest()
        {
            //while()
            if (m_IoBoard_2.IPDCCylinder(ON))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.IPDTest, Time_out) == false)
                {
                    FileWriter.WriteLog("IPDCylinderTest not in Test location");

                    if (m_IoBoard_2.IPDCCylinder(ON))
                    { 
                        //Thread.Sleep(200);
                        if (m_IoBoard_2.StatusCk(m_IoBoard_2.IPDTest, Time_out) == false)
                        {
                            FileWriter.WriteLog("IPDCylinderTest not in Test location Again");
                            return false;
                        }
                    }
                    else
                    {
                        FileWriter.WriteLog("IPDCylinderTest fail Again");
                        return false;
                    }
                    return true;
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("IPDCylinderTest fail");
                return false;
            }
        }

        /// <summary>
        /// left Display test 
        /// </summary>
        /// <returns></returns>
        public int [] LeftDisplayTest()
        {
            if (this.m_left_display_tcs != null)
            {
                //return this.m_left_display_tcs.ReadLux();
                return this.m_left_display_tcs.ReadRGB();
            }
            else
            {
                return null;
            }
        }
        public double LeftDisplayLux()
        {
            if (this.m_left_display_tcs != null)
            {
                return this.m_left_display_tcs.ReadLux();
            }
            else
            {
                return -1;
            }
        }
        
        /// <summary>
        /// right Display test 
        /// </summary>
        /// <returns></returns>
        public int[] RightDisplayTest()
        {
            if (this.m_right_display_tcs != null)
            {
                //return this.m_right_display_tcs.ReadLux();
                var datas = this.m_right_display_tcs.ReadRGB();
                if (datas == null)
                {
                    return this.m_right_display_tcs.ReadRGB();
                }
                else
                {
                    return datas;
                }
            }
            else
            {
                //return -1;
                return null;
            }
        }
        public double RightDisplayLux()
        {
            if (this.m_right_display_tcs != null)
            {
                var data = this.m_right_display_tcs.ReadLux();
                if (data == -1)
                {
                    return this.m_right_display_tcs.ReadLux();
                }
                else
                {
                    return data;
                }
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// power LED test 
        /// </summary>
        /// <returns></returns>
        public int[] PowerLedTest()
        {
            if (this.m_power_led_tcs != null)
            {
                var datas = this.m_power_led_tcs.ReadRGB();
                if (datas == null)
                {
                    return this.m_power_led_tcs.ReadRGB();
                }
                else
                {
                    return datas;
                }
            }
            else
            {
                return null;
            }
        }
        public double PowerLedLux()
        {
            if (this.m_power_led_tcs != null)
            {
                var data = this.m_power_led_tcs.ReadLux();
                if (data == -1)
                {
                    return this.m_power_led_tcs.ReadLux();
                }
                else
                    return data;
            }
            else
            {
                return -1;
            }            
        }
        
        /// <summary>
        /// status LED test 
        /// </summary>
        /// <returns></returns>
        public int[] StatusLedTest()
        {
            if (this.m_status_led_tcs != null)
            {
                //return this.m_status_led_tcs.ReadRGB();
                var datas = this.m_status_led_tcs.ReadRGB();
                if (datas == null)
                {
                    return this.m_status_led_tcs.ReadRGB();
                }
                else
                {
                    return datas;
                }
            }
            else
            {
                return null;
            }
        }
        public double StatusLenLux()
        {
            if (this.m_status_led_tcs != null)
            {
                //return this.m_status_led_tcs.ReadLux();
                var data = this.m_status_led_tcs.ReadLux();
                if (data == -1)
                {
                    this.LBreset();
                    return this.m_status_led_tcs.ReadLux();
                }
                else
                    return data;
            }
            else
            {
                return -1;
            }
        }

        # region 删除模块

        //    /// <summary>
    //    /// sd 卡插入
    //    /// </summary>
    //    /// <returns></returns>
    //    public bool SDCardInsertion()
    //    {
    //        bool res = true;

    ////        m_IoBoard.GrayCardCylinder(OFF);
    //        if (!this.GrayCdTestRest())
    //        {
    //            FileWriter.WriteLog("Gray Card not in Home location");
    //            return false;
    //        }
    //        if (m_IoBoard.StatusCk(m_IoBoard.GrayCardOrigin, Time_out))
    //        {
    //            m_IoBoard.SDHorizCylinder(ON);
    //            if (m_IoBoard.StatusCk(m_IoBoard.SDHorizTest, Time_out))
    //            {
    //                Thread.Sleep(200);
    //                m_IoBoard.SDVertCylinder(ON);
    //                if (m_IoBoard.StatusCk(IoBoard.SDVertTest, Time_out))
    //                {
    //                    res = true;
    //                }
    //                else
    //                {
    //                    FileWriter.WriteLog("SDVer not in Test location");
    //                    res = false;
    //                }
    //            }
    //            else
    //            {
    //                FileWriter.WriteLog("SDHoriz not in Test location");
    //                res = false;
    //            }
    //            return res;
    //        }
    //        else
    //        {
    //            FileWriter.WriteLog("GrayCard not in org location");
    //            return false;
    //        }
    //    }

        ///// <summary>
        ///// sd卡释放
        ///// </summary>
        ///// <returns></returns>
        //public bool SDCardBack()
        //{
        //    bool res = true;
        //    m_IoBoard.SDVertCylinder(OFF);//
        //    if (m_IoBoard.StatusCk(m_IoBoard.SDVertOrigin, Time_out))
        //    {
        //        m_IoBoard.SDHorizCylinder(OFF);//SDHorizCylinder
        //        if (m_IoBoard.StatusCk(IoBoard.SDHorizOrig, Time_out))//SDHorizOrig
        //        {
        //            res = true;
        //        }
        //        else
        //        {
        //            FileWriter.WriteLog("SDHorizOrig not in Origin location");
        //            res = false;
        //        }
        //    }
        //    else
        //    {
        //        FileWriter.WriteLog("SDVertCylinder not in Origin location");
        //        res = false;
        //    }
        //    return res;

        //}

        #endregion

        /// <summary>
        /// sd卡释放---设备中以无该模块, 保留方法名称，并且永远返回true.目的:不改变之前写的逻辑结构
        /// </summary>
        /// <returns></returns>
        public bool SDCardBack()
        {
            return true;
        }

        #region 鼻子
        /// <summary>
        /// 灰卡无限距离测试
        /// </summary>
        /// <returns></returns>[5]
        public bool ProxGrayCdTest500mm()
        {
            bool res = true;
            res = this.GrayCdTestRest();
            if (res == false)
            {
                FileWriter.WriteLog("GrayCard moons move fail");
                return false;
            }
            return true;
            //m_IoBoard_2.GrayCardCylinder(OFF);
            //if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
            //{
            //    FileWriter.WriteLog("GrayCardCylinder move fail");
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}
        }

        /// <summary>
        /// 灰卡30mm测试 鼻子
        /// </summary>
        /// <returns></returns>[6]
        public bool ProxGrayCdTest30mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos30mm))
                        {
                            //bool value = m_MoonGrayRTU.WaitStopRTU(1);
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    else if (m_devices_id == "1")
                    {
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos30mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    else { FileWriter.WriteLog("devicesID error!"); return false; }
                    
                }
            }
            else
            {
                FileWriter.WriteLog("gray not in Origin location");
                return false;
            }
        }
        
        /// <summary>
        /// 灰卡20mm测试 鼻子
        /// </summary>
        /// <returns></returns>[7]
        public bool ProxGrayCdTest20mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos20mm))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    else if (m_devices_id == "1")
                    {
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos20mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    else 
                    {
                        FileWriter.WriteLog("gray motor move fail");
                        return false;
                    }
                }
            }
            else
            {
                FileWriter.WriteLog("gray not in Origin location");
                return false;
            }
        }

        /// <summary>
        /// 灰卡13mm测试 鼻子
        /// </summary>
        /// <returns></returns>[8]
        public bool ProxGrayCdTest13mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos13mm))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("13mm gray motor move fail");
                            return false;
                        }
                    }
                    if (m_devices_id == "1")
                    {
                       
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos13mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("13mm gray motor move fail");
                            return false;
                        }
                    }
                    return false;
                    
                }
            }
            else
            {
                FileWriter.WriteLog("13 mm gray not in Origin location");
                return false;
            }

        }

        public bool GrayCdTestRest()
        {
            bool res = false;
            //res = this.SDCardBack();
            //if (res == false)
            //{
            //    FileWriter.WriteLog("gray back fail in the test");
            //    return false;
            //}

            m_IoBoard_2.GrayCardCylinder(ON);
            res = m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out);
            if (res == false)
            {
                FileWriter.WriteLog("before GrayCdTestRest GrayCard not in the test");
                return false;
            }
            //if (m_MoonGrayTCP.CheckHomeStatus(m_gray_slave) == false)
            //{
            //    res = m_MoonGrayTCP.SetMoonsHome(m_gray_slave);
            //    FileWriter.WriteLog("before GrayCdTestRest moons Check null , setMoonsHome in the test");
            //    if (res == false)
            //    {
            //         FileWriter.WriteLog("SetMoonsHome fail");
            //        return false;
            //    }
            //}
            res = false;
            if (m_devices_id == "2")
            {
                res = m_MoonGrayRTU.RTU_MotorGoHome(m_gray_slave);
            }
            if (m_devices_id == "1")
            {
                res = m_MoonGrayTCP.SetMoonsHome(m_gray_slave);
            }
            if (res == false)
            {
                return false;
            }
            else
            {
                /*灰卡复位前检查灰卡当前位置 如果偏差太大返回false*/
                double pos = m_MoonGrayTCP.GetMoonsLocalPosition(m_gray_slave);
                if (Math.Abs(pos - m_GrayHome) > 10)
                {
                    FileWriter.WriteLog("GrayMotor reset fail.   LocalPosition:"+pos.ToString()+" TargetPosition:"+m_GrayHome.ToString());
                    return false;
                }
                m_IoBoard_2.GrayCardCylinder(OFF);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
                {
                    return false;
                }
            }
            return true;
        }
        #endregion

        #region 眉毛 brow
        /// <summary>
        /// 灰卡无限距离测试 眉毛
        /// </summary>
        /// <returns></returns>[5]
        public bool CapGrayCdTest500mm()
        {
            bool res = true;
            res = this.GrayCdTestRest();
            if (res == false)
            {
                FileWriter.WriteLog("GrayCard moons move fail");
                return false;
            }

            //m_IoBoard_2.GrayCardCylinder(OFF);
            //if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
            //{
            //    FileWriter.WriteLog("GrayCardCylinder move fail");
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}
            return true;
        }

        /// <summary>
        /// 灰卡13mm测试 眉毛
        /// </summary>
        /// <returns></returns>[6]
        public bool CapGrayCdTest13mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "1")
                    {
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos30mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos30mm))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    return false;
                }
            }
            else
            {
                FileWriter.WriteLog("gray not in Origin location");
                return false;
            }
        }
        
        /// <summary>
        /// 灰卡20mm测试 眉毛
        /// </summary>
        /// <returns></returns>[7]
        public bool CapGrayCdTest20mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos20mm))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    if (m_devices_id == "1")
                    {
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos20mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("gray motor move fail");
                            return false;
                        }
                    }
                    return false;
                }
            }
            else
            {
                FileWriter.WriteLog("gray not in Origin location");
                return false;
            }
        }

        /// <summary>
        /// 灰卡30mm测试 眉毛
        /// </summary>
        /// <returns></returns>[8]
        public bool CapGrayCdTest30mm()
        {
            if (this.SDCardBack())
            {
                m_IoBoard_2.GrayCardCylinder(ON);
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardTest, Time_out) == false)
                {
                    return false;
                }
                else
                {
                    if (m_devices_id == "2")
                    {

                        if (m_MoonGrayRTU.RTU_MotorRun(m_gray_slave, m_Proxpos13mm))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("30mm gray motor move fail");
                            return false;
                        }
                    }
                    if (m_devices_id == "1")
                    {
                        if (m_MoonGrayTCP.MoveLine(m_gray_slave, m_Proxpos13mm, m_graycard_speed, m_graycard_acspeed, m_graycard_respeed))
                        {
                            return true;
                        }
                        else
                        {
                            FileWriter.WriteLog("30mm gray motor move fail");
                            return false;
                        }
                    }
                    return false;
                }
            }
            else
            {
                FileWriter.WriteLog("30 mm gray not in Origin location");
                return false;
            }

        }

        #endregion

        /// <summary>
        /// 拨动旋钮，将旋钮拨动到底
        /// </summary>
        /// <returns></returns>
        public bool IPD_check_status(int Motor_Position, DateTime start) 
        { 
            bool ret = false;
            double __Torque_Range__ = Math.Abs(Motor_Position * m_Torque_Range / 100);
            dt = new DataTable();
            dt.Columns.Add("0", typeof(int));
            Motor_Position = Math.Abs(Motor_Position);
            //1. 停止条件一：达到额定扭矩，并稳定：10个计数
            int count = 0;
            double delta = Math.Abs(Motor_Position - __Torque_Range__);
            //2. 停止条件二：达不到额定扭矩，根据当前稳定的扭矩10个计数，差在稳定范围
            Queue<int> que = new Queue<int>();
            try
            {
                while (true)
                {
                    int w = m_MoonIPDWheel.GetmotorSpeed(m_IPDWheelSlave);
                    dt.Rows.Add(w);
                    if ((DateTime.Now - start).Seconds > m_Motor_IPD_TimeOut)
                    {
                        ret = true;
                        break;
                    }
                    Console.WriteLine(Math.Abs(Motor_Position - w).ToString());
                    if (que.Count > Max_count)
                    {
                        que.Dequeue();
                    }
                    que.Enqueue(w);
                    if (Math.Abs(w) > delta)
                    //if (Math.Abs(Motor_Position - w) <= __Torque_Range__)
                    {
                        //ret = true;
                        //break;
                        if (count > 10)
                        {
                            ret = true;
                            Console.WriteLine("条件一");
                            break;
                        }
                        count++;
                    }
                    else if (que.Count > Sample_length && Math.Abs(que.Max() - que.Min()) < Sample_range)
                    {
                        ret = true;
                        Console.WriteLine("条件二");
                        break;
                    }
                    Thread.Sleep(50);     //50ms取一次值
                }
            }
            catch (Exception ex)
            {
                FileWriter.WriteLog("IPD_check_status() Exception:" + ex.Message);
            }
            ret = m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, 0);       //将扭矩值设为百分之0
            Thread.Sleep(100);
            ret = m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, 0);       //将扭矩值设为百分之0

            string os = DateTime.Now.ToString("yyyy_MM_dd");
            string p = DateTime.Now.ToString("HH_mm_ss");
            string path1 = string.Format(@"D:\csv_data\{0}\{1}.csv", os, p);
            Write_csv.SaveCSV1(dt, path1);
            return ret;
        }

        /// <summary>
        /// 拨动旋钮，将旋钮拨动到底
        /// </summary>
        /// <returns></returns>
        /// 
        public bool IPDWheelMotor2(bool check = true)
        {

            //1.设定正反向转动最大值
            bool status = true;
            status = m_MoonIPDWheel.SetMoonsPositiveRotation(m_IPDWheelSlave, m_Motor_PositiveRotation);
            status = m_MoonIPDWheel.SetMoonsReversingRotation(m_IPDWheelSlave, m_Motor_ReversingRotation);
            if (!status)
            {
                FileWriter.WriteLog("SetMoonsPositiveRotation or SetMoonsPositiveRotation is fail");
                return false;
            }
            //2.输入扭矩m_Motor_Position
            FileWriter.WriteLog("IPDWheelMotor2(): SetMoonsPosition");
            status = m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, (ushort)m_Motor_Position);
            DateTime start = DateTime.Now;

            FileWriter.WriteLog("IPDWheelMotor2():check---"+check+" IPD_check_status()");
            if (check)
            {
                bool ret = this.IPD_check_status(m_Motor_Position, start);
                Console.WriteLine("IPDWheelMotor2{0}",ret.ToString());
                FileWriter.WriteLog("IPDWheelMotor2(): result:"+ret);
                return ret;
            }
            else
            {
                Thread thrwad = new Thread(() =>
                {
                    this.IPD_check_status((ushort)m_Motor_Position, start);
                });
                thrwad.Start();
                return true;
            };
        }
        
        public bool IPDWheelMotor() 
        {
            return this.IPDWheelMotor2(true);
        }

        /// <summary>
        /// 电机反转拨动IPD---20220811zx 新增
        /// </summary>
        /// <returns></returns>
        public bool IPDWheelMotor_Reversing()
        {
            return IPDWheelMotor2_Reversing(true);
        }

        public bool IPDWheelMotor2_Reversing(bool check = true)
        {

            //1.设定正反向转动最大值
            bool status = true;
            status = m_MoonIPDWheel.SetMoonsPositiveRotation(m_IPDWheelSlave, m_Motor_PositiveRotation);
            status = m_MoonIPDWheel.SetMoonsReversingRotation(m_IPDWheelSlave, m_Motor_ReversingRotation);
            if (!status)
            {
                FileWriter.WriteLog("IPDWheelMotor2_Reversing:SetMoonsPositiveRotation or SetMoonsPositiveRotation is fail");
                return false;
            }
            //2.输入扭矩m_Motor_Position
            FileWriter.WriteLog("IPDWheelMotor2_Reversing(): SetMoonsPosition");
            status = m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, (ushort)m_Motor_Reversing);
            DateTime start = DateTime.Now;

            FileWriter.WriteLog("IPDWheelMotor2_Reversing():check---" + check + " IPD_check_status()");
            if (check)
            {
                bool ret = this.IPD_check_status(m_Motor_Reversing, start);
                Console.WriteLine("IPDWheelMotor2_Reversing{0}", ret.ToString());
                FileWriter.WriteLog("IPDWheelMotor2_Reversing(): result:" + ret);
                return ret;
            }
            else
            {
                Thread thrwad = new Thread(() =>
                {
                    this.IPD_check_status((ushort)m_Motor_Position, start);
                });
                thrwad.Start();
                return true;
            };
        }

        public bool IPDWheelMotor1() 
        {
            bool result = this.IPDWheelMotor2(false);
            return result;
        }

        /// <summary>
        /// AudioMotor 耳机电机插入
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public bool AudioMotor2(bool check = true, int pos = 1)
        {
            //1.设定正反向转动最大值
            bool status = true;
            status = m_MoonIPDWheel.SetMoonsPositiveRotation(m_headphone_slave, m_headphone_acspeed);
            status = m_MoonIPDWheel.SetMoonsReversingRotation(m_headphone_slave, m_headphone_respeed);
            if (!status)
            {
                FileWriter.WriteLog("Audio SetMoonsPositiveRotation or SetMoonsPositiveRotation is fail");
                return false;
            }
            //2.输入扭矩m_Motor_Position
            FileWriter.WriteLog("AudioMotor(): SetMoonsPosition");
            status = m_MoonIPDWheel.SetMoonsPosition(m_headphone_slave, (ushort)m_headphone_pos);
            DateTime start = DateTime.Now;

            FileWriter.WriteLog("AudioMotor():check---" + check + " IPD_check_status()");

            if (check)
            {
                bool ret = this.Audio_check_status(pos, start);
                Console.WriteLine("AudioMotor{0}", ret.ToString());
                FileWriter.WriteLog("AudioMotor(): result:" + ret);
                return ret;
            }
            else
            {
                Thread thrwad = new Thread(() =>
                {
                    this.Audio_check_status(pos, start);
                });
                thrwad.Start();
                return true;
            };
        }

        /// <summary>
        /// 判断耳机插入停止逻辑
        /// </summary>
        /// <param name="Motor_Position"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public bool Audio_check_status(int Motor_Position, DateTime start)
        {
            bool ret = false;
            double __Torque_Range__ = Math.Abs(m_headphone_pos * m_headphone_StopRange_1 / 100);
            dt = new DataTable();
            dt.Columns.Add("speed");
            dt.Columns.Add("speedN");

            Queue<int> que = new Queue<int>();
            Thread.Sleep(3500);
            while (true)
            {
                /*判断耳机停止条件
                 * 1. 达到额定扭矩的百分之几停止电机
                 * 2. 两次采样之间的扭矩波动超过百分之几停止电机
                 * 3. 或者电机速度为0 停止电机
                 */
                //获取扭矩
                int foerce = Math.Abs(m_MoonIPDWheel.GetmotorSpeed(m_headphone_slave));
                //获取速度
                int speed = Math.Abs(m_MoonIPDWheel.GetCurrentSpeed(m_headphone_slave));

                dt.Rows.Add(new string[] { foerce.ToString(), speed.ToString() });
                if ((DateTime.Now - start).Seconds > m_headphone_TimeOut)
                {
                    ret = true;
                    break;
                }
                Console.WriteLine(Math.Abs(Motor_Position - foerce).ToString());
                if (que.Count > 2)
                {
                    que.Dequeue();
                }
                que.Enqueue(foerce);
                /*条件一*/
                Console.WriteLine("Range1：----> w：" + foerce.ToString() + " __Torque_Range__:" + __Torque_Range__.ToString() + " s:" + m_headphone_Stopspeed);
                if (foerce >= __Torque_Range__ || speed <= m_headphone_Stopspeed)
                {
                    Thread.Sleep(50);
                    foerce = Math.Abs(m_MoonIPDWheel.GetmotorSpeed(m_headphone_slave));
                    speed = Math.Abs(m_MoonIPDWheel.GetCurrentSpeed(m_headphone_slave));
                    if (foerce >= __Torque_Range__ || speed <= m_headphone_Stopspeed)
                    {
                        ret = true;
                        FileWriter.WriteLog("Audio Stop Range1:" + foerce + " >= " + __Torque_Range__ + "|| " + speed + " <= " + m_headphone_Stopspeed);
                        Console.WriteLine("Audio Stop Range1");
                        break;
                    }
                }
                ///*条件二*/
                ////Console.WriteLine("Range2：---> Max：" + que.Max().ToString() + " Min：" + que.Min().ToString() + " Range:" + Math.Abs((que.Max()-que.Min())/(que.Min()*1f)) );
                //if (Math.Abs((que.Max() - que.Min()) / (que.Min()*1f)) > m_headphone_StopRange_2/100f || s <= m_headphone_Stopspeed)
                ////if (que.Count > Sample_length && Math.Abs(que.Max() - que.Min()) < Sample_range)
                //{
                //    ret = true;
                //    Console.WriteLine("Audio Stop Range2");
                //    break;
                //}
                
                Thread.Sleep(50);     //50ms取一次值
            }
            ret = m_MoonIPDWheel.SetMoonsPosition(m_headphone_slave, 0);       //将扭矩值设为百分之0
            string os = DateTime.Now.ToString("yyyy_MM_dd");
            string p = DateTime.Now.ToString("HH_mm_ss");
            string path1 = string.Format(@"D:\csv_data\{0}\{1}_Audio.csv", os, p);
            Write_csv.SaveCSV1(dt, path1);
            return ret;
        }

        /// <summary>
        /// AudioMotor 耳机电机回原
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public bool AudioMotor_Home(bool check = true, int pos = 1)
        {
            //1.设定正反向转动最大值
            bool status = true;
            status = m_MoonIPDWheel.SetMoonsPositiveRotation(m_headphone_slave, m_headphone_acspeed);
            status = m_MoonIPDWheel.SetMoonsReversingRotation(m_headphone_slave, m_headphone_respeed);
            if (!status)
            {
                FileWriter.WriteLog("Audio SetMoonsPositiveRotation or SetMoonsPositiveRotation is fail");
                return false;
            }
            //2.输入扭矩m_Motor_Position
            FileWriter.WriteLog("AudioMotor(): SetMoonsPosition");
            int a = m_headphone_pos_re * -1;
            status = m_MoonIPDWheel.SetMoonsPosition(m_headphone_slave, (ushort)a);
            DateTime start = DateTime.Now;

            FileWriter.WriteLog("AudioMotor():check---" + check + " AudioMotor_Home()");

            if (check)
            {
                bool ret = this.Audio_Check_Reset(pos, start);
                Console.WriteLine("AudioMotor{0}", ret.ToString());
                FileWriter.WriteLog("AudioMotor(): result:" + ret);
                return ret;
            }
            else
            {
                Thread thrwad = new Thread(() =>
                {
                    this.Audio_Check_Reset(pos, start);
                });
                thrwad.Start();
                return true;
            }
        }

        /// <summary>
        /// 耳机电机回原停止逻辑,暂时使用 时间 控制回原
        /// </summary>
        /// <param name="Motor_Position"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public bool Audio_Check_Reset(int Motor_Position, DateTime start)
        {
            bool ret = false;
            double __Torque_Range__ = Math.Abs(m_headphone_pos_re * m_headphone_StopRange_1 / 100);
            dt = new DataTable();
            dt.Columns.Add("speed");
            dt.Columns.Add("speedN");

            Queue<int> que = new Queue<int>();
            Thread.Sleep(1500);
            while (true)
            {
                #region 跟据扭矩判断回原
                /*判断耳机停止条件
                 * 1. 达到额定扭矩的百分之几停止电机
                 * 2. 两次采样之间的扭矩波动超过百分之几停止电机
                 * 3. 或者电机速度为0 停止电机
                 */
                //获取扭矩
                int w = Math.Abs(m_MoonIPDWheel.GetmotorSpeed(m_headphone_slave));
                //获取速度
                int s = Math.Abs(m_MoonIPDWheel.GetCurrentSpeed(m_headphone_slave));

                dt.Rows.Add(new string[] { w.ToString(), s.ToString() });
                if ((DateTime.Now - start).Seconds > m_headphone_HomeTime)
                {
                    ret = true;
                    break;
                }
                Console.WriteLine(Math.Abs(Motor_Position - w).ToString());
                if (que.Count > 2)
                {
                    que.Dequeue();
                }
                que.Enqueue(w);
                /*条件一*/
                Console.WriteLine("Range1：----> w：" + w.ToString() + " __Torque_Range__:" + __Torque_Range__.ToString());
                if (w >= __Torque_Range__ || s <= m_headphone_Stopspeed)
                {
                    ret = true;
                    Console.WriteLine("Audio Stop Range1");
                    break;
                }
                #endregion

                Thread.Sleep(5);     //5ms取一次值
            }
            ret = m_MoonIPDWheel.SetMoonsPosition(m_headphone_slave, 0);       //将扭矩值设为百分之0
            string os = DateTime.Now.ToString("yyyy_MM_dd");
            string p = DateTime.Now.ToString("HH_mm_ss");
            string path1 = string.Format(@"D:\csv_data\{0}\{1}_Audio_Home.csv", os, p);
            Write_csv.SaveCSV1(dt, path1);
            return ret;
        }

        DateTime end = new DateTime();
        DateTime start = new DateTime();
        /// <summary>
        /// 复位
        /// </summary>
        /// <returns></returns>[9]
        public bool Reset()
        {
            FileWriter.WriteLog("Reset Start");
            //Thread.Sleep(5000);
            IsReset = false;
            bool res = false;
            lock (Lock)
            {

                //m_MoonIPDWheel.SetMoonsPosition(m_IPDWheelSlave, 0);       //将扭矩值设为百分之0
                if (m_devices_id == "2")
                {
                    //m_MoonMotorRTU.Motor_Stop(m_headphone_slave);
                    //m_MoonDoorRTU.Motor_Stop(m_DoorSlave);
                    //m_MoonGrayRTU.Motor_Stop(m_gray_slave);
                }
                if (m_devices_id == "1")
                {
                    //m_MoonMotorTCP.SetMoonsImmediatelyStop(m_headphone_slave);
                    //m_MoonGrayTCP.SetMoonsImmediatelyStop(m_gray_slave);
                    //m_MoonDoorTCP.SetMoonsImmediatelyStop(m_DoorSlave);
                }
                res = true;
                if (PowerButtonReset() == false)
                {
                    res = false;
                }
                Console.WriteLine("PowerButtonReset！");
                if (VolumeplusButtonReset() == false)
                {
                    res = false;
                }
                Console.WriteLine("VolumeplusButtonReset！");
                if (VolumeSubButtonReset() == false)
                {
                    res = false;
                }
                Console.WriteLine("VolumeSubButtonReset！");
                if (PogoCylinderReset() == false)
                {
                    res = false;
                }
                Console.WriteLine("PogoCylinderReset！");
                if (IPDCylinderReset() == false)
                {
                    res = false;
                }
                Console.WriteLine("IPDCylinderReset！");
                if (ElectromagnetClose() == false)
                {
                    res = false;
                }
                Console.WriteLine("ElectromagnetClose！");
                if (PogoClose() == false)
                {
                    res = false;
                }
                Console.WriteLine("PogoClose！");
                if (CoverBoardRest() == false)
                {
                    res = false;
                }
                Console.WriteLine("CoverBoardRest！");
                if (m_IoBoard_2.GrayCardCylinder(ON) == false)
                {
                    res = false;
                }

                Console.WriteLine("GrayCardCylinder！");
                /*P1.1---20220928 优化电机复位逻辑 改成多线程运行*/
                Console.WriteLine("Reset 灰卡！");
                FileWriter.WriteLog("Reset 灰卡！");
                //bool check_huika = this.Moon_Gray_Reset();
                //if (check_huika)
                //{
                //    m_IoBoard_2.GrayCardCylinder(OFF);
                //    if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
                //    {
                //        res = false;
                //        FileWriter.WriteLog("GrayCard reset fail:GrayCard not in Origin location");
                //    }
                //}
                //else
                //{
                //    res = false;
                //}
                FuncHandle moonGray_fh = new FuncHandle(this.Moon_Gray_Reset);
                IAsyncResult moonGray_ar = moonGray_fh.BeginInvoke(null, moonGray_fh);
                
                Console.WriteLine("Reset 耳机！");
                FileWriter.WriteLog("Reset 耳机！");
                //this.Moon_Audio_Reset();
                FuncHandle moonAudio_fh = new FuncHandle(this.Moon_Audio_Reset);
                IAsyncResult moonAudio_ar = moonAudio_fh.BeginInvoke(null, moonAudio_fh);

                Console.WriteLine("Reset 开门！");
                FileWriter.WriteLog("Reset 开门！");
                //this.Moon_Door_Reset();
                FuncHandle moonDoor_fh = new FuncHandle(this.Moon_Door_Reset);
                IAsyncResult moonDoor_ar = moonDoor_fh.BeginInvoke(null, moonDoor_fh);


                if (!moonGray_fh.EndInvoke(moonGray_ar))
                {
                    res = false;
                    FileWriter.WriteLog("GrayMotor  reset fail");
                }
                else
                {
                    //检查灰卡电机回原到位
                    if (m_devices_id == "2")
                    {
                        Console.WriteLine("灰卡 RTU_MotorGoHome！");
                        if (m_MoonGrayRTU.CheckHomeStatusRTU(m_gray_slave) == false)
                        {
                            return false;
                        }
                        Console.WriteLine("灰卡 RTU_MotorCheckHome2！");
                        if (m_MoonMotorRTU.CheckHomeStatusRTU(m_gray_slave) == false)
                        {
                            return false;
                        }
                        FileWriter.WriteLog("Reset m_MoonMotorRTU CheckMoonsHome completed");
                        m_IoBoard_2.GrayCardCylinder(OFF);
                        if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
                        {
                            res = false;
                            FileWriter.WriteLog("GrayCard reset fail:GrayCard not in Origin location");
                        }
                    }
                    else
                    {
                        GrayCdTestRest();
                        if (m_IoBoard_2.StatusCk(m_IoBoard_2.GrayCardOrigin, Time_out) == false)
                        {
                            res = false;
                            FileWriter.WriteLog("GrayCard reset fail:GrayCard not in Origin location");
                        }
                    }

                }
                if (!moonAudio_fh.EndInvoke(moonAudio_ar))
                {
                    res = false;
                    FileWriter.WriteLog("AudioMotor  reset fail");
                }
                if (!moonDoor_fh.EndInvoke(moonDoor_ar))
                {
                    res = false;
                    FileWriter.WriteLog("open door fail");
                }
                if (CameraOFF() == false)
                {
                    res = false;
                }
            }
            IsReset = res;
            end = DateTime.Now;
            return res;
        }

        /// <summary>
        /// 用于处理灰卡、耳机、门的复位委托
        /// </summary>
        /// <returns></returns>
        public delegate bool FuncHandle();

        /// <summary>
        /// 灰卡电机复位函数
        /// </summary>
        /// <returns></returns>
        public bool Moon_Gray_Reset()
        {
            if (m_devices_id == "2")
            {
                Console.WriteLine("灰卡 RTU_MotorGoHome！");
                //只是下发灰卡电机回原，并不会检查电机是否到位  RTU_MotorGoHome_1只用于设备复位
                if (m_MoonGrayRTU.RTU_MotorGoHome(m_gray_slave) == false)
                {
                    return false;
                }
                FileWriter.WriteLog("Reset m_MoonGrayTCP SetMoonsHome completed");
                Console.WriteLine("灰卡 RTU_MotorGoHome2！");
                if (m_MoonMotorRTU.RTU_MotorGoHome(m_gray_slave) == false)
                {
                    return false;
                }
                FileWriter.WriteLog("Reset m_MoonMotorRTU SetMoonsHome completed");
            }
            else if (m_devices_id == "1")
            {
                bool status = false;
                status = m_MoonGrayTCP.SetMoonsHome(m_gray_slave);
                if (status == false)
                {
                    FileWriter.WriteLog("m_MoonGrayTCP SetMoonsHome fail");
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 耳机电机复位函数
        /// </summary>
        /// <returns></returns>
        public bool Moon_Audio_Reset()
        {
            return this.AudioBack();
        }

        /// <summary>
        /// 门复位
        /// </summary>
        /// <returns></returns>
        public bool Moon_Door_Reset()
        {
            return this.OpenDoor();
        }

        /// <summary>
        /// 夹紧气缸松开
        /// </summary>
        /// <returns></returns>
        public bool CoverBoardRest()
        {
            bool status = false;

            if (m_IoBoard_2.StatusCk(m_IoBoard.PoweroutOrigin, Time_out) == true)
            {
                status = m_IoBoard_2.HolderSensorOut(OFF);
                status = m_IoBoard_2.HolderSensorIn(ON);
                if (status == false)
                {
                    status = m_IoBoard_2.HolderSensorOut(OFF);
                    status = m_IoBoard_2.HolderSensorIn(ON);
                }
            }
            return status;
        }

        /// <summary>
        /// 夹紧气缸加紧
        /// </summary>
        /// <returns></returns>
        public bool CoverBoardTest()
        {
            bool status = false;

            if (m_IoBoard_2.StatusCk(m_IoBoard.PoweroutOrigin, Time_out) == true)
            {
                status = m_IoBoard_2.HolderSensorIn(OFF);
                status = m_IoBoard_2.HolderSensorOut(ON);
                if (status == true)
                {
                    if (m_IoBoard_2.StatusCk(m_IoBoard.CoverClose, Time_out) == false)
                    {
                        //res = false;
                        FileWriter.WriteLog("PowerCylinderTest reset fail:CoverBoardT not in Test location");
                        return false;
                    }

                }
                else if (status == false)
                {
                    status = m_IoBoard_2.HolderSensorIn(OFF);
                    status = m_IoBoard_2.HolderSensorOut(ON);
                    {
                        if (m_IoBoard_2.StatusCk(m_IoBoard.CoverClose, Time_out) == false)
                        {
                            //res = false;
                            FileWriter.WriteLog("PowerCylinderTest reset fail:CoverBoardT not in Test location");
                            return false;
                        }

                    }
                }
            }

            return status;
        }

        /// <summary>
        /// 电源气缸伸出
        /// </summary>
        /// <returns></returns>
        public bool PowerCylinderTest()
        {
            if (IoBoard.PowerOutCylinder(ON) == false)
            {
                if(IoBoard.PowerOutCylinder(ON)==false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PowerOutTest, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerCylinderTest reset fail:PowerCylinder not in Test location");
                    return false;
                }
            }
            else if (m_IoBoard_2.StatusCk(m_IoBoard.PowerOutTest, Time_out) == false)
            {
                //res = false;
                if (IoBoard.PowerOutCylinder(ON) == false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PowerOutTest, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerCylinderTest reset fail:PowerCylinder not in Test location");
                    return false;
                }
            }
            return true; 
        }

        /// <summary>
        /// 电源气缸缩回
        /// </summary>
        /// <returns></returns>
        public bool PowerCylinderRest()
        {
            if (IoBoard.PowerOutCylinder(OFF) == false)
            {
                if (IoBoard.PowerOutCylinder(OFF) == false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PoweroutOrigin, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerCylinderRest reset fail:PowerCylinder not in Origin location");
                    return false;
                }
            }
            else if (m_IoBoard_2.StatusCk(m_IoBoard.PoweroutOrigin, Time_out) == false)
            {
                if (IoBoard.PowerOutCylinder(OFF) == false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PoweroutOrigin, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerCylinderRest reset fail:PowerCylinder not in Origin location");
                    return false;
                }
            }
            return true; 
        }
        /// <summary>
        /// 电源键伸出
        /// </summary>
        /// <returns></returns>
        public bool PowerKeyTest()
        {
            if (IoBoard.PowerCylinder(ON) == false)
            {
                if (IoBoard.PowerCylinder(ON) == false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PowerTest, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerKeyTest reset fail:PowerKey not in Test location");
                    return false;
                }
            }
            else if (m_IoBoard_2.StatusCk(m_IoBoard.PowerTest, Time_out) == false)
            {
                //res = false;
                if (IoBoard.PowerCylinder(ON) == false)
                {
                    return false;
                }
                if (m_IoBoard_2.StatusCk(m_IoBoard.PowerOutTest, Time_out) == false)
                {
                    //res = false;
                    FileWriter.WriteLog("PowerKeyTest reset fail:PowerKey not in Test location");
                    return false;
                }
                //FileWriter.WriteLog("PowerCylinderTest reset fail:GrayCard not in Origin location");
                //return false;
            }
            return true; 
        }
        /// <summary>
        /// 电源键缩回
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool PowerKeyRest()
        {
            if (IoBoard.PowerCylinder(OFF) == false)
            {
                if (IoBoard.PowerCylinder(OFF) == false)
                {
                    return false;
                }
            }
            return true; 
        }

        /// <summary>
        /// Camera键下压
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool CameraON()
        {
            bool status = false;
            /*CameraRGB盖板打开前检测门是否在原点位置,如果不在原点回一次原点*/
            if (m_devices_id == "1")
            {
                status = this.m_MoonDoorTCP.CheckHomeStatus(m_DoorSlave) ? true : this.m_MoonDoorTCP.SetMoonsHome(m_DoorSlave);
            }
            else if (m_devices_id == "2")
            {
                status = this.m_MoonDoorRTU.CheckHomeStatusRTU(m_DoorSlave) ? true : this.m_MoonDoorRTU.RTU_MotorGoHome(m_DoorSlave);
            }
            else
            {
                FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false;
            }
            if (!status)
            {
                FileWriter.WriteLog("检测到门不在原点位，无法伸出CameraRGB盖板"); return false;
            }

            /*检测Camera RGB 盖板到位*/
            if (m_IoBoard_2.PowerRGBCylinder(ON))
            {
                if (m_IoBoard_2.StatusCk(m_IoBoard_2.RGBTest, Time_out) == false)
                {
                    FileWriter.WriteLog("PowerRGBCylinder not in Test location");

                    if (m_IoBoard_2.PowerRGBCylinder(ON))
                    {
                        //Thread.Sleep(200);
                        if (m_IoBoard_2.StatusCk(m_IoBoard_2.RGBTest, Time_out) == false)
                        {
                            FileWriter.WriteLog("PowerRGBCylinder not in Test location Again");
                            return false;
                        }
                    }
                    else
                    {
                        FileWriter.WriteLog("PowerRGBCylinder fail Again");
                        return false;
                    }
                    return true;
                }
                else
                    return true;
            }
            else
            {
                FileWriter.WriteLog("IPDCylinderTest fail");
                return false;
            }

            return true;
        }
        /// <summary>
        /// Camera键松开
        /// </summary>
        /// <param name="On"></param>
        /// <returns></returns>
        public bool CameraOFF()
        {
            bool status = false;
            /*CameraRGB盖板打开前检测门是否在原点位置*/
            if (m_devices_id == "1")
            {
                status = this.m_MoonDoorTCP.CheckHomeStatus(m_DoorSlave) ? true : this.m_MoonDoorTCP.SetMoonsHome(m_DoorSlave);
            }
            else if (m_devices_id == "2")
            {
                status = this.m_MoonDoorRTU.CheckHomeStatusRTU(m_DoorSlave) ? true : this.m_MoonDoorRTU.RTU_MotorGoHome(m_DoorSlave);
            }
            else
            {
                FileWriter.WriteLog("设备号错误，没有当前设备ID！"); return false;
            }
            if (!status)
            {
                FileWriter.WriteLog("检测到门不在原点位，无法打开CameraRGB盖板"); return false;
            }

            if (IoBoard_2.PowerRGBCylinder(OFF) == false)
            {
                if (IoBoard_2.PowerRGBCylinder(OFF) == false)
                {
                    return false;
                }
            }
            return true;
        }

        public bool Check_RGBTest()
        {
            return m_IoBoard_2.RGBTest();
        }

        public bool Set_RGB_value(string section, string name, string value)
        {
            try
            {
                ConfigHelper.Instance.Set_value(section, name, value);
                return true;
            }
            catch (Exception er)
            {
                return false;
            }
        }

        public string Get_RGB_value(string section, string name)
        {
            try
            {
                return ConfigHelper.Instance.Get_Value(section, name);
            }
            catch (Exception er)
            {
                return er.ToString();
            }
        }

    }
}
