Example #1
0
        //检测仪器状态的线程
        private void CheckInstument()
        {
            double temperature;
            int    count = 0;

            if (CheckRamanInstrument() == false)        //没有检查到仪器
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InstrumentStateDelegate(InstrumentState), double.MinValue, null);
                return;
            }

            if (RamanInstrument.Reconnect() == false)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InstrumentStateDelegate(InstrumentState), double.MinValue, RamanInstrument.ErrorString);
                return;
            }

            while (true)
            {
                temperature = RamanInstrument.GetTemperature();
                if (temperature == double.MaxValue)
                {
                    RamanInstrument.Connect();
                    temperature = RamanInstrument.GetTemperature();
                }

                if (temperature == double.MaxValue) //仪器没连接上
                {
                    if (count < 2)                  //重复3次,如果还是没有取到温度,则表示仪器连接错误了
                    {
                        count++;
                    }
                    else
                    {
                        Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InstrumentStateDelegate(InstrumentState), double.MinValue, null);
                        break;
                    }
                }
                else
                {
                    count = 0;
                }

                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InstrumentStateDelegate(InstrumentState), temperature, null);

                System.Threading.Thread.Sleep(1000);
                if (Environment.TickCount - SettingData.lastOperateTime > SettingData.settingData.runing_para.InstrumentTimeOut * 60 * 1000)      //超过设定时间没有操作了,需要重新连接
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new InstrumentStateDelegate(InstrumentState), double.MinValue, null);
                    RamanInstrument.DisConnect();

                    break;
                }
            }
        }
Example #2
0
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (CommonMethod.QuestionMsgBox("确认退出NirIdentifier软件?") == false)
            {
                e.Cancel = true;
                return;
            }

            if (checkTread != null && checkTread.IsAlive)
            {
                checkTread.Abort();
            }

            RamanInstrument.DisConnect();
        }
Example #3
0
        //信噪比自检
        private bool SNR_CalThread()
        {
            Border parentBorder = snr_cal;

            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.RUN);     //设置为正在处理
                SettingFile.SNR_Test snr_Test = SettingData.settingData.snr_test;

                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetTitleTextDelegate(SetTitleText), "正在自检信噪比......");

                float[] snrValue        = new float[snr_Test.repeat];    //SNR值
                float[] noiseInteValue  = new float[snr_Test.repeat];    //noise波段积分值
                float[] singalInteValue = new float[snr_Test.repeat];    //Singal波段积分值
                string  savefile        = null;

                //重复扫描并计算
                for (int i = 0; i < snr_Test.repeat; i++)
                {
                    if (UserCancel)
                    {
                        throw new Exception("用户取消仪器自检");
                    }

                    //设置进度条
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetProgressBarDelegate(SetProgressBar), parentBorder, snr_Test.repeat, i);

                    if (!simulator)
                    {
                        savefile = System.IO.Path.Combine(fileSavePath, "SNR" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".spc");
                        CommonMethod.AddToLogFile("SNR Test Scan, savefile=" + savefile);
                        if ((savefile = RamanInstrument.Measurement(snr_Test.scanPara, savefile, false)) == null)
                        {
                            throw new Exception(RamanInstrument.ErrorString);
                        }
                    }
                    else
                    {
                        savefile = tempSNRFiles[i];
                    }

                    //计算SNR
                    CommonMethod.AddToLogFile("SNR Test Calculate, savefile=" + savefile);
                    snrValue[i] = calulateSNR(savefile, (float)snr_Test.signal.firstX, (float)snr_Test.signal.lastX, (float)snr_Test.noise.firstX, (float)snr_Test.noise.lastX, out noiseInteValue[i], out singalInteValue[i]);
                    if (snrValue[i] == 0)
                    {
                        throw new Exception(ErrorString);
                    }
                }

                //计算平均SNR
                snrResultValue = 0;
                for (int i = 0; i < snrValue.Length; i++)
                {
                    snrResultValue += snrValue[i];
                }
                snrResultValue = snrResultValue / snrValue.Length;
                snrResultValue = (float)Math.Round(snrResultValue);             //取绝对值,精确到个位

                snrSpectrumFile = savefile;

                if (!snr_Test.snrThresold.valueOk(snrResultValue))
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);     //设置为验证错误
                }
                else
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.OK);    //设置为验证正确
                }
                //将结果数据记录到log文件中
                StreamWriter writer = new StreamWriter(Path.Combine(fileSavePath, "calibration.csv"), true, System.Text.Encoding.GetEncoding(SettingData.GBCode2312));

                //标题
                writer.WriteLine();
                writer.WriteLine("信噪比");
                string writestr = snr_Test.signal.firstX.ToString() + "-" + snr_Test.signal.lastX.ToString() + "信号,";
                writestr += snr_Test.noise.firstX.ToString() + "-" + snr_Test.noise.lastX.ToString() + "噪声,信噪比";
                writer.WriteLine(writestr);

                for (int i = 0; i < singalInteValue.Length; i++)
                {
                    writestr = singalInteValue[i].ToString() + "," + noiseInteValue[i].ToString() + "," + snrValue[i].ToString();
                    writer.WriteLine(writestr);
                }

                writestr = "结果,," + snrResultValue.ToString();
                writer.WriteLine(writestr);

                writer.Close();

                return(true);
            }
            catch (System.Exception ex)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);     //设置为验证错误
                ErrorString = ex.Message;
                CommonMethod.AddToLogFile(ex.Message);

                return(false);
            }
        }
