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 numPins = 32; public UInt32 numPorts = 32; public UInt32 numCounters = 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 CheckBox[] checkStateArray = new CheckBox[32]; public Button[] toggleBtnArray = new Button[32]; // For bitmasks: UInt32 OutMask = 0; // 32 bits // all bits to 0 means all ports not output hence input UInt32 Data = 0; // 32 bits // State flags: //byte bTristateAll = 0; // 0 = false all bits not tristated public bool[] bModeInOut = new bool[32]; public Form1() { InitializeComponent(); // Array of the toggle mode buttons: toggleBtnArray[0] = btnToggle0; toggleBtnArray[1] = btnToggle1; toggleBtnArray[2] = btnToggle2; toggleBtnArray[3] = btnToggle3; toggleBtnArray[4] = btnToggle4; toggleBtnArray[5] = btnToggle5; toggleBtnArray[6] = btnToggle6; toggleBtnArray[7] = btnToggle7; toggleBtnArray[8] = btnToggle8; toggleBtnArray[9] = btnToggle9; toggleBtnArray[10] = btnToggle10; toggleBtnArray[11] = btnToggle11; toggleBtnArray[12] = btnToggle12; toggleBtnArray[13] = btnToggle13; toggleBtnArray[14] = btnToggle14; toggleBtnArray[15] = btnToggle15; toggleBtnArray[16] = btnToggle16; toggleBtnArray[17] = btnToggle17; toggleBtnArray[18] = btnToggle18; toggleBtnArray[19] = btnToggle19; toggleBtnArray[20] = btnToggle20; toggleBtnArray[21] = btnToggle21; toggleBtnArray[22] = btnToggle22; toggleBtnArray[23] = btnToggle23; toggleBtnArray[24] = btnToggle24; toggleBtnArray[25] = btnToggle25; toggleBtnArray[26] = btnToggle26; toggleBtnArray[27] = btnToggle27; toggleBtnArray[28] = btnToggle28; toggleBtnArray[29] = btnToggle29; toggleBtnArray[30] = btnToggle30; toggleBtnArray[31] = btnToggle31; // Array of the LED style checkboxes: checkStateArray[0] = checkBoxA0_1; checkStateArray[1] = checkBoxA0_2; checkStateArray[2] = checkBoxA0_3; checkStateArray[3] = checkBoxA0_4; checkStateArray[4] = checkBoxA0_5; checkStateArray[5] = checkBoxA0_6; checkStateArray[6] = checkBoxA0_7; checkStateArray[7] = checkBoxA0_8; checkStateArray[8] = checkBoxB0_9; checkStateArray[9] = checkBoxB0_10; checkStateArray[10] = checkBoxB0_11; checkStateArray[11] = checkBoxB0_12; checkStateArray[12] = checkBoxB0_13; checkStateArray[13] = checkBoxB0_14; checkStateArray[14] = checkBoxB0_15; checkStateArray[15] = checkBoxB0_16; checkStateArray[16] = checkBoxC0_17; checkStateArray[17] = checkBoxC0_18; checkStateArray[18] = checkBoxC0_19; checkStateArray[19] = checkBoxC0_20; checkStateArray[20] = checkBoxC0_21; checkStateArray[21] = checkBoxC0_22; checkStateArray[22] = checkBoxC0_23; checkStateArray[23] = checkBoxC0_24; checkStateArray[24] = checkBoxA1_25; checkStateArray[25] = checkBoxA1_26; checkStateArray[26] = checkBoxA1_27; checkStateArray[27] = checkBoxA1_28; checkStateArray[28] = checkBoxA1_29; checkStateArray[29] = checkBoxA1_30; checkStateArray[30] = checkBoxA1_31; checkStateArray[31] = checkBoxA1_32; } 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 == 0x8004) ) { 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(); } for (int i = 0; i < numPorts; i++) { bModeInOut[i] = false; } // 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); // Config device: OutMask = 0x00000000; // all bit to 0 mean all ports not output hence input Data = 0xFFFFFFFF; byte tristate = 0; // always false for USB-DIO-32I Status = AIOUSB.DIO_Configure(DeviceIndex, tristate, ref OutMask, ref Data); Status = AIOUSB.DIO_ReadAll(DeviceIndex, out Data); // we just set this validate // 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() { // Get data from board update display states: AIOUSB.DIO_ReadAll(DeviceIndex, out Data); // Test each bit and set check state: for (int i = 0; i < numPins; i++) { if( (Data & ( 1 << (i) )) != 0 ) // looks complicated but efficient checkStateArray[i].Checked = true; // High Level else checkStateArray[i].Checked = false; // Low Level } } private void btnToggle_Click(object sender, EventArgs e) { // Button Clicks to toggle the input output mode and display states: // Need to stop the timer change board and GUI: myTimer.Stop(); Int32 i; Int32 iOutMask = 0x0000; //Get button sender Tag (index) UInt32 iTagindex = 0; Button btnSender = sender as Button; if (btnSender != null) { iTagindex = Convert.ToUInt32(btnSender.Tag); } // Toggle local mode value and button display states: if (bModeInOut[iTagindex]) { bModeInOut[iTagindex] = false; btnSender.Text = "I"; } else { bModeInOut[iTagindex] = true; btnSender.Text = "O"; } // Recreate new Outmask for Configure: for (i = 0; i < numPorts; i++) { if (bModeInOut[i] == true) // if output mode { iOutMask = iOutMask | (1 << i); // Int32 OutMask = (UInt32)iOutMask; } } byte tristate = 0; AIOUSB.DIO_Configure(DeviceIndex, tristate, ref OutMask, ref Data); // Restart timer thread: myTimer.Enabled = true; } private void checkBoxPin_CheckedChanged(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); bool bHighLow; UInt32 iTagindex = 0; // get correct checkbox get the checked state //Get sender Tag (index) and if check CheckBox chkSender = sender as CheckBox; if (chkSender != null) { iTagindex = Convert.ToUInt32(chkSender.Tag); } bHighLow = chkSender.Checked; Int32 i; Int32 iStateMask = 0x0000; byte bByte = 1; i = (Int32)iTagindex; // downcast iStateMask = ( ((int)Data) & (1 << (i)) ); // get single bit from index if (iStateMask != 0) bByte = 0; AIOUSB.DIO_Write1(DeviceIndex, (UInt32)i, bByte); //byte is used like a bool // restart timer and Gui will update: myTimer.Enabled = true; } private void btnDevice_Click(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(); } } }