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 namespace Sample1 { public partial class Form1 : Form { // One and only Device Index: public UInt32 DeviceIndex; // 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]; public ProgressBar[] CountProgress = new ProgressBar[numAnalogOutputs]; public Label[] CountLabel = new Label[numAnalogOutputs]; // Num channels we will be using: public const int numAnalogOutputs = 2; public Form1() { InitializeComponent(); // Arrays of the controls: VoltProgress[0] = progressBar1; VoltProgress[1] = progressBar2; VoltLabel[0] = lblVolt0; VoltLabel[1] = lblVolt1; CountProgress[0] = progressBar3; CountProgress[1] = progressBar4; CountLabel[0] = lblCount0; CountLabel[1] = lblCount1; } 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); //-------------------------------------- // device was detected // verify PID - USB-AO16-xxx - 0x807x // USB-AO16-16 Rev A - 0x8060 //-------------------------------------- if ( (Status == 0) && ((PID & 0xFFF0) == 0x8070) ) { 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 >= 0x8140 && 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); // Init for this board: //Unsleep the DAC/ADC reference. Status = AIOUSB.DACSetBoardRange(DeviceIndex, 2); // 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 = 100; //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 btnExecute_Click(object sender, EventArgs e) { btnExecute.Enabled = false; btnStop.Enabled = true; // Restart timer thread: myTimer.Enabled = true; } private void btnStop_Click(object sender, EventArgs e) { // Stop the timer thread: myTimer.Stop(); btnExecute.Enabled = true; btnStop.Enabled = false; } 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: UInt32 Status; double[] readVal = new double[2]; ushort[] readCount = new ushort[2]; Status = AIOUSB.ADC_GetScanV( DeviceIndex, readVal ); // Voltage Status = AIOUSB.ADC_GetScan(DeviceIndex, readCount); // Voltage int ERROR_SUCCESS = 0; if (Status != ERROR_SUCCESS) { MessageBox.Show("GetScanV() Error!!!"); } else { for (int count = 0; count < 2; ++count) { VoltProgress[count].Value = (int)readVal[count]; string formatString = "Ch {0}: {1,8:F4} V"; string LabelString = String.Format(formatString, count, readVal[count]); VoltLabel[count].Text = LabelString; CountProgress[count].Value = readCount[count]; string formatString2 = "Ch {0}: {1,8:G}"; LabelString = String.Format(formatString2, count, readCount[count]); CountLabel[count].Text = LabelString; } } } } }