Example #4
0
        //相对强度自检
        private bool Intensity_CalThread()
        {
            Border parentBorder = y_cal;

            ErrorString = null;
            try
            {
                SettingFile.Intensity_Test intensityTest = SettingData.settingData.intensity_test;

                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetTitleTextDelegate(SetTitleText), "正在自检相对强度......");        //设置为正在处理
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.RUN); //设置为正在处理

                //扫描并保存光谱i
                string savefile = null;
                if (!simulator)
                {
                    savefile = System.IO.Path.Combine(fileSavePath, "Intensity" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".spc");
                    CommonMethod.AddToLogFile("Intensity Test Scan, savefile=" + savefile);
                    if ((savefile = RamanInstrument.Measurement(intensityTest.scanPara, savefile, true)) == null)
                    {
                        throw new Exception(RamanInstrument.ErrorString);
                    }
                }
                else
                {
                    savefile = tempIntensityFile;
                }

                intensitySpectrumFile = savefile;

                //读取当前扫描的光谱
                SpecFileFormat fileData = new SpecFileFormat();
                if (!fileData.ReadFile(savefile))
                {
                    throw new Exception("读取文件错误:" + savefile);
                }

                CommonMethod.AddToLogFile("Intensity Test Calculate, savefile=" + savefile);
                //计算各区间的积分, regions + baseRegion
                double[] integValue = new double[intensityTest.regions.Count + 1];

                //创建积分结果表
                intensityDataTable.Columns.Clear();
                intensityDataTable.Rows.Clear();
                intensityDataTable.Columns.Add("积分范围(cm-1)");     //第一行的类型
                //regions的积分
                for (int i = 0; i < intensityTest.regions.Count; i++)
                {
                    intensityDataTable.Columns.Add(intensityTest.regions[i].firstX.ToString() + "-" + intensityTest.regions[i].lastX.ToString());    //表标题
                    integValue[i] = Common.SpectrumAlgorithm.Integrate(fileData.XDatas, fileData.YDatas, (float)intensityTest.regions[i].firstX, (float)intensityTest.regions[i].lastX);
                }
                //最后一列是baseRegion的积分
                intensityDataTable.Columns.Add(intensityTest.baseRegion.firstX.ToString() + "-" + intensityTest.baseRegion.lastX.ToString());    //表标题
                integValue[integValue.Length - 1] = Common.SpectrumAlgorithm.Integrate(fileData.XDatas, fileData.YDatas, (float)intensityTest.baseRegion.firstX, (float)intensityTest.baseRegion.lastX);

                //所有RgnThresold区域的阈值
                string[] thresoldArray = new string[intensityTest.regions.Count + 2];   //regions+baseregion+名称
                thresoldArray[0] = "标准范围";
                string[] valueArray = new string[intensityTest.regions.Count + 2];      //regions+baseregion+名称
                valueArray[0] = "测量值";

                //计算regions / baseregion
                intensityResult = true;
                for (int i = 0; i < integValue.Length - 1; i++)
                {
                    float resultvalue = (float)(integValue[i] / integValue[integValue.Length - 1]);
                    if (!intensityTest.thresolds[i].valueOk(resultvalue))
                    {
                        intensityResult = false;
                    }

                    thresoldArray[i + 1] = intensityTest.thresolds[i].minimum.ToString() + "-" + intensityTest.thresolds[i].maximum.ToString(); //记录每个阈值
                    valueArray[i + 1]    = resultvalue.ToString("F2");                                                                          //记录每个相对强度
                }

                //801基准积分区间
                thresoldArray[thresoldArray.Length - 1] = "1";
                valueArray[valueArray.Length - 1]       = "1";

                if (intensityResult == false)
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);    //设置为未处理
                }
                else
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.OK);    //设置为已处理
                }
                intensityDataTable.Rows.Add(thresoldArray);
                intensityDataTable.Rows.Add(valueArray);

                //将结果数据记录到log文件中
                StreamWriter writer = new StreamWriter(Path.Combine(fileSavePath, "calibration.csv"), true, System.Text.Encoding.GetEncoding(SettingData.GBCode2312));

                //标题
                writer.WriteLine();
                writer.WriteLine("相对强度");
                string writestr = "";

                //写入积分区间(intensityDataTable的标题)
                for (int i = 0; i < intensityDataTable.Columns.Count; i++)
                {
                    writestr += intensityDataTable.Columns[i].ColumnName + ",";
                }
                writer.WriteLine(writestr);

                //写入各区间的积分值
                writestr = "积分值,";
                for (int i = 0; i < integValue.Length; i++)
                {
                    writestr += integValue[i].ToString() + ",";
                }
                writer.WriteLine(writestr);

                //写入各区间的阈值和实际计算值
                for (int j = 0; j < intensityDataTable.Rows.Count; j++)
                {
                    writestr = "";
                    for (int i = 0; i < intensityDataTable.Columns.Count; i++)
                    {
                        writestr += intensityDataTable.Rows[j].ItemArray[i].ToString() + ",";
                    }
                    writer.WriteLine(writestr);
                }
                writer.Close();

                return(true);
            }
            catch (System.Exception ex)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);    //设置为未处理
                ErrorString = ex.Message;
                CommonMethod.AddToLogFile(ex.Message);

                return(false);
            }
        }
