/// <summary>
        /// 单例赋值给List和DataGridView
        /// </summary>
        private void ShowCtrDev()
        {
            for (int i = 0; i < Module_DeviceManage.Instance.Devices.Count; i++)
            {
                /* 通过listUCDevice管理自定义控件 */
                UCDevice uCDevice = new UCDevice();
                listUCDevices.Add(uCDevice);
                Module_Device module_Device = Module_DeviceManage.Instance.Devices[i];
                /* 设备自定义控件赋值 */
                listUCDevices[i].m_strDevSubName = module_Device.Name;
                listUCDevices[i].m_strDevIP      = module_Device.visaResource;
                listUCDevices[i].m_Mac           = module_Device.MAC;
                listUCDevices[i].m_Model         = module_Device.Model;
                listUCDevices[i].m_strDevSN      = module_Device.SN;
                listUCDevices[i].m_SoftVersion   = module_Device.SoftVersion;
                listUCDevices[i].m_bOnline       = module_Device.Status;
                listUCDevices[i].updateCurrentState();

                /* 绑定初始的通道显示信息表 */
                for (int j = 1; j < 5; j++)
                {
                    module_Device.GetChannel(j).ChannelID  = j;                                                                                           //通道
                    module_Device.GetChannel(j).Collect    = true;                                                                                        //采集
                    module_Device.GetChannel(j).Tag        = "Tag" + (((int.Parse(module_Device.VirtualNumber) - 1) * 4 + j)).ToString().PadLeft(3, '0'); //Tag
                    module_Device.GetChannel(j).Scale      = "1";                                                                                         //范围
                    module_Device.GetChannel(j).Offset     = "0";                                                                                         //偏移
                    module_Device.GetChannel(j).Impedance  = "1M";                                                                                        //阻抗
                    module_Device.GetChannel(j).Coupling   = "DC";                                                                                        //耦合
                    module_Device.GetChannel(j).ProbeRatio = "*1";                                                                                        //探头比
                }
            }
        }
Ejemplo n.º 2
0
 private void iconButton_Save_Click(object sender, EventArgs e)
 {
     if (this.bUpdate)
     {
         this.folderBrowserDialog1.ShowDialog();
         string filePath = folderBrowserDialog1.SelectedPath;
         Parallel.For(0, Module_DeviceManage.Instance.Devices.Count, item =>
         {
             int iteration = item;
             Module_Device module_Device = Module_DeviceManage.Instance.Devices[iteration];
             // 读取四个通道的内存数据
             for (int i = 1; i < 5; i++)
             {
                 string path = filePath + "\\" + DateTime.Now.ToString("yyyyMM_ddHHmmss") + module_Device.Name + "CHAN" + i.ToString() + ".csv";
                 if (File.Exists(path))
                 {
                     File.Delete(path);
                 }
                 string para = " Xincrement =" + module_Device.Channels[i - 1].XIncrement.ToString() +
                               " Xreference =" + module_Device.Channels[i - 1].XReference.ToString() +
                               " Xorigin =" + module_Device.Channels[i - 1].XOrigin.ToString() +
                               " Yincrement =" + module_Device.Channels[i - 1].YIncrement.ToString() +
                               " Yreference =" + module_Device.Channels[i - 1].YReference.ToString() +
                               " Yorigin =" + module_Device.Channels[i - 1].YOrigin.ToString();
                 using (FileStream csvFs = new FileStream(path, FileMode.Create))
                 {
                     using (StreamWriter csvSw = new StreamWriter(csvFs, System.Text.Encoding.Default))
                     {
                         int j = 0;
                         csvSw.WriteLine(para);
                         while (j < Module_DeviceManage.Instance.Points)
                         {
                             csvSw.WriteLine(module_Device.Channels[i - 1].Data[j]);
                             j++;
                         }
                         csvSw.Flush();
                         //关闭流
                         csvSw.Close();
                         csvFs.Close();
                     }
                 }
             }
         });
         MessageBox.Show("Save successfully!");
     }
     else
     {
         MessageBox.Show("Only memory data is allowed to be saved,please click Update to get" +
                         "the data and then click Save.");
     }
 }
