using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using AIOUSBNet; // the namespace exposes the AIOUSB Class interface // You must also add a reference to the AIOUSBnet.dll in the project settings // Usually intalled in C:\Windows\system32 namespace Sample1 { public partial class Form1 : Form { // One and only Device Index: public UInt32 DeviceIndex; // AD specifics: bool bCal = true; const int Channels = 16; // Interval Timer fou GUI update: // (Note: A Windows Forms timer designed for single threaded environments not a System Timer 55 ms min res) static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); // Array of controls for GUI updates: public ProgressBar[] VoltProgress = new ProgressBar[numAnalogOutputs]; public Label[] VoltLabel = new Label[numAnalogOutputs]; // Struct for AI Board Config: public struct TUSBAI1616Config { public TUSBAI1616Config(int size) { ChannelRange = new byte[size]; CalibMode = 0; TrigMode = 0; StartStopCH = 0; Oversample = 0; AIMUXChEx = 0; } public byte[] ChannelRange; public byte CalibMode; public byte TrigMode; public byte StartStopCH; public byte Oversample; public byte AIMUXChEx; //AIMUX start and end address bit extension register. } // Num channels we will be using: public const int numAnalogOutputs = 16; // Struct for Configuration must pass number of channels for array size: public TUSBAI1616Config Config = new TUSBAI1616Config(numAnalogOutputs); // Required for ADC_SetConfig() dll calls: UInt32 ConfigBufSize = numAnalogOutputs + 5; // sizeof(Config); //$$$ could be better // Temp array for ADC_SetConfig() dll calls: public byte[] configArray = new byte[21]; // Array for AD Data the counts are unsigned 16 bit numbers (ushort) //public UInt16[] ADData = new UInt16[16 * 32 * 1024L]; //32K scans of 16 channels, or 64K scans of 8 differential channels. public UInt32 BufA; public UInt32 BufB; public bool bGoing; public UInt32 KnownBytesLeft; public int NextSample, NextChannel; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Called before Form is displayed Initialize resources: // Initialize default Device Only: DeviceIndex = AIOUSB.diOnly; // Device data: UInt32 Status; UInt32 PID = 0; UInt32 NameSize = 256; string strName = "name"; UInt32 DIOBytes = 0; UInt32 Counters = 0; UInt64 SerNum; UInt32 ERROR_SUCCESS = 0; bool deviceIndexValid = false; // Get The Device Information test for valid device found: Status = AIOUSB.QueryDeviceInfo(DeviceIndex, out PID, out NameSize, out strName, out DIOBytes, out Counters); if ((Status == ERROR_SUCCESS) && ((PID >= 0x8040) && (PID <= 0x815D))) // AI and AIO { deviceIndexValid = true; } else { // If Only device is not valid then Launch connect device dialog box: // New parent aware subform: FormDetect DetectSubForm = new FormDetect(this); DetectSubForm.ShowDialog(); Status = AIOUSB.QueryDeviceInfo(DeviceIndex, out PID, out NameSize, out strName, out DIOBytes, out Counters); if (Status == ERROR_SUCCESS && PID >= 0x8040 && PID <= 0x815D) deviceIndexValid = true; } if (!deviceIndexValid) { // No valid device found should exit // this.Close(); } // Check device status: Status = AIOUSB.ClearDevices(); // Cleans up any orphaned indexes Status = AIOUSB.GetDevices(); Status = AIOUSB.GetDeviceSerialNumber(DeviceIndex, out SerNum); if (AIOUSB.ADC_QueryCal(DeviceIndex) != ERROR_SUCCESS) { //this board doesn't have calibration bCal = false; lblCal.Visible = false; comboBoxCalibration.Visible = false; } //Stop the counter, in case it was running. double Hz = 0; UInt32 BlockIndex = 0; AIOUSB.CTR_StartOutputFreq(DeviceIndex, BlockIndex, out Hz); comboBoxRange.SelectedIndex = 0; // Init config values: Config.CalibMode = 0; //Take actual data, not internal calibration sources. Config.TrigMode = 5; //Scan selected channels each counter rising edge. Config.StartStopCH = 0; //Scan selected channels each counter rising edge. Config.Oversample = 14;//+14 oversample. //ConfigBufSize = 21; // Note This is set once above for (int i = 0; i < numAnalogOutputs; i++) { Config.ChannelRange[i] = 0; } // Temp copy struct to array for dll call: for (int i = 0; i < numAnalogOutputs; i++) { configArray[i] = Config.ChannelRange[i]; } configArray[16] = Config.CalibMode; configArray[17] = Config.TrigMode; configArray[18] = Config.StartStopCH; configArray[19] = Config.Oversample; Status = AIOUSB.ADC_SetConfig(DeviceIndex, configArray, out ConfigBufSize); NextChannel = 0; comboBoxCalibration.SelectedItem = ":AUTO:"; // Add the event and the event handler for the method that will // process the timer event to the timer: myTimer.Tick += new EventHandler(TimerEventProcessor); // Set the timer interval in miliseconds and start 55ms min resolution: myTimer.Interval = 1000; //myTimer.Start(); } private String FormatVolt(int Count) { string formatString; formatString = String.Format("{0,3:X}", Count); return (formatString); } private void btnDevice_Click_1(object sender, EventArgs e) { // Launch connect device dialog box // Switch between multiple devices or reconnect // New parent aware subform: FormDetect DetectSubForm = new FormDetect(this); DetectSubForm.ShowDialog(); } private void comboBoxRange_SelectedIndexChanged(object sender, EventArgs e) { int i; byte RangeCode; //The ranges in the combo box are listed in order, so that the index is equal //to the range code. RangeCode = (byte)comboBoxRange.SelectedIndex; if (checkBoxDIFF.Checked) RangeCode |= 0x08; for (i = 0; i < numAnalogOutputs; i++) Config.ChannelRange[i] = RangeCode; } private void checkBoxDIFF_CheckedChanged(object sender, EventArgs e) { comboBoxRange_SelectedIndexChanged(sender, e); } private void btnExecute_Click(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); UInt32 Status; byte StartChannel, EndChannel; StartChannel = 0; if (checkBoxDIFF.Checked) EndChannel = Channels / 2 - 1; else EndChannel = Channels - 1; // Could clean this up its tough to do bit operations on bytes in C# Config.StartStopCH = (byte) ( ((int)EndChannel << 4) | ((int)StartChannel & 0xF) ); Config.AIMUXChEx = (byte)( (EndChannel & 0xF0) | (StartChannel >> 4) ); // Temp copy struct to array for dll call: for (int i = 0; i < numAnalogOutputs; i++) { configArray[i] = Config.ChannelRange[i]; } configArray[16] = Config.CalibMode; configArray[17] = Config.TrigMode; configArray[18] = Config.StartStopCH; configArray[19] = Config.Oversample; configArray[20] = Config.AIMUXChEx; // ConfigBufSize = sizeof(Config); Status = AIOUSB.ADC_SetConfig(DeviceIndex, configArray, out ConfigBufSize); if ( bCal ) { Status = AIOUSB.ADC_SetCal(DeviceIndex, comboBoxCalibration.SelectedItem.ToString()); if ( Status != 0) //ERROR_SUCCESS ) { myTimer.Stop(); MessageBox.Show("Calibration Error!"); return; } } comboBoxCalibration.Enabled = false; comboBoxRange.Enabled = false; btnExecute.Enabled = false; btnStop.Enabled = true; checkBoxDIFF.Enabled = false; // Restart timer thread: myTimer.Enabled = true; } private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { // This is the method that runs when the timer event is raised: myTimer.Stop(); UpdateGUIState(); myTimer.Enabled = true; } private void UpdateGUIState() { // Read data and set GUI state: ulong Status; int ChannelsUsed; double[] Volts = new double[Channels]; Status = AIOUSB.ADC_GetScanV(DeviceIndex, Volts); if ( Status != 0)//ERROR_SUCCESS ) { myTimer.Stop(); MessageBox.Show("ADC_GetScanV Error!"); } else { if (checkBoxDIFF.Checked) ChannelsUsed = Channels / 2; else ChannelsUsed = Channels; listBoxVoltage.Items.Clear(); //ResetContent(); for ( int I = 0; I < ChannelsUsed; ++I ) { // Format and display: string strChVolt; string formatString = "Ch {0,2:D2}: {1,8:F4}"; strChVolt = String.Format(formatString, I, Volts[I]); listBoxVoltage.Items.Add(strChVolt); //AddString(ChannelData); } } } private void btnStop_Click(object sender, EventArgs e) { myTimer.Stop(); comboBoxCalibration.Enabled = true; comboBoxRange.Enabled = true; btnExecute.Enabled = true; btnStop.Enabled = false; checkBoxDIFF.Enabled = true; //myTimer.Enabled = true; } } }