示例#1
0
        private void BkWorkerReceiveData_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //如果窗体已经被释放,返回
            if (this.IsDisposed == true)
            {
                return;
            }
            float area = _channel.ProbeArea;                             //探测面积
            int   messageBufferLength = 62;                              //最短报文长度
            int   errNumber           = 0;                               //报文接收出现错误计数器

            byte[] receiveBufferMessage      = null;                     //存储接收报文信息缓冲区
            IList <MeasureData> measureDataS = new List <MeasureData>(); //解析后报文结构数据存储List对象

            if (e.UserState is byte[])
            {
                receiveBufferMessage = (byte[])e.UserState;
            }
            //接收报文数据为空
            try
            {
                if (receiveBufferMessage.Length < messageBufferLength)
                {
                    errNumber++;
                    //数据接收出现错误次数超限
                    if (errNumber >= 2)
                    {
                        if (_isEnglish == true)
                        {
                            MessageBox.Show(@"Communication error! Please check whether the communication is normal.");
                            return;
                        }
                        else
                        {
                            MessageBox.Show(@"通讯错误!请检查通讯是否正常。");
                            return;
                        }
                    }
                    return;
                }
            }
            catch (Exception EX_NAME)
            {
                Tools.ErrorLog(EX_NAME.ToString());
                if (_isEnglish == true)
                {
                    MessageBox.Show(@"Communication error! Please check whether the communication is normal.");
                    return;
                }
                else
                {
                    MessageBox.Show(@"通讯错误!请检查通讯是否正常。");
                    return;
                }
                // Console.WriteLine(EX_NAME);
                throw;
            }
            //解析P数据报文
            if (receiveBufferMessage[0] == Convert.ToByte('P'))
            {
                _channelParameters = Message.ExplainMessage <ChannelParameter>(receiveBufferMessage);//解析报文
                foreach (var itemParameter in _channelParameters)
                {
                    if (CmbChannelSelection.Text == itemParameter.Channel.ChannelName_English || CmbChannelSelection.Text == itemParameter.Channel.ChannelName)
                    {
                        /*
                         * 高压阈值赋值
                         */
                        TxtHV.Text = itemParameter.PresetHV.ToString();
                        Txtα.Text  = itemParameter.AlphaThreshold.ToString();
                        Txtβ.Text  = itemParameter.BetaThreshold.ToString();
                        //当前通道道盒参数
                        _setChannelParameter = itemParameter;
                    }
                }
            }

            //解析C数据报文
            if (receiveBufferMessage[0] == Convert.ToByte('C'))
            {
                IList <MeasureData> measureDatas = new List <MeasureData>();
                measureDatas = Message.ExplainMessage <MeasureData>(receiveBufferMessage);
                //扔掉2次预读取的数据
                if (throwDataCount < 2)
                {
                    for (int i = 0; i < measureDataS.Count; i++)
                    {
                        measureDataS[i].Alpha          = 0;
                        measureDataS[i].Beta           = 0;
                        measureDataS[i].InfraredStatus = 0;
                    }
                    throwDataCount++;
                    _commPort.ClearPortData();
                    return;
                }

                //解析通道数据
                foreach (var item in measureDatas)
                {
                    if (_channel.ChannelID == item.Channel.ChannelID)
                    {
                        _alphacps += item.Alpha;             //单次/时间的累加和
                        _betacps  += item.Beta;              //单次/时间的累加和
                        _alphacnt  = _alphacnt + item.Alpha; //类型内计数累加
                        _betacnt   = _betacnt + item.Beta;   //类型内计数累加
                        if (_channel.ChannelID == 7)
                        {
                            _hv = 500;
                        }
                        else
                        {
                            _hv = item.HV;//高压
                        }
                    }
                }
                _prgTime++;//进度条计数
                if (_prgTime > Convert.ToInt32(TxtMeasuringTime.Text))
                {
                    _prgTime           = 0;
                    PrgCalibrate.Value = _prgTime;//进度条显示
                }
                else
                {
                    PrgCalibrate.Value = _prgTime;//进度条显示
                }
                if (_sclaeState == false)
                {
                    if (_isEnglish)
                    {
                        _addInformation[0] = "BKG";
                    }
                    else
                    {
                        _addInformation[0] = "本底测量";
                    }
                    if (this.IsDisposed != true)
                    {
                        _addInformation[1] = CmbChannelSelection.Text;
                        _addInformation[2] = area.ToString();
                        _addInformation[3] = (String.Format("{0:f2}", (_alphacps / Convert.ToSingle(TxtMeasuringTime.Text)))).ToString();
                        _addInformation[4] = (String.Format("{0:f2}", (_betacps / Convert.ToSingle(TxtMeasuringTime.Text)))).ToString();
                        _addInformation[5] = _hv.ToString();
                        _measuringTime--;
                    }
                    if (_measuringTime == 0)
                    {
                        _measuringCount--;                                       //时间为0次数减1
                        _measuringTime = Convert.ToInt16(TxtMeasuringTime.Text); //次数减1后,时间恢复测量时间
                        DgvInformation.Rows.Add(_addInformation);                //添加本次数据
                        _alphacps = 0;
                        _betacps  = 0;
                        _prgTime  = 0;//进度条时间
                    }

                    if (_measuringCount == 0)
                    {
                        _alphaNb        = (_alphacnt / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text))); //本底总计数的平均值
                        _betaNb         = (_betacnt / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text)));  //本地总计数的平均值
                        _alphacnt       = 0;                                                                                         //类型内计数清零
                        _betacnt        = 0;                                                                                         //类型内计数清零
                        _measuringTime  = Convert.ToInt16(TxtMeasuringTime.Text) + 1;                                                //恢复时间为填写时间
                        _measuringCount = Convert.ToInt16(TxtCount.Text);                                                            //恢复次数

                        bkWorkerReceiveData.CancelAsync();
                        Thread.Sleep(500);
                        if (_isEnglish)
                        {
                            if (MessageBox.Show(@"Please insert the source!", @"Message") == DialogResult.OK)
                            {
                                _sclaeState = true;//刻度测量状态更换为"带源测量"
                                _alphacps   = 0;
                                _betacps    = 0;
                                bkWorkerReceiveData.RunWorkerAsync();
                            }
                        }
                        else
                        {
                            if (MessageBox.Show(@"请放入放射源!", @"提示") == DialogResult.OK)
                            {
                                throwDataCount = 0;
                                _sclaeState    = true;//刻度测量状态更换为"带源测量"
                                _alphacps      = 0;
                                _betacps       = 0;
                                _alphacnt      = 0;
                                _betacnt       = 0;
                                _prgTime       = 0;//进度条时间
                                bkWorkerReceiveData.RunWorkerAsync();
                                Thread.Sleep(200);
                            }
                        }
                        return;
                    }
                }

                if (_sclaeState == true && _measuringCount == 0)
                {
                    return;
                }
                if (_sclaeState == true)
                {
                    //_prgTime++;
                    //PrgCalibrate.Value = _prgTime;
                    if (_isEnglish)
                    {
                        _addInformation[0] = "Radioactive source";
                    }
                    else
                    {
                        _addInformation[0] = "带源测量";
                    }
                    _addInformation[1] = CmbChannelSelection.Text;
                    _addInformation[2] = area.ToString();
                    _addInformation[3] = (String.Format("{0:f2}", (_alphacps / Convert.ToSingle(TxtMeasuringTime.Text)))).ToString();
                    _addInformation[4] = (String.Format("{0:f2}", (_betacps / Convert.ToSingle(TxtMeasuringTime.Text)))).ToString();
                    _addInformation[5] = _hv.ToString();
                    _measuringTime--;
                    if (_measuringTime == 0)
                    {
                        _measuringCount--;
                        _measuringTime = Convert.ToInt16(TxtMeasuringTime.Text);
                        DgvInformation.Rows.Add(_addInformation);
                        _alphacps = 0;
                        _betacps  = 0;
                        _prgTime  = 0;//进度条时间
                    }

                    if (_measuringCount == 0)
                    {
                        //挂起线程
                        bkWorkerReceiveData.CancelAsync();
                        _alphaNr  = (_alphacnt / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text)));; //带源总计数的平均值
                        _betaNr   = (_betacnt / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text)));   //带源总计数的平均值
                        _effAlpha = (((_alphaNr - _alphaNb) < 0?0:(_alphaNr - _alphaNb)) / Convert.ToSingle(TxtSFR.Text));      //Alpha效率
                        _effBeta  = (((_betaNr - _betaNb) < 0?0:(_betaNr - _betaNb)) / Convert.ToSingle(TxtSFR.Text));          //Beta效率
                        _eff      = _effAlpha > _effBeta ? _effAlpha * 100 : _effBeta * 100;                                    //效率取Alpha或Beta的最大值
                        if (_eff > 0)
                        {
                            //Beta探测下限
                            _betaMda =
                                (_p * (_betaNb / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text)) +
                                       _betaNb / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text) * 2)) +
                                 (0.005f * _betaNb)) / (_effBeta / 2) / area;
                            //Alpha探测下限
                            _alphaMda =
                                (_p * (_alphaNb / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text)) +
                                       _alphaNb / (Convert.ToSingle(TxtMeasuringTime.Text) * Convert.ToSingle(TxtCount.Text) * 2)) +
                                 (0.005f * _betaNb)) / (_effBeta / 2) / area;
                        }
                        else
                        {
                            _betaMda  = 0;
                            _alphaMda = 0;
                        }

                        _resultMda = _effAlpha > _effBeta ? _alphaMda : _betaMda; //探测下限取值和效率一样的
                        float rangeMda;                                           //探测下限范围
                        //取当前核素的对象
                        var listef = _efficiencyList.Where(n =>
                                                           n.Channel.ChannelID == _channel.ChannelID && n.NuclideName == CmbNuclideSelect.Text)
                                     .ToList();
                        foreach (var item in listef)
                        {
                            _changedEfficiency = item;//当前核素的对象
                        }
                        //根据核素类型判断探测下限范围
                        if (_changedEfficiency.NuclideType == "α")
                        {
                            rangeMda = 0.037f;
                        }
                        else if (_changedEfficiency.NuclideType == "β")
                        {
                            rangeMda = 0.37f;
                        }
                        else
                        {
                            rangeMda = 0;
                        }
                        //按通道用不同的判断方法判断探测器的合格
                        if (_channel.ChannelID == 7)
                        {
                            if (_eff >= 20)
                            {
                                _isStandardize = "探测器合格!";
                            }
                            else
                            {
                                _isStandardize = "探测器不合格!";
                            }
                        }
                        else
                        {
                            //判断效率大于30%同时探测下限在一定范围内为合格
                            if (_eff >= 20 && _resultMda <= rangeMda)//需要补充代码的探测器下限范围
                            {
                                _isStandardize = "探测器合格!";
                            }
                            else
                            {
                                _isStandardize = "探测器不合格!";
                            }
                        }
                        //串道比计算
                        Calibration calibration_AlphaBetaPercent = new Calibration();
                        if (_alphacnt > 0 || _betacnt > 0)
                        {
                            if (_efficiencyList.First(x => x.NuclideType == "α").NuclideName == CmbNuclideSelect.Text)
                            {
                                //alpha 串道计数
                                calibration_AlphaBetaPercent.AlphaBetaPercent = _betacnt / _alphacnt * 100;
                            }
                            else
                            {
                                //beta 串道计数
                                calibration_AlphaBetaPercent.AlphaBetaPercent = _alphacnt / _betacnt * 100;
                            }
                        }


                        if (calibration_AlphaBetaPercent.AlphaBetaPercent <= 0)
                        {
                            //串道比小与零强制为0
                            calibration_AlphaBetaPercent.AlphaBetaPercent = 0;
                        }
                        //向刻度数据表添加信息
                        Calibration calibration = new Calibration
                        {
                            Efficiency       = _eff > 0 ?Convert.ToSingle(_eff.ToString("0.000")) : 0,                           //效率
                            MDA              = _resultMda > 0 ?Convert.ToSingle(_resultMda.ToString("0.000")):0,
                            AlphaBetaPercent = Convert.ToSingle(calibration_AlphaBetaPercent.AlphaBetaPercent.ToString("0.00")), //串道比
                            CalibrationTime  = DateTime.Now,
                            Channel          = _changedEfficiency.Channel,
                            HighVoltage      = _setChannelParameter.PresetHV,
                            Threshold        =
                                Convert.ToString(
                                    $"α:{_setChannelParameter.AlphaThreshold};β:{_setChannelParameter.BetaThreshold}")
                        };
                        calibration.AddData(calibration);
                        //合格则把结果存入数据库
                        if (_isStandardize == "探测器合格!")
                        {
                            //向核素数据表添加信息
                            _changedEfficiency.Efficiency = _eff;
                            _changedEfficiency.SetParameter(_changedEfficiency);
                        }
                        //不合格则把数据存入文本文件
                        else if (_isStandardize == "探测器不合格!")
                        {
                            string path = $@"CalibrationLog\{DateTime.Now.ToString("yyyyMMddTHHmmss")}.txt";
                            if (!File.Exists(path))
                            {
                                // Create a file to write to.
                                FileInfo fi1 = new FileInfo(path);
                                using (StreamWriter sw = fi1.CreateText())
                                {
                                    sw.WriteLine("状态\t通道\t面积(cm2)\tα计数\tβ计数\t高压(V)");
                                    string str = "";
                                    for (int i = 0; i < DgvInformation.Rows.Count - 1; i++)
                                    {
                                        for (int j = 0; j < DgvInformation.Columns.Count; j++)
                                        {
                                            str = DgvInformation.Rows[i].Cells[j].Value.ToString().Trim();
                                            str = str + "\t";
                                            sw.Write(str);
                                        }
                                        sw.WriteLine("");
                                    }
                                    sw.WriteLine($@"{CmbNuclideSelect.Text}的效率:{_eff:F1}%,可探测下限:{_resultMda:F3}Bq/cm^2;串道比:{calibration_AlphaBetaPercent.AlphaBetaPercent}%;");
                                    sw.Close();
                                }
                            }
                        }
                        //测量结果
                        TxtResult.Text = $@"{CmbNuclideSelect.Text}的效率:{_eff:F1}%,可探测下限:{_resultMda:F3}Bq/cm^2;串道比:{calibration_AlphaBetaPercent.AlphaBetaPercent:f2}%;";
                        //使刻度按钮可以使用
                        BtnCalibrate.Enabled   = true;
                        BtnSet.Enabled         = true;
                        GrpCalibration.Enabled = true;
                    }
                }
            }
        }