using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using ConsoleApplication16; using System.Drawing.Drawing2D; namespace TestCS { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; public System.Windows.Forms.Label lblStatus; private System.Windows.Forms.Label label2; public System.Windows.Forms.Label lblConfig; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label1; public System.Windows.Forms.Label lblFileConfig; private System.Windows.Forms.Label lblChannels; private System.Windows.Forms.Label label5; private System.Windows.Forms.PictureBox picPlot; private System.Windows.Forms.Label label4; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.lblStatus = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.lblConfig = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.lblFileConfig = new System.Windows.Forms.Label(); this.lblChannels = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.picPlot = new System.Windows.Forms.PictureBox(); this.label4 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(24, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(160, 24); this.button1.TabIndex = 0; this.button1.Text = "Run DPPAPI Tests"; this.button1.Click += new System.EventHandler(this.button1_Click); // // lblStatus // this.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblStatus.Location = new System.Drawing.Point(256, 8); this.lblStatus.Name = "lblStatus"; this.lblStatus.Size = new System.Drawing.Size(176, 144); this.lblStatus.TabIndex = 1; // // label2 // this.label2.Location = new System.Drawing.Point(192, 8); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 16); this.label2.TabIndex = 2; this.label2.Text = "Status"; this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight; // // lblConfig // this.lblConfig.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblConfig.Location = new System.Drawing.Point(440, 32); this.lblConfig.Name = "lblConfig"; this.lblConfig.Size = new System.Drawing.Size(192, 392); this.lblConfig.TabIndex = 3; // // label3 // this.label3.Location = new System.Drawing.Point(440, 8); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(160, 16); this.label3.TabIndex = 4; this.label3.Text = "DPP Initial Configuration"; // // label1 // this.label1.Location = new System.Drawing.Point(640, 8); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(112, 16); this.label1.TabIndex = 6; this.label1.Text = "File Configuration"; // // lblFileConfig // this.lblFileConfig.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.lblFileConfig.Location = new System.Drawing.Point(640, 32); this.lblFileConfig.Name = "lblFileConfig"; this.lblFileConfig.Size = new System.Drawing.Size(176, 392); this.lblFileConfig.TabIndex = 5; // // lblChannels // this.lblChannels.Location = new System.Drawing.Point(83, 56); this.lblChannels.Name = "lblChannels"; this.lblChannels.Size = new System.Drawing.Size(88, 16); this.lblChannels.TabIndex = 7; // // label5 // this.label5.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.label5.Location = new System.Drawing.Point(21, 56); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(52, 16); this.label5.TabIndex = 8; this.label5.Text = "Channels"; this.label5.TextAlign = System.Drawing.ContentAlignment.TopRight; // // picPlot // this.picPlot.Location = new System.Drawing.Point(16, 168); this.picPlot.Name = "picPlot"; this.picPlot.Size = new System.Drawing.Size(424, 256); this.picPlot.TabIndex = 9; this.picPlot.TabStop = false; // // label4 // this.label4.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; this.label4.Location = new System.Drawing.Point(8, 144); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(96, 16); this.label4.TabIndex = 10; this.label4.Text = "Spectrum Plot"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(824, 438); this.Controls.Add(this.label4); this.Controls.Add(this.picPlot); this.Controls.Add(this.label5); this.Controls.Add(this.lblChannels); this.Controls.Add(this.label1); this.Controls.Add(this.lblFileConfig); this.Controls.Add(this.label3); this.Controls.Add(this.lblConfig); this.Controls.Add(this.label2); this.Controls.Add(this.lblStatus); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "CSharp DPPAPI Example"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { int[] DataBuffer; int iNumChan = 0; string strChan = ""; DataBuffer = Measurements(this.lblStatus, this.lblConfig, this.lblFileConfig, this.lblChannels); strChan = this.lblChannels.Text; if (strChan.Length > 0) { iNumChan = int.Parse(strChan); PlotData(DataBuffer, iNumChan, false); } } //static long[] Measurements(string strStat, string strCfg) static int[] Measurements(System.Windows.Forms.Label lblStat, System.Windows.Forms.Label lblDppCfg, System.Windows.Forms.Label lblFileCfg, System.Windows.Forms.Label lblChannels) { unsafe { void* openDppApi; int NumDevice; int NumChan; int[] DataBuffer = new int[8192]; byte[] szStatus = new byte[2000]; byte[] szConfig = new byte[2000]; byte[] szConfigFile = new byte[2000]; openDppApi = forDLL.OpenDppApi(); NumDevice = forDLL.OpenUSBDevice(openDppApi); forDLL.GetStatusString(openDppApi, 1, szStatus, 2000); // get device status display string string strStatus = System.Text.Encoding.UTF8.GetString(szStatus); lblStat.Text = strStatus; //MessageBox.Show(strStatus, "Status", MessageBoxButtons.OK, MessageBoxIcon.Stop); forDLL.GetConfigFromDpp(openDppApi); forDLL.GetConfigString(openDppApi, szConfig, 2000); // get device config display string string strDppCfg = System.Text.Encoding.UTF8.GetString(szConfig); lblDppCfg.Text = strDppCfg; string strAppPath = Environment.CurrentDirectory; string strCfgFilename = strAppPath + @"\DPP.cfg"; byte[] szCfgFilename = System.Text.Encoding.UTF8.GetBytes(strCfgFilename); forDLL.GetConfigFromFile(openDppApi,szCfgFilename,(int)szCfgFilename.Length,forDLL.DPPDP5); forDLL.GetConfigString(openDppApi, szConfigFile, 2000); // get device config display string string strDppCfgFile = System.Text.Encoding.UTF8.GetString(szConfigFile); lblFileCfg.Text = strDppCfgFile; forDLL.SendConfigToDpp(openDppApi); NumChan = forDLL.GetDppData(openDppApi, DataBuffer); lblChannels.Text = NumChan.ToString(); forDLL.CloseUSBDevice(openDppApi); forDLL.CloseDppApi(openDppApi); return DataBuffer; } } //------------------------------------------------------------------- // PlotData // Calculates and saves the plot buffer data then calls UpdatePlot // OptScaleType = false; //linear // OptScaleType = true; //log //------------------------------------------------------------------- public void PlotData(int[] DataBuffer, int NumChan, bool OptScaleType) { int i; long YMAX; long XPlotMAX; long YPlotMAX; int[] PlotBuffer = new int[8192]; XPlotMAX = NumChan - 1; if (NumChan > 0) { //if number of channels > 0 plot data YMAX = 0; for(i = 0; i < NumChan; i++) { //get the largest value to be displayed if (OptScaleType) { //get the Y maximum value for the select scale if (Math.Log(0.1 + DataBuffer[i]) > YMAX) { YMAX = (int)Math.Log(0.1 + DataBuffer[i]); } } else { if (DataBuffer[i] > YMAX) { YMAX = DataBuffer[i]; } } } if (YMAX < 1) { return; } //if no y scale exit sub if (OptScaleType) { //adjust plot to include entire high peak YMAX = (YMAX * 10) + 10; } else { if (YMAX < 20) { YMAX = YMAX + 2; } else { YMAX = YMAX + 10; } } for (i = 0; i < NumChan;i++ ) { //get the largest value to be displayed if (OptScaleType) { PlotBuffer[i] = (int)Math.Log(0.1 + DataBuffer[i]) * 10; } else { PlotBuffer[i] = DataBuffer[i]; } } YPlotMAX = YMAX; UpdatePlot(PlotBuffer,XPlotMAX,YPlotMAX); } } //------------------------------------------------------------------- // UpdatePlot // Draws the plot display with the current plot buffer //------------------------------------------------------------------- private void UpdatePlot(int[] PlotBuffer, long XPlotMAX, long YPlotMAX) { picPlot.CreateGraphics().Clear(this.BackColor); picPlot.Refresh(); Graphics picGraphic = this.picPlot.CreateGraphics(); Pen picPen = new Pen(Color.Red); //Pen picPen = new Pen(Color.Black); double XScaleFactor = (double) this.picPlot.Width; if (XPlotMAX > 0) XScaleFactor /= (double)(XPlotMAX + 1); //XScaleFactor *= 2; double YScaleFactor = (double) this.picPlot.Height; if (YPlotMAX > 0) YScaleFactor /= (double)(YPlotMAX + 1); int pH = this.picPlot.Height; //YScaleFactor *= -1.0; double x0, y0, x1, y1, x2, y2; x0 = 0; y0 = 0; y0++; for(int X=0; X<=XPlotMAX; X++) { x1 = X; y1 = PlotBuffer[X]; if (y1 < 0) { y1 = 0; } y1++; x2 = X + 1; y2 = PlotBuffer[X + 1]; if (y2 < 0) { y2 = 0; } y2++; picGraphic.DrawLine(picPen, (int)x0, pH - (int)y0, (int)(x1 * XScaleFactor), pH - (int)(y1 * YScaleFactor)); picGraphic.DrawLine(picPen, (int)(x1 * XScaleFactor), pH - (int)(y1 * YScaleFactor), (int)(x2 * XScaleFactor), pH - (int)(y2 * YScaleFactor)); picGraphic.DrawLine(picPen, (int)(x1 * XScaleFactor), pH - 0, (int)(x1 * XScaleFactor), pH - (int)(y1 * YScaleFactor)); picGraphic.DrawLine(picPen, (int)(x2 * XScaleFactor), pH - 0, (int)(x2 * XScaleFactor), pH - (int)(y2 * YScaleFactor)); x0 = (int)(x2 * XScaleFactor); y0 = (int)(y2 * YScaleFactor); y0++; } } private void Form1_Load(object sender, System.EventArgs e) { } } }