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 = 96; public UInt32 numPorts = 12; // 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[96]; public Button[] toggleBtnArray = new Button[12]; // 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[12]; public Form1() { InitializeComponent(); int i = 0; // 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; // 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; checkStateArray[32] = checkBoxB1_33; checkStateArray[33] = checkBoxB1_34; checkStateArray[34] = checkBoxB1_35; checkStateArray[35] = checkBoxB1_36; checkStateArray[36] = checkBoxB1_37; checkStateArray[37] = checkBoxB1_38; checkStateArray[38] = checkBoxB1_39; checkStateArray[39] = checkBoxB1_40; checkStateArray[40] = checkBoxC1_41; checkStateArray[41] = checkBoxC1_42; checkStateArray[42] = checkBoxC1_43; checkStateArray[43] = checkBoxC1_44; checkStateArray[44] = checkBoxC1_45; checkStateArray[45] = checkBoxC1_46; checkStateArray[46] = checkBoxC1_47; checkStateArray[47] = checkBoxC1_48; checkStateArray[48] = checkBoxA2_49; checkStateArray[49] = checkBoxA2_50; checkStateArray[50] = checkBoxA2_51; checkStateArray[51] = checkBoxA2_52; checkStateArray[52] = checkBoxA2_53; checkStateArray[53] = checkBoxA2_54; checkStateArray[54] = checkBoxA2_55; checkStateArray[55] = checkBoxA2_56; checkStateArray[56] = checkBoxB2_57; checkStateArray[57] = checkBoxB2_58; checkStateArray[58] = checkBoxB2_59; checkStateArray[59] = checkBoxB2_60; checkStateArray[60] = checkBoxB2_61; checkStateArray[61] = checkBoxB2_62; checkStateArray[62] = checkBoxB2_63; checkStateArray[63] = checkBoxB2_64; checkStateArray[64] = checkBoxC2_65; checkStateArray[65] = checkBoxC2_66; checkStateArray[66] = checkBoxC2_67; checkStateArray[67] = checkBoxC2_68; checkStateArray[68] = checkBoxC2_69; checkStateArray[69] = checkBoxC2_70; checkStateArray[70] = checkBoxC2_71; checkStateArray[71] = checkBoxC2_72; checkStateArray[72] = checkBoxA3_73; checkStateArray[73] = checkBoxA3_74; checkStateArray[74] = checkBoxA3_75; checkStateArray[75] = checkBoxA3_76; checkStateArray[76] = checkBoxA3_77; checkStateArray[77] = checkBoxA3_78; checkStateArray[78] = checkBoxA3_79; checkStateArray[79] = checkBoxA3_80; checkStateArray[80] = checkBoxB3_81; checkStateArray[81] = checkBoxB3_82; checkStateArray[82] = checkBoxB3_83; checkStateArray[83] = checkBoxB3_84; checkStateArray[84] = checkBoxB3_85; checkStateArray[85] = checkBoxB3_86; checkStateArray[86] = checkBoxB3_87; checkStateArray[87] = checkBoxB3_88; checkStateArray[88] = checkBoxC3_89; checkStateArray[89] = checkBoxC3_90; checkStateArray[90] = checkBoxC3_91; checkStateArray[91] = checkBoxC3_92; checkStateArray[92] = checkBoxC3_93; checkStateArray[93] = checkBoxC3_94; checkStateArray[94] = checkBoxC3_95; checkStateArray[95] = checkBoxC3_96; // Initialize default values: DeviceIndex = AIOUSB.diFirst; // Default to first device found //outMask = 0xFFFFFFFF; // Power on state UInt32 Status; UInt64 SerNum; // Check device status: Status = AIOUSB.ClearDevices(); // Cleans up any orphaned indexes Status = AIOUSB.GetDevices(); Status = AIOUSB.GetDeviceSerialNumber(DeviceIndex, out SerNum); // For a 96 port board there will be Outmask 2 bytes and DataOut 12 bytes //Byte Tristate = 0; // 0 = false all bits not tristated //Byte outMaskByte = 0; // all bit to 0 mean all ports not output hence input for (i = 0; i < numPorts; i++) { Data[i] = 0xFF; bModeOut[i] = false; } Status = AIOUSB.DIO_Configure(DeviceIndex, bTristateAll, OutMask, Data); //Status = AIOUSB.DIO_Configure(DeviceIndex, Tristate, outMaskByte, DataOut); //!= ERROR_SUCCESS AIOUSB.DIO_ReadAll(DeviceIndex, Data); // we just set this validate } private void Form1_Load(object sender, EventArgs e) { // Called before Form is displayed Initialize resources: // Query Device Info: UInt32 Status; UInt32 PID = 0; UInt32 NameSize = 256; // must pass this in by reference it gets used as sizeof buffer in and then modified as sizeof buffer out string strName = "name"; // May need string builder for this 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 == 0x8003) ) { 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 >= 0x8018) && (PID <= 0x801E)) 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: 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 btnTristateAll_Click(object sender, EventArgs e) { // Need to stop the timer change board and GUI: myTimer.Stop(); int i = 0; Int32 iOutMask = 0x0000; Int16 isOutMask = 0x0000; //Get button sender Tag (index) Button btnSender = sender as Button; UInt32 iTagindex = Convert.ToUInt32(btnSender.Tag); // Toggle tristate var; if (bTristateAll != 0) // bool byte { bTristateAll = 0; btnSender.Text = "Enter Tristate Mode"; Tristatelabel.Text = "Not Tristate: I/O Buffers Enabled"; } else { bTristateAll = 1; btnSender.Text = " Exit Tristate Mode"; Tristatelabel.Text = "Tristate Mode: I/O Buffers Disabled"; } // Toggle board states: for (i = 0; i < numPorts; i++) { if (bModeOut[i] == true) // if output mode { // USB-DIO96 its 2 bytes one Int16 or short iOutMask = iOutMask | (1 << i); // Int32 isOutMask = (Int16)iOutMask; //safe downcast? } } AIOUSB.DIO_Configure(DeviceIndex, bTristateAll, ref isOutMask, Data); // Restart timer thread: myTimer.Enabled = true; } 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:"; } // Toggle board states: for (i = 0; i < numPorts; i++) { if (bModeOut[i] == true) { // USB-DIO96 its 12 bits in one Int16 or short iOutMask = iOutMask | (1 << i); // Int32 isOutMask = (Int16)iOutMask; //safe downcast } } i = (int)iTagindex; //Data[i] = 0xFF; //Set new output byte high, in case user made a mistake 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; 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; // downcast 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(); } } }