Newer
Older
RbFreqStand / RbFreqStandMeasure / R_DevService / Service / Impl / SensitivityServiceImpl.cs
using Casic.Birmm.RbFreqStandMeasure.R_DataBase.Model;
using Casic.Birmm.RbFreqStandMeasure.R_DevService.Dto;
using Casic.Birmm.RbFreqStandMeasure.tools;
using Casic.Birmm.RbFreqStandMeasure.Tools;
using System;
using System.Reflection;

namespace Casic.Birmm.RbFreqStandMeasure.R_DevService.Service.Impl
{
    class SensitivityServiceImpl:SensitivityService
    {

        public int beginTest(string type, GPIBService gPIBService, GPIBService tcpService,
            CounterCheckParam counterCheckParam, ref SensitivityContentParam sensitivityContentParam)
        {
            int iRetval = -1;

            if (!gPIBService.isOpen() || !tcpService.isOpen())
                return iRetval;
            try
            {
                //设置计数器阻抗
                GPIB_TCPIP_OPERATIO.setIMP(tcpService);
                //启动output(一次)OUTPUT 0\OUTPUT 1
                GPIB_TCPIP_OPERATIO.output(gPIBService);

                //设置周期/频率、电压GPIB
                if (type == "freq")
                    GPIB_TCPIP_OPERATIO.setFreq(gPIBService, sensitivityContentParam.Freq);
                else if (type == "period")
                    GPIB_TCPIP_OPERATIO.setPeriod(gPIBService, sensitivityContentParam.Period);

                bool lessTolerance = false;
                string currentVolTage = "0";
                while (!lessTolerance)
                {
                    //获取当前灵敏度
                    getVOLTageValue(counterCheckParam, ref currentVolTage);

                    if (currentVolTage == null)
                    {
                        sensitivityContentParam.VolTage = "灵敏度超限";
                        sensitivityContentParam.CheckTime = DateTime.Now;
                        break;
                    }
                    //设置当前灵敏度
                    GPIB_TCPIP_OPERATIO.setVOLTage(gPIBService, currentVolTage, counterCheckParam.SoOutDelay);

                    string currentValue_E = "";
                    string currentValue = "";
                    //主动读取检测数据LAN
                    if (type == "freq")
                        currentValue_E = GPIB_TCPIP_OPERATIO.getFreq(tcpService);//Hz
                    else if (type == "period")
                        currentValue_E = GPIB_TCPIP_OPERATIO.getPeriod(tcpService);//s
                    if (currentValue_E == "") continue;
                    currentValue = UnitConvertHelper.convertDt(currentValue_E);
                    //检测数据是否在容差范围内
                    bool errorCheckResult = satisfyCheck(counterCheckParam, sensitivityContentParam, currentValue, type);
                    //返回满足容差的数据
                    if (errorCheckResult)
                    {
                        if (type == "freq")
                            sensitivityContentParam.ReFreq = UnitConvertHelper.hzConvert(sensitivityContentParam.Freq, currentValue);
                        else if (type == "period")
                            sensitivityContentParam.RePeriod = UnitConvertHelper.sConvert(sensitivityContentParam.Period, currentValue);

                        sensitivityContentParam.VolTage = currentVolTage;
                        sensitivityContentParam.CheckTime = DateTime.Now;
                        
                        lessTolerance = true;
                    }
                }
                iRetval = 0;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "beginTest:" + ex.Message);
                //sensitivityContentParam = null;
                iRetval = -1;
            }
            return iRetval;
        }

        void getVOLTageValue(CounterCheckParam counterCheckParam, ref string currentValue)
        {
            if (currentValue == "0")
                currentValue = counterCheckParam.SoEleFrequency;
            else
            {
                double value = Convert.ToDouble(currentValue) + Convert.ToDouble(counterCheckParam.SoEleAddValue);
                if (value < Convert.ToDouble(counterCheckParam.SoEleAlarm))
                    currentValue = (Convert.ToDouble(currentValue) + Convert.ToDouble(counterCheckParam.SoEleAddValue)).ToString();
                else
                    currentValue = null;
            }
         
        }

        bool satisfyCheck(CounterCheckParam counterCheckParams, SensitivityContentParam sensitivityContentParam,
            string currentValue,string checkType)
        {
            try
            {
                if (checkType == "freq")
                {
                    double freThreshold1 = Convert.ToDouble(counterCheckParams.FreThreshold1);
                    double freThreshold2 = Convert.ToDouble(counterCheckParams.FreThreshold2);

                    double bFreq = UnitConvertHelper.convertHZ(sensitivityContentParam.Freq);
                    double cFreq = Convert.ToDouble(currentValue);

                    double errorFreq = Math.Abs(cFreq - bFreq);

                    if (bFreq >= 1 * 1000 * 1000)
                        return errorFreq < freThreshold2 ? true : false;
                    else
                        return errorFreq < freThreshold1 ? true : false;
                }
                else if (checkType == "period")
                {
                    double periodThreshold1 = Convert.ToDouble(counterCheckParams.CycThreshold1);
                    double periodThreshold2 = Convert.ToDouble(counterCheckParams.CycThreshold2);

                    double bPeriod = UnitConvertHelper.convertS(sensitivityContentParam.Period);
                    double cPeriod = Convert.ToDouble(currentValue);

                    double errorPeriod = Math.Abs(cPeriod - bPeriod);

                    if (bPeriod >= 1000 / 1000 / 1000 / 1000)
                        return errorPeriod < periodThreshold2 ? true : false;
                    else
                        return errorPeriod < periodThreshold1 ? true : false;
                }
                else
                    return false;
            }
            catch(Exception ex)
            {
                LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType,"statisfyCheck:"+ex.Message);
                return false;
            }
        }
        
        /**
         * 
         * GPIBService gPIBService=new GPIBServiceImpl();
         * GPIBService tcpService=new TCPServiceImpl();
         * gPIBService.open();
         * tcpService.open();
         * for(datagridview.row)
         * {
         * SensitivityService.beginTest()//0:成功;-1:失败
         * }
         * gPIBService.close();
         * */
    }
}