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; public UInt32 numAnalogOutputs = 8; // 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 TrackBar[] FreqOutTrack= new TrackBar[8]; public Button[] FreqOutButton = new Button[8]; public Label[] FreqLabel = new Label[8]; public TextBox[] FreqDisplay = new TextBox[15]; // For data: ushort[] Data = new ushort[] { 0, 0, 0, 0, 0, 0, 0, 0 }; public Form1() { InitializeComponent(); // Arrays of the controls: FreqOutTrack[0] = trackBar0; FreqOutTrack[1] = trackBar1; FreqOutTrack[2] = trackBar2; FreqOutTrack[3] = trackBar3; FreqOutTrack[4] = trackBar4; FreqOutTrack[5] = trackBar5; FreqOutTrack[6] = trackBar6; FreqOutTrack[7] = trackBar7; FreqOutButton[0] = btnCount0; FreqOutButton[1] = btnCount1; FreqOutButton[2] = btnCount2; FreqOutButton[3] = btnCount3; FreqOutButton[4] = btnCount4; FreqOutButton[5] = btnCount5; FreqOutButton[6] = btnCount6; FreqOutButton[7] = btnCount7; FreqLabel[0] = lblCount0; FreqLabel[1] = lblCount1; FreqLabel[2] = lblCount2; FreqLabel[3] = lblCount3; FreqLabel[4] = lblCount4; FreqLabel[5] = lblCount5; FreqLabel[6] = lblCount6; FreqLabel[7] = lblCount7; // Initialize default values: DeviceIndex = AIOUSB.diFirst; // Default to first device found UInt32 Status; UInt64 SerNum; // Check device status: Status = AIOUSB.ClearDevices(); // Cleans up any orphaned indexes Status = AIOUSB.GetDevices(); Status = AIOUSB.GetDeviceSerialNumber(DeviceIndex, out SerNum); } 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 >= 0x8060 && PID <= 0x807F) { 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 >= 0x4002) && (PID <= 0x4003) ) deviceIndexValid = true; } if (!deviceIndexValid) { // No valid device found should exit // this.Close(); } // 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(); // Check device status: Status = AIOUSB.ClearDevices(); // Cleans up any orphaned indexes Status = AIOUSB.GetDevices(); Status = AIOUSB.GetDeviceSerialNumber(DeviceIndex, out SerNum); // Get The Device Information: Status = AIOUSB.QueryDeviceInfo(DeviceIndex, out PID, out NameSize, out strName, out DIOBytes, out Counters); // Init analog outputs: ushort value = 0; for (uint count = 0; count < numAnalogOutputs; count++) { AIOUSB.DACDirect(DeviceIndex, (ushort)count, value); } } 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() { // Get data from board update display states: // Not actually doing anything on the timer for this sample: } private void btnFreqX_Click(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); //Get button sender Tag (index) UInt32 iTagindex = 0; Button btnSender = sender as Button; if (btnSender != null) { iTagindex = Convert.ToUInt32(btnSender.Tag); } UInt32 Status; ushort CounterValue; CounterValue = (ushort)(FreqOutTrack[iTagindex].Value); Status = AIOUSB.DACDirect(DeviceIndex, (ushort)iTagindex, CounterValue); // Restart timer thread: myTimer.Enabled = true; } private String FormatCount(int Count) { string formatString; formatString = String.Format("{0,3:X}", Count); return (formatString); } private void trackBarFreqX_Scroll(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); //Get button sender Tag (index) UInt32 iTagindex = 0; TrackBar tbarSender = sender as TrackBar; if (tbarSender != null) { iTagindex = Convert.ToUInt32(tbarSender.Tag); } int CounterValue; String TempString; CounterValue = FreqOutTrack[iTagindex].Value; TempString = FormatCount(CounterValue); FreqLabel[iTagindex].Text = "DAC Counts: " + TempString; // Restart timer thread: myTimer.Enabled = true; } 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 btnUpdateAll_Click(object sender, EventArgs e) { uint count; for (count = 0; count < numAnalogOutputs; count++) { Data[count] = (ushort)(FreqOutTrack[count].Value); } AIOUSB.DACMultiDirect(DeviceIndex, Data, numAnalogOutputs); } } }