Ejemplo n.º 3
0
        private void AssignDevInfo()
        {
            devInfo = devInfo.TrimEnd(Environment.NewLine.ToCharArray());                                              //去除最后的/n
            string[]      recvDevInfo   = devInfo.Split(';');
            Module_Device module_Device = new Module_Device();                                                         //创建设备实例

            module_Device.visaResource  = m_visaName;                                                                  //Visa资源名
            module_Device.Name          = this.textBox_DevSubName.Text;                                                //设备别名
            module_Device.IP            = m_devIP;                                                                     //设备IP
            module_Device.MAC           = recvDevInfo[1];                                                              //MAC
            module_Device.Model         = recvDevInfo[0].Split(',')[1];                                                //设备型号
            module_Device.SN            = recvDevInfo[0].Split(',')[2];                                                //设备SN
            module_Device.SoftVersion   = recvDevInfo[0].Split(',')[3];                                                //设备固件版本
            module_Device.Status        = true;                                                                        //设备在线状态
            module_Device.VirtualNumber = (Module_DeviceManage.Instance.Devices.Count + 1).ToString().PadLeft(3, '0'); //虚拟编号
            Module_DeviceManage.Instance.Devices.Add(module_Device);
        }
Ejemplo n.º 4
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();
            });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// /*创建线程池*/
        /// </summary>
        private void getPscreenWaveDataTaskScheduler(bool bBlock)
        {
            //ClearChart();
            LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler();
            List <Task> tasks = new List <Task>();
            // Create a TaskFactory and pass it our custom scheduler.
            TaskFactory             factory = new TaskFactory(lcts);
            CancellationTokenSource cts     = new CancellationTokenSource();//取消线程

            // Use our factory to run a set of tasks.
            Object lockObj = new Object();

            for (int tCtr = 0; tCtr < Module_DeviceManage.Instance.Devices.Count; tCtr++)
            {
                int           iteration     = tCtr;
                Module_Device module_Device = Module_DeviceManage.Instance.Devices[iteration];
                if (module_Device.deviceVisa == null)
                {
                    module_Device.deviceVisa = new CVisa(module_Device.visaResource, 5000);
                }
                Task t = factory.StartNew(() =>
                {
                    try
                    {
                        string cmd;
                        byte[] command;
                        cmd     = CGlobalCmd.STR_CMD_SET_RUN;
                        command = Encoding.Default.GetBytes(cmd + "\n");
                        module_Device.deviceVisa.WriteBytes(command, command.Length);
                        Thread.Sleep(10);
                        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(100);
                            cmd     = CGlobalCmd.STR_CMD_SET_READMODENOR + ";" + CGlobalCmd.STR_CMD_SET_READTYPE;
                            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[] DataTemp   = new byte[1024];
                            byte[] DataScreen = new byte[1000 + 12];
                            var startIndex    = 0;
                            var dataLength    = 0;
                            int headerCount   = 0;
                            do
                            {
                                int readLength = module_Device.deviceVisa.ReadBytes(DataTemp);
                                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, DataScreen, startIndex, readLength);
                                startIndex += readLength;
                            } while (startIndex < dataLength);

                            //获取波形参数PREamble
                            cmd     = CGlobalCmd.STR_CMD_GET_WAVEPARASALL;
                            command = Encoding.Default.GetBytes(cmd + "\n");
                            module_Device.deviceVisa.WriteBytes(command, command.Length);
                            Thread.Sleep(200);
                            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();

                            //计算实际电压值
                            DataScreen           = DataScreen.Skip(12).ToArray();
                            double[] doubleYData = new double[DataScreen.Length];
                            for (int j = 0; j < DataScreen.Length; j++)
                            {
                                doubleYData[j] = (double)(DataScreen[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);
                            }
                            PaintSeries(module_Device.Name + "_" + "CHAN" + i.ToString(), doubleYData);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        module_Device.deviceVisa.Close();
                        module_Device.deviceVisa = null;
                    }
                }, cts.Token);
                tasks.Add(t);
            }
            // Wait for the tasks to complete before displaying a completion message.
            if (bBlock)
            {
                Task.WaitAll(tasks.ToArray());
            }
            cts.Dispose();
#if Debug
            Console.WriteLine("\n\nSuccessful completion.");
#endif
        }
        /// <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();
            }
        }
Ejemplo n.º 7
0
 public void SetDevice(Module_Device device)
 {
     _Device = device;
 }