ulai02.cs
来自「A .net program for datacollector」· CS 代码 · 共 400 行
CS
400 行
// ==============================================================================
// File: ULAI02.CS
//
// Library Call Demonstrated: Mccdaq.MccBoard.AInScan()in Foreground mode
// with scan options = MccDaq.ScanOptions.ConvertData
//
// Purpose: Scans a range of A/D Input Channels and stores
// the sample data in an array.
//
// Demonstration: Displays the analog input on up to eight channels.
//
// Other Library Calls: MccDaq.MccService.ErrHandling()
// MccDaq.MccService.WinBufAlloc
// MccDaq.MccService.WinBufToArray()
// MccDaq.MccService.WinBufFree()
//
// Special Requirements: Board 0 must have an A/D converter.
// Analog signals on up to eight input channels.
//
// ==============================================================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.IO;
using MccDaq;
namespace ULAI02
{
public class frmDataDisplay : System.Windows.Forms.Form
{
// Required by the Windows Form Designer
private System.ComponentModel.IContainer components;
public ToolTip ToolTip1;
public Button cmdStopConvert;
public Button cmdStart;
public Label Label1;
public Label lblDemoFunction;
//public Label[] lblADData;
int NumPoints; // Number of data points to collect
int NumPerCh; // Number of data points per channel
const int FirstPoint = 0; // set first element in buffer to transfer to array
//!!!!!!!!!!!!!!!!!!!!!!!
const int ChanNum=4; // Number of collecting channels
//!!!!!!!!!!!!!!!!!!!!!!
const char space='\n';
private MccDaq.MccBoard DaqBoard;
private ushort[] ADData;
public System.Windows.Forms.Label label2;
public System.Windows.Forms.TextBox txtSamplesPerCh; // dimension an array to hold the input values
private int MemHandle = 0; // define a variable to contain the handle for memory allocated
// by Windows through MccDaq.MccService.WinBufAlloc()
public frmDataDisplay()
{
MccDaq.ErrorInfo ULStat;
// This call is required by the Windows Form Designer.
InitializeComponent();
// Initiate error handling
// activating error handling will trap errors like
// bad channel numbers and non-configured conditions.
// Parameters:
// MccDaq.ErrorReporting.PrintAll :all warnings and errors encountered will be printed
// MccDaq.ErrorHandling.StopAll :if an error is encountered, the program will stop
ULStat = MccDaq.MccService.ErrHandling(MccDaq.ErrorReporting.PrintAll, MccDaq.ErrorHandling.StopAll);
// Create a new MccBoard object for Board 1
DaqBoard = new MccDaq.MccBoard(1);
// Get the total number of points to collect
//NumPerCh = int.Parse(txtSamplesPerCh.Text);
//txtSamplesPerCh.Text = NumPerCh.ToString();
//-------------------------------------------------------
NumPerCh=160000;
//-------------------------------------------------------
NumPoints=NumPerCh*ChanNum;
// Initiate the data array
ADData=new ushort[NumPoints];
// Allocate memory buffer to hold data..
MemHandle = MccDaq.MccService.WinBufAlloc(NumPoints); // set aside memory to hold data
// This gives us access to labels via an indexed array
//lblADData = (new Label[] {this._lblADData_0, this._lblADData_1, this._lblADData_2, this._lblADData_3, this._lblADData_4, this._lblADData_5, this._lblADData_6, this._lblADData_7});
}
// Form overrides dispose to clean up the component list.
protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
// be sure to release the memory buffer...
if ( MemHandle != 0)
MccDaq.MccService.WinBufFree(MemHandle);
}
base.Dispose(Disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.cmdStopConvert = new System.Windows.Forms.Button();
this.cmdStart = new System.Windows.Forms.Button();
this.Label1 = new System.Windows.Forms.Label();
this.lblDemoFunction = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtSamplesPerCh = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// cmdStopConvert
//
this.cmdStopConvert.BackColor = System.Drawing.SystemColors.Control;
this.cmdStopConvert.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdStopConvert.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.cmdStopConvert.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdStopConvert.Location = new System.Drawing.Point(240, 152);
this.cmdStopConvert.Name = "cmdStopConvert";
this.cmdStopConvert.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdStopConvert.Size = new System.Drawing.Size(58, 26);
this.cmdStopConvert.TabIndex = 17;
this.cmdStopConvert.Text = "Quit";
this.cmdStopConvert.Click += new System.EventHandler(this.cmdStopConvert_Click);
//
// cmdStart
//
this.cmdStart.BackColor = System.Drawing.SystemColors.Control;
this.cmdStart.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdStart.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.cmdStart.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdStart.Location = new System.Drawing.Point(88, 152);
this.cmdStart.Name = "cmdStart";
this.cmdStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdStart.Size = new System.Drawing.Size(58, 26);
this.cmdStart.TabIndex = 18;
this.cmdStart.Text = "Start";
this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click);
//
// Label1
//
this.Label1.BackColor = System.Drawing.SystemColors.Window;
this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
this.Label1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Label1.ForeColor = System.Drawing.SystemColors.WindowText;
this.Label1.Location = new System.Drawing.Point(56, 66);
this.Label1.Name = "Label1";
this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label1.Size = new System.Drawing.Size(136, 17);
this.Label1.TabIndex = 19;
this.Label1.Text = "Measure Channels 0 to 3";
//
// lblDemoFunction
//
this.lblDemoFunction.BackColor = System.Drawing.SystemColors.Window;
this.lblDemoFunction.Cursor = System.Windows.Forms.Cursors.Default;
this.lblDemoFunction.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblDemoFunction.ForeColor = System.Drawing.SystemColors.WindowText;
this.lblDemoFunction.Location = new System.Drawing.Point(8, 8);
this.lblDemoFunction.Name = "lblDemoFunction";
this.lblDemoFunction.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.lblDemoFunction.Size = new System.Drawing.Size(337, 41);
this.lblDemoFunction.TabIndex = 0;
this.lblDemoFunction.Text = "Demonstration of MccBoard.AInScan() with scan option set to MccDaq.ScanOptions.Fo" +
"reground";
this.lblDemoFunction.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.ForeColor = System.Drawing.Color.Black;
this.label2.Location = new System.Drawing.Point(56, 104);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(120, 23);
this.label2.TabIndex = 22;
this.label2.Text = "Samples per channel";
//
// txtSamplesPerCh
//
this.txtSamplesPerCh.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.txtSamplesPerCh.Location = new System.Drawing.Point(168, 104);
this.txtSamplesPerCh.Name = "txtSamplesPerCh";
this.txtSamplesPerCh.Size = new System.Drawing.Size(112, 20);
this.txtSamplesPerCh.TabIndex = 23;
this.txtSamplesPerCh.Text = "16000";
//
// frmDataDisplay
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 13);
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(416, 229);
this.Controls.Add(this.txtSamplesPerCh);
this.Controls.Add(this.label2);
this.Controls.Add(this.cmdStopConvert);
this.Controls.Add(this.cmdStart);
this.Controls.Add(this.Label1);
this.Controls.Add(this.lblDemoFunction);
this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.ForeColor = System.Drawing.Color.Blue;
this.Location = new System.Drawing.Point(190, 108);
this.Name = "frmDataDisplay";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Universal Library Analog Input Scan";
this.Load += new System.EventHandler(this.frmDataDisplay_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmDataDisplay());
}
private void cmdStart_Click(object eventSender, System.EventArgs eventArgs)
{
int j;
int i;
MccDaq.ErrorInfo ULStat;
MccDaq.Range Range;
MccDaq.ScanOptions Options;
int Rate;
int Count;
int LowChan;
int HighChan;
float TemSamp;
cmdStart.Enabled = false;
// Collect the values by calling MccDaq.MccBoard.AInScan function
// Parameters:
// LowChan :the first channel of the scan
// HighChan :the last channel of the scan
// Count :the total number of A/D samples to collect
// Rate :sample rate
// Range :the range for the board
// MemHandle :Handle for Windows buffer to store data in
// Options :data collection options
//--------------------------------------------------------
LowChan = 0; // first channel to acquire
HighChan=3; // last channel to acquire
//--------------------------------------------------------
//HighChan = int.Parse(txtHighChan.Text); // last channel to acquire
//if ((HighChan > 7)) HighChan = 7;
//txtHighChan.Text = HighChan.ToString();
Count = NumPoints; // total number of data points to collect
//---------------------------------------------------------------
Rate = 16000; // per channel sampling rate ((samples per second) per channel)
//---------------------------------------------------------------
// return data as 12-bit values (ignored for 16-bit boards)
Options = MccDaq.ScanOptions.ConvertData;
Range = MccDaq.Range.Bip1Volts; // set the range
ULStat = DaqBoard.AInScan( LowChan, HighChan, Count, ref Rate, Range, MemHandle, Options);
if (ULStat.Value == MccDaq.ErrorInfo.ErrorCode.BadRange)
{
MessageBox.Show( "Change the Range argument to one supported by this board.", "Unsupported Range", 0);
}
// Transfer the data from the memory buffer set up by Windows to an array
ULStat = MccDaq.MccService.WinBufToArray( MemHandle, out ADData[0], FirstPoint, Count);
/*
for (i=0; i<=HighChan; ++i)
lblADData[i].Text = ADData[i].ToString("D");
for (j=HighChan + 1; j<=7; ++j)
lblADData[j].Text = "0";*/
//unpack the array into each channel
//for(i=0; i<=(NumPoints-ChanNum); i=i+ChanNum)
//{
//ADData_ch0[i]
//}
FileStream dataFile_ch0=new FileStream("C:/micArray/data/dataV_ch0.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw_ch0=new StreamWriter(dataFile_ch0);
FileStream dataFile_ch1=new FileStream("C:/micArray/data/dataV_ch1.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw_ch1=new StreamWriter(dataFile_ch1);
FileStream dataFile_ch2=new FileStream("C:/micArray/data/dataV_ch2.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw_ch2=new StreamWriter(dataFile_ch2);
FileStream dataFile_ch3=new FileStream("C:/micArray/data/dataV_ch3.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw_ch3=new StreamWriter(dataFile_ch3);
for(i=0; i<=(NumPoints-ChanNum); i=i+ChanNum)
{
//write data from ch0 to file dataV_ch0.txt
ULStat=DaqBoard.ToEngUnits( Range, ADData[i], out TemSamp);
sw_ch0.Write(TemSamp);
sw_ch0.Write(space);
//write data from ch1 to file dataV_ch1.txt
ULStat=DaqBoard.ToEngUnits( Range, ADData[i+1], out TemSamp);
sw_ch1.Write(TemSamp);
sw_ch1.Write(space);
//write data from ch2 to file dataV_ch2.txt
ULStat=DaqBoard.ToEngUnits( Range, ADData[i+2], out TemSamp);
sw_ch2.Write(TemSamp);
sw_ch2.Write(space);
//write data from ch2 to file dataV_ch3.txt
ULStat=DaqBoard.ToEngUnits( Range, ADData[i+3], out TemSamp);
sw_ch3.Write(TemSamp);
sw_ch3.Write(space);
}
sw_ch0.Close();
sw_ch1.Close();
sw_ch2.Close();
sw_ch3.Close();
cmdStart.Enabled = true;
}
private void cmdStopConvert_Click(object eventSender, System.EventArgs eventArgs)
{
MccDaq.ErrorInfo ULStat;
ULStat = MccDaq.MccService.WinBufFree(MemHandle); // Free up memory for use by
// other programs
MemHandle = 0;
Application.Exit();
}
private void frmDataDisplay_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?