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 = 2; // For DAC data: ushort[] Data = new ushort[] { 0, 0, 0, 0 }; // 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[] DACOutTrack= new TrackBar[2]; public Button[] DACOutButton = new Button[2]; public Label[] DACLabel = new Label[2]; public TextBox[] DACDisplay = new TextBox[15]; public Form1() { InitializeComponent(); // Arrays of the controls: DACOutTrack[0] = trackBar0; DACOutTrack[1] = trackBar1; DACOutButton[0] = btnCount0; DACOutButton[1] = btnCount1; DACLabel[0] = lblCount0; DACLabel[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); if (Status == ERROR_SUCCESS && PID >= 0x8140 && PID <= 0x815D) // AIO and not AI { 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 analog outputs: for (uint count = 0; count < numAnalogOutputs; count++) { AIOUSB.DACDirect(DeviceIndex, (ushort)count, 0); } // 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 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: // We arent doing any loop for this sample: } private void btnCount_Click(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); //Get button sender Tag (index) Button btnSender = sender as Button; UInt32 iTagindex = Convert.ToUInt32(btnSender.Tag); UInt32 Status; ushort CounterValue; CounterValue = (ushort)(DACOutTrack[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,4:X}", Count); return (formatString); } private void trackBarCount_Scroll(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); //Get button sender Tag (index) TrackBar tbarSender = sender as TrackBar; UInt32 iTagindex = Convert.ToUInt32(tbarSender.Tag); int CounterValue; String TempString; CounterValue = DACOutTrack[iTagindex].Value; TempString = FormatCount(CounterValue); DACLabel[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) { UInt32 Status = 0; ushort count; for (count = 0; count < numAnalogOutputs; count++) { Data[count * 2] = count; Data[count * 2 + 1] = (ushort)(DACOutTrack[count].Value); } Status = AIOUSB.DACMultiDirect(DeviceIndex, Data, numAnalogOutputs); if (Status != 0 ) MessageBox.Show("Error Updating all DACs!!!"); } } }