Esempio n. 1
0
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                var portName = cmbSerialPort.Text;
                var rate     = int.Parse(comboBoxRate.Text);
                Console.WriteLine("rate:" + rate);
                //首先判断端口是否打开
                if (null != _plcSerialPort)
                {
                    _plcSerialPort.Close();
                    _plcSerialPort = null;
                }
                //判断串口是否存在
                if (string.IsNullOrEmpty(portName))
                {
                    MessageBox.Show("请选择串口");
                    return;
                }
                Console.WriteLine(rate);
                _plcSerialPort = new PLCSerialPort(portName, rate, null);
                //打开串口
                if (!_plcSerialPort.Open())
                {
                    throw new Exception("打开串口失败");
                }

                //判断是否已经从设备中读取参数
                // if (false == this.hasInit)
                {
                    if (false == InitParameter())
                    {
                        return;
                    }
                    hasInit = true;
                    //保存设置的参数
                    AppConfig.SetValue("com", portName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 2
0
        private void ReadTestData()
        {
            this.progressBar.Value = 0;
            //MessageBox.Show("正在采集数据,请稍后。。。");
            if (false == hasInit)
            {
                MessageBox.Show("请先读取参数");
                return;
            }
            //开始检测之前首先要清空数据
            ClearDrawData();
            var portName = cmbSerialPort.Text;
            var rate     = int.Parse(comboBoxRate.Text);

            //首先判断端口是否打开
            if (null != _plcSerialPort)
            {
                _plcSerialPort.Close();
                _plcSerialPort = null;
            }
            //判断串口是否存在
            if (string.IsNullOrEmpty(portName))
            {
                MessageBox.Show("请选择串口");
                return;
            }
            _plcSerialPort = new PLCSerialPort(portName, rate, null);
            //打开串口
            if (!_plcSerialPort.Open())
            {
                throw new Exception("打开串口失败");
            }

            Task.Factory.StartNew(() =>
            {
                try
                {
                    //开启检测的工
                    this.Invoke(new Action(() =>
                    {
                        this.cmbSerialPort.Enabled = false;
                        this.btnConfirm.Enabled    = false;
                    }));

                    //在开始测试之前需要发送测试命令
                    SetValue((ushort)RegisterSetting.测试命令, 1, _plcSerialPort);
                    Thread.Sleep(200);
                    //开始读取,读取采集的状态, 如果采集到数据就把数据呈现出来
                    SetSystemStatus("等待测试");

                    try
                    {
                        //开始检测之前首先要清空数据
                        ClearDrawData();

                        this.Invoke(new Action(() => { btnConfirm.Enabled = false; }));
                        this.isReadingStatus = true;
                        //
                        var valueIndex = 0;
                        while (isReadingStatus)
                        {
                            valueIndex++;
                            this.Invoke(new Action(() =>
                            {
                                this.progressBar.Value = valueIndex;
                            }));
                            byte value = _plcSerialPort.GetD10Status();
                            Console.WriteLine($"D10状态:{value}");
                            //呈现测试的结果
                            ShowTestResult(value);
                            //已经检测完毕
                            if (value == 2)
                            {
                                //获取运行的参数,开始计算
                                GetRunParameter();
                                //开始检测
                                BeginWork();
                                break;
                            }
                            if (value == 6)
                            {
                                MessageBox.Show("长时间没有响应");
                                break;
                            }

                            Thread.Sleep(2000);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Invoke(new Action(() => { MessageBox.Show(ex.Message); }));
                    }
                    finally
                    {
                        isReadingStatus = false;
                        //按钮恢复状态
                        this.Invoke(new Action(() => { this.btnConfirm.Enabled = true; }));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    this.btnConfirm.Enabled = true;
                }
            });
        }