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 System.Globalization; //for NumberStyles format specifiers using AIOUSBNet; // namespace exposes AIOUSB Class interface // you must also add a reference to the AIOUSBNet.dll in the project // The path should be C:\\Windows\\system32\\AIOUSBNet.dll or the equivelant for your system namespace Sample1 { public partial class FormSample1 : Form { // Array of controls for GUI updates: public CheckBox[] checkOutArray = new CheckBox[16]; public CheckBox[] checkInArray = new CheckBox[16]; // One and only Device Index: public UInt32 DeviceIndex; // For bitmasks: public UInt32 outMask; public UInt32 inMask; // Interval Timer for GUI update: // (Note: A Windows Forms timer designed for single threaded environments not a System Timer) static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); public FormSample1() { InitializeComponent(); // Additional Constructor Initializations here: // Initialize default values: DeviceIndex = AIOUSB.diFirst; // Default to first device found outMask = 0xFFFFFFFF; // Power on relay state inMask = 0; // Array of the LED style checkboxes: checkOutArray[0] = checkOutA0; checkOutArray[1] = checkOutA1; checkOutArray[2] = checkOutA2; checkOutArray[3] = checkOutA3; checkOutArray[4] = checkOutA4; checkOutArray[5] = checkOutA5; checkOutArray[6] = checkOutA6; checkOutArray[7] = checkOutA7; checkOutArray[8] = checkOutB8; checkOutArray[9] = checkOutB9; checkOutArray[10] = checkOutB10; checkOutArray[11] = checkOutB11; checkOutArray[12] = checkOutB12; checkOutArray[13] = checkOutB13; checkOutArray[14] = checkOutB14; checkOutArray[15] = checkOutB15; checkInArray[0] = checkInA0; checkInArray[1] = checkInA1; checkInArray[2] = checkInA2; checkInArray[3] = checkInA3; checkInArray[4] = checkInA4; checkInArray[5] = checkInA5; checkInArray[6] = checkInA6; checkInArray[7] = checkInA7; checkInArray[8] = checkInB8; checkInArray[9] = checkInB9; checkInArray[10] = checkInB10; checkInArray[11] = checkInB11; checkInArray[12] = checkInB12; checkInArray[13] = checkInB13; checkInArray[14] = checkInB14; checkInArray[15] = checkInB15; } private void FormSample1_Load(object sender, EventArgs e) { // Initial form settings here before the form is displayed: UInt32 Status; // Query Device Info: 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"; UInt32 DIOBytes = 0; UInt32 Counters = 0; UInt64 SerNum = 0; 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 >= 0x8010 && PID <= 0x8016) { 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 >= 0x8010 && PID <= 0x8016) deviceIndexValid = true; } if (!deviceIndexValid) { // No valid device found should exit: // this.Close(); } string strDisplay; string formatString; formatString = " Index: {0,8:X8}" + " PID: {1,8:X8}" + " Name: {2}"; // Ping driver and device and Test for errors: // Check device status: Status = AIOUSB.ClearDevices(); // Cleans up any orphaned indexes Status = AIOUSB.GetDevices(); Status = AIOUSB.GetDeviceSerialNumber(DeviceIndex, out SerNum); // Get The Device Information: // Using a Sting class for Name array: Status = AIOUSB.QueryDeviceInfo(DeviceIndex, out PID, out NameSize, out strName, out DIOBytes, out Counters); // Display device Status: if (Status == 0) // == ERROR_SUCCESS { txtStatus.Text = " Device Connected !"; if (PID == 0x8010) { strDisplay = String.Format(formatString, DeviceIndex, PID, strName); txtStatus.Text += strDisplay; } } else { txtStatus.Text = " Not Connected !"; } // Default power on output relay state for board is // 0xFFFFFFFF bits set to One // Force Initial default relay state and update GUI state: // (Inputs are not effected but the bits are sent) outMask = 0xFFFFFFFF; AIOUSB.DIO_WriteAll(DeviceIndex, ref outMask); for (int i = 0; i < 16; i++) checkOutArray[i].Checked = true; // 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 btnExit_Click(object sender, EventArgs e) { // Right now this is only called on button click not window close ALtF4 etc: // Clean up and Close App: UInt32 Status; Status = AIOUSB.ClearDevices(); // Kill the timer: myTimer.Stop(); myTimer.Enabled = false; myTimer.Dispose(); // Close the form and Application: this.Close(); } 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(); } private void btnWritePortAll_Click(object sender, EventArgs e) { UInt32 bits = 0; UInt32 result; // Test overloaded WriteAll with an array of bytes: // bits are 7-0 right to left byte inputA = 160; byte inputB = 255; byte[] sampleDataIn = new byte[4] {inputA,inputB,0,0}; result = AIOUSB.DIO_WriteAll(DeviceIndex, sampleDataIn); try { bits = UInt32.Parse(txtWriteAll.Text, NumberStyles.HexNumber); } catch (FormatException) { txtStatus.Text = "Format: Enter an integer or valid hex values 0-9, A-F and a-f, no 0x prefix!"; return; } catch (OverflowException) { txtStatus.Text = "Overflow: Invalid entry!"; return; } result = AIOUSB.DIO_WriteAll(DeviceIndex, ref bits); } private void btnReadPortAll_Click(object sender, EventArgs e) { // The 32 bit Return value from ReadAll is 4 8 bit Bytes // bits in reverse order 7-0 // Byte Index is 3,2,1,0 In B, In A, Out B, Out A UInt32 bits = 0; UInt32 result; // Test overloaded ReadAll with an array of bytes: // bits are 7-0 right to left byte inputA = 0; byte inputB = 0; byte outputA = 0; byte outputB = 0; byte[] DataOut = new byte[] { 0, 0, 0, 0 }; result = AIOUSB.DIO_ReadAll(DeviceIndex, DataOut); outputA = DataOut[0]; outputB = DataOut[1]; inputA = DataOut[2]; inputB = DataOut[3]; result = AIOUSB.DIO_ReadAll(DeviceIndex, out bits); // Display Hex format: txtReadAllHex.Text = bits.ToString("X8"); // Display Binary format: string myString; char pad = '0'; myString = Convert.ToString(bits, 2); txtReadAllBin.Text = myString.PadLeft(32, pad); } private void btnOutRelay_Click(object sender, EventArgs e) { // Button Clicks to toggle the relay and hence display states: UInt32 result; byte stateByte = 1; UInt32 bits = 0; //Get sender Tag (index) uint iTagindex = 0; Button btnSender = sender as Button; if (btnSender != null) { iTagindex = Convert.ToUInt32(btnSender.Tag); } // Read All then test correct bit and set individual relay state: result = AIOUSB.DIO_ReadAll(DeviceIndex, out bits); if ( ((bits >> ((int)iTagindex)) % 2) != 0) stateByte = 0; else stateByte = 1; result = AIOUSB.DIO_Write1(DeviceIndex, iTagindex, stateByte); } private void btnReadPortA_Click_1(object sender, EventArgs e) { UInt32 result; UInt32 ByteIndex = 2; byte stateByte = 0; result = AIOUSB.DIO_Read8(DeviceIndex, ByteIndex, out stateByte); // Cycle through the A inputs (and outputs) and set the display to the right state: for (int i = 0; i < 8; i++) { if ( (stateByte >> i) % 2 != 0) checkInArray[i].Checked = true; else checkInArray[i].Checked = false; } txtReadA.Text = stateByte.ToString("X2"); } private void btnReadPortB_Click(object sender, EventArgs e) { UInt32 result; UInt32 ByteIndex = 3; byte stateByte = 0; // Cycle through the B inputs and set the display to the right state: result = AIOUSB.DIO_Read8(DeviceIndex, ByteIndex, out stateByte); for (int i = 0; i < 8; i++) { if ((stateByte >> i) % 2 != 0) checkInArray[i+8].Checked = true; else checkInArray[i+8].Checked = false; } txtReadB.Text = stateByte.ToString("X2"); } private void btnWritePortA_Click(object sender, EventArgs e) { // Use Write 8 to write one port: byte myByte = 0; UInt32 result; uint ByteIndex = 0; try { myByte = byte.Parse(txtWriteA.Text, NumberStyles.HexNumber); } catch (FormatException) { txtStatus.Text = "Format: Enter an integer or valid hex values 0-9, A-F and a-f, no 0x prefix!"; return; } catch (OverflowException) { txtStatus.Text = "Overflow: Invalid entry!"; return; } result = AIOUSB.DIO_Write8(DeviceIndex, ByteIndex, myByte); } private void btnWritePortB_Click(object sender, EventArgs e) { // Use Write 8 to write one port: byte myByte = 0; UInt32 result; uint ByteIndex = 1; try { myByte = byte.Parse(txtWriteB.Text, NumberStyles.HexNumber); } catch (FormatException) { txtStatus.Text = "Format: Enter an integer or valid hex values 0-9, A-F and a-f, no 0x prefix!"; return; } catch (OverflowException) { txtStatus.Text = "Overflow: Invalid entry!"; return; } result = AIOUSB.DIO_Write8(DeviceIndex, ByteIndex, myByte); } private void UpdateGUIState() { UInt32 result = 0; UInt32 ByteIndex2 = 2; UInt32 ByteIndex3 = 3; byte inputByte2 = 0; byte inputByte3 = 0; int i; UInt32 allBytes = 0; // Get all the data: result = AIOUSB.DIO_Read8(DeviceIndex, ByteIndex2, out inputByte2); result = AIOUSB.DIO_Read8(DeviceIndex, ByteIndex3, out inputByte3); result = AIOUSB.DIO_ReadAll(DeviceIndex, out allBytes); // Cycle through the inputs and outputs and set the contols to the right state: for (i = 0; i < 16; i++) { if ((allBytes >> i+16) % 2 != 0) checkInArray[i].Checked = true; else checkInArray[i].Checked = false; if ((allBytes >> (i)) % 2 != 0) checkOutArray[i].Checked = true; else checkOutArray[i].Checked = false; } // Update Txt Input Hex: txtReadA.Text = inputByte2.ToString("X2"); txtReadB.Text = inputByte3.ToString("X2"); // Update Txt All Hex: txtReadAllHex.Text = allBytes.ToString("X8"); // Update Txt All Binary: string myString; char pad = '0'; myString = Convert.ToString(allBytes, 2); txtReadAllBin.Text = myString.PadLeft(32, pad); } 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; } } }