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 numCounters = 5; // 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[5]; public Button[] FreqOutButton = new Button[5]; public Label[] FreqLabel = new Label[5]; public TextBox[] FreqDisplay = new TextBox[15]; // For data: byte[] Data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; public Form1() { InitializeComponent(); // Arrays of the controls: FreqOutTrack[0] = trackBarFreqA; FreqOutTrack[1] = trackBarFreqB; FreqOutTrack[2] = trackBarFreqC; FreqOutTrack[3] = trackBarFreqD; FreqOutTrack[4] = trackBarFreqE; FreqOutButton[0] = btnFreqA; FreqOutButton[1] = btnFreqB; FreqOutButton[2] = btnFreqC; FreqOutButton[3] = btnFreqD; FreqOutButton[4] = btnFreqE; FreqLabel[0] = lblFreqA; FreqLabel[1] = lblFreqB; FreqLabel[2] = lblFreqC; FreqLabel[3] = lblFreqD; FreqLabel[4] = lblFreqE; FreqDisplay[0] = textBox1; FreqDisplay[1] = textBox2; FreqDisplay[2] = textBox3; FreqDisplay[3] = textBox4; FreqDisplay[4] = textBox5; FreqDisplay[5] = textBox6; FreqDisplay[6] = textBox7; FreqDisplay[7] = textBox8; FreqDisplay[8] = textBox9; FreqDisplay[9] = textBox10; FreqDisplay[10] = textBox11; FreqDisplay[11] = textBox12; FreqDisplay[12] = textBox13; FreqDisplay[13] = textBox14; FreqDisplay[14] = textBox15; // 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 == 0x8020) ) { 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(); } // 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); } 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: ushort pReadValue; int k = 0; for (uint i = 0; i < numCounters; i++) { for (uint j = 0; j < 3; j++) { AIOUSB.CTR_8254Read(DeviceIndex, i,j, out pReadValue); string formatString; formatString = String.Format("{0,0:G}", pReadValue); FreqDisplay[k++].Text = formatString; } } // Test Code for read all and read Latched UInt16 arrays: UInt16[] pDataAll = new UInt16[16]; AIOUSB.CTR_8254ReadAll(DeviceIndex, pDataAll); UInt16[] pDataLatched = new UInt16[17]; byte[] pDataBytes = new byte[2]; byte pByteGateSpeed; // Read Latched: AIOUSB.CTR_8254ReadLatched(DeviceIndex, pDataLatched); // get last 2 bytes: pDataBytes = BitConverter.GetBytes(pDataLatched[16]); // Get correct byte pByteGateSpeed = pDataBytes[0]; } 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; iTagindex = Convert.ToUInt32(btnSender.Tag); UInt32 Status; ulong CounterValue; CounterValue = (ulong)(0xFFFF - FreqOutTrack[iTagindex].Value); Status = AIOUSB.CTR_8254ModeLoad(DeviceIndex, iTagindex, 1, 2, (ushort)CounterValue); Status = AIOUSB.CTR_8254ModeLoad(DeviceIndex, iTagindex, 2, 2, (ushort)CounterValue); // Restart timer thread: myTimer.Enabled = true; } private String FormatFreq(double Freq) { string formatString; if (Freq >= 1000000) formatString = String.Format("{0,0:F2}", Freq / 1000000) + "MHz"; else if (Freq >= 1000) formatString = String.Format("{0,0:F2}", Freq / 1000) + "KHz"; else if (Freq >= 1) formatString = String.Format("{0,0:F2}", Freq) + "Hz"; else formatString = String.Format("{0,0:F2}", Freq) + "Hz"; 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; iTagindex = Convert.ToUInt32(tbarSender.Tag); double CounterValue; String TempString; CounterValue = (0xFFFF - FreqOutTrack[iTagindex].Value); TempString = FormatFreq(3000000 / (CounterValue * CounterValue)); FreqLabel[iTagindex].Text = "Output " + 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(); } } }