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 = 16; public UInt32 numPorts = 2; // 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[16]; public Button[] toggleBtnArray = new Button[2]; // For bitmasks: byte[] OutMask = new byte[] { 0, 0}; byte[] Data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // State flags: byte bTristateAll = 0; // 0 = false all bits not tristated public bool[] bModeOut = new bool[2]; public Form1() { InitializeComponent(); // Array of the toggle mode buttons: toggleBtnArray[0] = btnToggle0; toggleBtnArray[1] = btnToggle1; // 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; } 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 >= 0x8040 && PID <= 0x815D) // AI and AIO { 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 >= 0x8040 && 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 values: for (int i = 0; i < numPorts; i++) { Data[i] = 0xFF; bModeOut[i] = false; // Default power up not Output hence Input Mode } Status = AIOUSB.DIO_Configure(DeviceIndex, bTristateAll, OutMask, Data); // 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, Data); // Test each bit and set check state: for (int i = 0; i < numPins; i++) { if( (Data[i/8] & ( 1 << (i % 8) )) != 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; Int16 isOutMask = 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 (bModeOut[iTagindex]) { bModeOut[iTagindex] = false; btnSender.Text = "Toggle to Output Mode"; //label2.Text = "Output State:"; } else { bModeOut[iTagindex] = true; btnSender.Text = "Toggle to Input Mode"; //label2.Text = "Input State:"; } // Recreate new Outmask for Configure: for (i = 0; i < numPorts; i++) { if (bModeOut[i] == true) // if output mode { // USB-DIO96 its 12 bits in one Int16 or short 48 is 6 ports iOutMask = iOutMask | (1 << i); // Int32 isOutMask = (Int16)iOutMask; //safe downcast } } i = (int)iTagindex; AIOUSB.DIO_Configure(DeviceIndex, bTristateAll, ref isOutMask, 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 = false; 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 iOutMask = 0x0000; byte bByte = 1; i = (Int32)iTagindex; // cast ok iOutMask = ((Data[i / 8]) & (1 << (i % 8))); // get single bit if (iOutMask != 0) bByte = 0; AIOUSB.DIO_Write1(DeviceIndex, (UInt32)i, bByte); // byte 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(); } } }