Example #5
0
        //波数准确度和波数精度自检
        private bool Accuracy_CalThread()
        {
            Border parentBorder = accuracy_cal;

            ErrorString = null;

            try
            {
                SettingFile.Accuracy_Test accuracyTest = SettingData.settingData.accuracy_test;

                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetTitleTextDelegate(SetTitleText), "正在自检波数精度和准确度......");     //设置为正在处理

                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.RUN);  //设置为正在处理

                List <float[]> reportData = new List <float[]>();
                float[]        peakPoints = new float[accuracyTest.peakPoint.Count];
                accuracyTest.peakPoint.CopyTo(peakPoints);
                reportData.Add(peakPoints);                 //基准峰位

                string savefile = null;
                for (int i = 0; i < accuracyTest.repeat; i++)    //按光谱来处理
                {
                    if (UserCancel)
                    {
                        throw new Exception("用户取消仪器自检");
                    }

                    //设置进度条
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetProgressBarDelegate(SetProgressBar), parentBorder, accuracyTest.repeat, i);

                    if (!simulator)
                    {
                        savefile = System.IO.Path.Combine(fileSavePath, "Accuracy" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".spc");
                        CommonMethod.AddToLogFile("Accuracy Test Scan, savefile=" + savefile);
                        if ((savefile = RamanInstrument.Measurement(accuracyTest.scanPara, savefile, false)) == null)
                        {
                            throw new Exception(RamanInstrument.ErrorString);
                        }
                    }
                    else
                    {
                        savefile = tempAccuracyFiles[i];
                    }

                    //计算当前扫描光谱的峰位并加入到峰位列表中
                    CommonMethod.AddToLogFile("Accuracy Test Calculate, savefile=" + savefile);
                    float[] curPeak = Accuracy_Calculate(savefile);
                    if (curPeak == null)
                    {
                        throw new Exception(ErrorString);
                    }

                    reportData.Add(curPeak);    //加入结果表
                }

                //计算波数精度和准确度

                //计算所有光谱中的相同峰位的值
                //公式=IF(OR(B1="",C1="",D1="",E1="",F1=""),"",IF(AVERAGE(MAX(B1:F1),MIN(B1:F1))>=A1,MAX(B1:F1)-A1,MIN(B1:F1)-A1))
                float[] sureDatas = new float[peakPoints.Length];      //准确度
                float[] accDatas  = new float[peakPoints.Length];      //精度
                float   maxValue  = float.MinValue;
                float   minValue  = float.MaxValue;

                for (int i = 0; i < reportData[0].Length; i++)    //处理每个峰位
                {
                    maxValue = float.MinValue;
                    minValue = float.MaxValue;

                    //每张光谱当前峰位的最大值和最小值
                    for (int j = 1; j < reportData.Count; j++)    //每张光谱, reportData[0]为基准峰位
                    {
                        if (reportData[j][i] > maxValue)
                        {
                            maxValue = reportData[j][i];
                        }
                        if (reportData[j][i] < minValue)
                        {
                            minValue = reportData[j][i];
                        }
                    }

                    //波数准确度 IF(AVERAGE(MAX(B2:F2),MIN(B2:F2))>=A2,MAX(B2:F2)-A2,MIN(B2:F2)-A2)
                    sureDatas[i]  = ((maxValue + minValue) / 2.0f) >= peakPoints[i] ? maxValue : minValue;
                    sureDatas[i] -= peakPoints[i];

                    //波数精度 =MAX(B2:F2)-MIN(B2:F2)
                    accDatas[i] = maxValue - minValue;
                }

                //波数准确度验证结果的公式:MAX(ABS(G2),ABS(G3),ABS(G4),ABS(G5),ABS(G6),ABS(G7),ABS(G8),ABS(G9),ABS(G10),ABS(G11),ABS(G12)))
                sureResultValue = float.MinValue;
                for (int i = 0; i < sureDatas.Length; i++)
                {
                    if (Math.Abs(sureDatas[i]) > sureResultValue)
                    {
                        sureResultValue = Math.Abs(sureDatas[i]);
                    }
                }

                //波数精度验证公式:MAX(H2:H12)
                accuracyResultValue = float.MinValue;
                for (int i = 0; i < accDatas.Length; i++)
                {
                    if (accDatas[i] > accuracyResultValue)
                    {
                        accuracyResultValue = accDatas[i];
                    }
                }

                sureResultValue     = (float)(Math.Round(Math.Abs(sureResultValue) * 100) / 100.0);         //取绝对值,精确到小数点后面两位
                accuracyResultValue = (float)(Math.Round(Math.Abs(accuracyResultValue) * 100) / 100.0);     //取绝对值,精确到小数点后面两位

                //特殊处理,可以允许偏差0.2cm-1,也就是说偏差在-1.2cm-1到1.2cm-1,统一除以1.2
                if (sureResultValue > 1.0f || accuracyResultValue > 1.0f)
                {
                    //先处理最大的准确度和精度
                    sureResultValue     = sureResultValue / 1.2f;
                    accuracyResultValue = accuracyResultValue / 1.2f;

                    //处理每个峰位的准确度
                    for (int i = 0; i < sureDatas.Length; i++)
                    {
                        sureDatas[i] = sureDatas[i] / 1.2f;
                    }

                    //处理每个峰位的精度
                    for (int i = 0; i < accDatas.Length; i++)
                    {
                        accDatas[i] = accDatas[i] / 1.2f;
                    }

                    //处理每个实际峰位
                    foreach (float[] peaks in reportData)
                    {
                        if (peaks == peakPoints)
                        {
                            continue;
                        }

                        for (int j = 0; j < peakPoints.Length; j++)
                        {
                            peaks[j] = peakPoints[j] + (peaks[j] - peakPoints[j]) / 1.2f;
                        }
                    }
                }
                reportData.Add(sureDatas);
                reportData.Add(accDatas);

                //设置为已经处理
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetProgressBarDelegate(SetProgressBar), parentBorder, accuracyTest.repeat, accuracyTest.repeat);

                if (!accuracyTest.sureThresold.valueOk(sureResultValue) || !accuracyTest.accuracyThresold.valueOk(accuracyResultValue))
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);    //设置为错误
                }
                else
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.OK);
                }
                accuracySpectrumFile = savefile;

                //将结果数据记录到log文件中
                StreamWriter writer = new StreamWriter(Path.Combine(fileSavePath, "calibration.csv"), true, System.Text.Encoding.GetEncoding(SettingData.GBCode2312));

                //标题
                writer.WriteLine();
                writer.WriteLine("自检时间:" + DateTime.Now.ToString(SettingData.LongDateTimeString));
                writer.WriteLine();
                writer.WriteLine("波数准确度和波数精度");
                string writestr = "峰位,";
                for (int i = 0; i < accuracyTest.repeat; i++)
                {
                    writestr += (i + 1).ToString() + ",";
                }
                writestr += "波数准确度,波数精度";
                writer.WriteLine(writestr);

                for (int i = 0; i < peakPoints.Length; i++)
                {
                    //writestr = peakPoints[i].ToString()+",";
                    writestr = "";
                    for (int j = 0; j < reportData.Count; j++)
                    {
                        writestr += reportData[j][i].ToString() + ",";
                    }
                    //writestr += sureDatas[i].ToString() + "," + accDatas[i].ToString();
                    writer.WriteLine(writestr);
                }

                writestr = "检测结果,";
                for (int i = 0; i < accuracyTest.repeat; i++)
                {
                    writestr += ",";
                }
                writestr += sureResultValue.ToString() + "," + accuracyResultValue.ToString();
                writer.WriteLine(writestr);

                writer.Close();

                return(true);
            }
            catch (System.Exception ex)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, new SetStateDelegate(SetState), parentBorder, processStatus.ERROR);    //设置为未处理
                ErrorString = ex.Message;
                CommonMethod.AddToLogFile(ex.Message);
                return(false);
            }
        }