Пример #1
0
        /// <summary>
        /// 手动获取内存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iconButton_Update_Click(object sender, EventArgs e)
        {
            if (!Form_DeviceConfig.bConfig)
            {
                MessageBox.Show("Please click the Apply button!");
                return;
            }
            //结束屏幕绘制线程
            bool bWaiting = false;

            if (bCollect)
            {
                bCollect = false;
                bWaiting = true;
                this.iconButton_Collect.IconChar  = FontAwesome.Sharp.IconChar.Play;
                this.iconButton_Collect.IconColor = Color.Green;
                Task.Run(() =>
                {
                    sema_1.WaitOne();
                    collectTaskCancel.Cancel();
                    collectTask.Wait();
                    collectTaskCancel.Dispose();
                    collectTaskCancel = new CancellationTokenSource();
                    sema_2.Release();
                });
            }

            Task.Run(() => {
                Form_Progressbar form_Progressbar = new Form_Progressbar();
                /* 开启进度条 */
                CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
                cancelTokenSource.Token.Register(() =>
                {
                    form_Progressbar.CloseProcess();//委托方法调用进度条关闭
                    DialogResult res = MessageBox.Show("Paint Completed", "Paint", MessageBoxButtons.OK);
                    if (res == DialogResult.OK)
                    {
                        //RaiseEvent("ProcessClose");
                        return;
                    }
                });
                Task.Run(() =>
                {
                    form_Progressbar.ProcessMarquee("Painting...");                  //设置进度条显示为左右转动
                    form_Progressbar.StartPosition = FormStartPosition.CenterScreen; //程序中间
                    form_Progressbar.ShowDialog();
                }, cancelTokenSource.Token);
                bUpdate = true;
                ClearChart();
                if (bWaiting == true)
                {
                    sema_2.WaitOne();
                }
                //并行执行,Stop获取内存数据
                Parallel.For(0, Module_DeviceManage.Instance.Devices.Count, item =>
                {
                    int iteration = item;
                    Module_Device module_Device = Module_DeviceManage.Instance.Devices[iteration];
                    if (module_Device.deviceVisa != null)
                    {
                        module_Device.deviceVisa.Close();
                    }
                    try
                    {
                        module_Device.deviceVisa = new CVisa(module_Device.visaResource, 5000);
                        // 开启Stop,读取内存波形
                        string cmd     = CGlobalCmd.STR_CMD_SET_STOP + "\n";
                        byte[] command = Encoding.Default.GetBytes(cmd);
                        module_Device.deviceVisa.WriteBytes(command, command.Length);
                        Thread.Sleep(200);

                        // 读取四个通道的内存数据
                        for (int i = 1; i < 5; i++)
                        {
                            cmd     = CGlobalCmd.STR_CMD_SET_CHANSOURCE;
                            cmd     = cmd.Replace("<n>", i.ToString());
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(50);
                            cmd     = CGlobalCmd.STR_CMD_SET_READMODE + ";" + CGlobalCmd.STR_CMD_SET_READTYPE;
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(50);
                            cmd     = CGlobalCmd.STR_CMD_SET_READPOINT + Module_DeviceManage.Instance.Points.ToString();
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(100);
                            cmd     = CGlobalCmd.STR_CMD_GET_READDATA;
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(200);

                            //回读内存点
                            byte[] Data     = new byte[Module_DeviceManage.Instance.Points + 12];
                            byte[] DataTemp = new byte[6144];
                            var startIndex  = 0;
                            var dataLength  = 0;
                            int headerCount = 0;
                            int readLength  = 0;
                            do
                            {
                                readLength = module_Device.deviceVisa.ReadBytes(DataTemp, DataTemp.Length);//此句需要加偏移
                                if (readLength == 0)
                                {
                                    break;
                                }
                                if (startIndex == 0)
                                {
                                    var N       = DataTemp[1] - '0';
                                    headerCount = N + 2;
                                    dataLength  = Convert.ToInt32(Encoding.ASCII.GetString(DataTemp, 2, N)) + headerCount + 1;
                                }
                                // cpy data
                                Buffer.BlockCopy(DataTemp, 0, Data, startIndex, readLength);
                                startIndex += readLength;
                            } while (startIndex < dataLength);

                            Data = Data.Skip(12).ToArray();
                            Thread.Sleep(1000);

                            //获取波形参数PREamble
                            cmd     = CGlobalCmd.STR_CMD_GET_WAVEPARASALL;
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(50);
                            byte[] DataPREamble    = new byte[1024];
                            int readLengthPREamble = module_Device.deviceVisa.ReadBytes(DataPREamble, 1024);
                            string result          = Encoding.Default.GetString(DataPREamble, 0, readLengthPREamble);
                            string[] res           = result.TrimEnd(Environment.NewLine.ToCharArray()).Split(',');
                            module_Device.Channels[i - 1].XIncrement = res[4].ToString();
                            module_Device.Channels[i - 1].XOrigin    = res[5].ToString();
                            module_Device.Channels[i - 1].XReference = res[6].ToString();
                            module_Device.Channels[i - 1].YIncrement = res[7].ToString();
                            module_Device.Channels[i - 1].YOrigin    = res[8].ToString();
                            module_Device.Channels[i - 1].YReference = res[9].ToString();


                            double[] doubleData = new double[Data.Length];
                            for (int j = 0; j < Data.Length; j++)
                            {
                                doubleData[j] = (double)(Data[j] - (double.Parse(module_Device.Channels[i - 1].YOrigin) + double.Parse(module_Device.Channels[i - 1].YReference))) * double.Parse(module_Device.Channels[i - 1].YIncrement);
                            }
                            module_Device.Channels[i - 1].SetData(Data, 0, Data.Length);
                            module_Device.Channels[i - 1].SetDoubleData(doubleData, 0, Data.Length);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                });
                PaintMemorySeries();
                cancelTokenSource.Cancel();
            });
        }
        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iconButton_Config_Click(object sender, EventArgs e)
        {
            if (IsEmptyCheckout()) //空值判断
            {
                MessageBox.Show("Null value or no device information, please enter or register the instrument and click pply");
                return;
            }
            else
            {
                if (IsValidCheck()) //参数有效性判断
                {
                    MessageBox.Show("Invalid parameters exist, please check.", "Warnning!");
                    return;
                }

                Form_Progressbar form_Progressbar = new Form_Progressbar();
                /* 开启进度条 */
                CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
                cancelTokenSource.Token.Register(() =>
                {
                    form_Progressbar.CloseProcess();//委托方法调用进度条关闭
                    DialogResult res = MessageBox.Show("Channel configuration is complete", "Configuration", MessageBoxButtons.OK);
                    if (res == DialogResult.OK)
                    {
                        //RaiseEvent("ProcessClose");
                        return;
                    }
                });
                bConfig = true;
                Task.Run(() =>
                {
                    form_Progressbar.ProcessMarquee("Configuring...");               //设置进度条显示为左右转动
                    form_Progressbar.StartPosition = FormStartPosition.CenterScreen; //程序中间
                    form_Progressbar.ShowDialog();
                }, cancelTokenSource.Token);

                /* 仪器设备信息 */
                Dictionary <string, string> device_CommonSCPI = new Dictionary <string, string>();
                device_CommonSCPI.Add("TriggerSource", comboBox_TriggeSource.Text);                                                //触发源
                device_CommonSCPI.Add("TriggerMode", comboBox_TrigStyle.Text);                                                     //触发方式
                device_CommonSCPI.Add("MemDepth", comboBox_StorgeDepth.Text);                                                      //存储深度
                device_CommonSCPI.Add("HoldOff", CUnitTransform.UnitTransF(comboBox_TrigHoldUnit, textBox_TrigHold));              //触发释抑
                device_CommonSCPI.Add("HorizontalTimebase", CUnitTransform.UnitTransF(comboBox_HorTimeUnit, textBox_HorTime));     //水平时基
                device_CommonSCPI.Add("HorizontalOffset", CUnitTransform.UnitTransF(comboBox_HoriOffsetUnit, textBox_HoriOffset)); //水平偏移
                device_CommonSCPI.Add("TriggerLevel", CUnitTransform.UnitTransF(comboBox_TriggerLevelUnit, textBox_TriggerLevel)); //触发电平

                DataTable dataTable_DeviceChannel = dataGridView_ChanSet.DataSource as DataTable;                                  //将DataGridView转换为DataTable
                /* 赋值给单例 */
                Module_DeviceManage.Instance.AssignDeviceAndChannel(device_CommonSCPI, dataTable_DeviceChannel);
                /* 建立Visa连接 */
                ClassVisa._Visa_Init();//初始化句柄
                Parallel.For(0, Module_DeviceManage.Instance.Devices.Count, i =>
                {
                    Module_Device module_Device = Module_DeviceManage.Instance.Devices[i];
                    if (module_Device.Status == true)
                    {
                        if (module_Device.deviceVisa == null)
                        {
                            try
                            {
                                module_Device.deviceVisa = new CVisa(module_Device.visaResource);
                            }
                            catch (Exception)
                            {
                                module_Device.Status = false;
                            }
                        }
                    }
                    else
                    {
                        module_Device.deviceVisa.Close();
                        module_Device.deviceVisa = null;
                    }
                });
                /* 发送指令 */
                Module_DeviceManage.Instance.SetDevices();
                Module_DeviceManage.Instance.MaxChannelModel = Module_DeviceManage.Instance.GetMaxChannelMode();

                /* 设置延时,每台设备设置5s延时用于Visa传输指令 */
                Thread.Sleep(5000 * Module_DeviceManage.Instance.Devices.Count);
                cancelTokenSource.Cancel();
            }
        }