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.IO; // for file IO 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 = 18; public UInt32 numPorts = 4; public UInt32 numCounters = 1; // 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]; // oversized public Button[] toggleBtnArray = new Button[4 + 1]; // plus fast public Button[] TristateButton = new Button[2]; public Label[] Tristatelabel = new Label[2]; public TrackBar[] CountOutTrack = new TrackBar[1]; public Button[] CountOutButton = new Button[1]; public Label[] CountLabel = new Label[1]; // For bitmasks: byte[] OutMask = new byte[] {0}; byte[] Data = new byte[] { 0, 0, 0, 0 }; // for 4 ports //UInt32 Data = 0; // for just 18 bits // State vars: public byte[] TristateMask = new byte[1] {0}; public byte[] bTristate = new byte[2] {0,0}; // 0 = false all bits not tristated public bool[] bModeInOut = new bool[4 + 1]; // plus fast 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] = btnToggleFast; TristateButton[0] = btnTristateA; TristateButton[1] = btnTristateOther; Tristatelabel[0] = TristatelabelA; Tristatelabel[1] = TristatelabelOther; // Array for Data Rate Control: CountOutTrack[0] = trackBarFreqA; CountLabel[0] = lblFreqA; // 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[16] = checkBoxC0_17; checkStateArray[17] = checkBoxC0_18; checkStateArray[18] = checkBoxC0_19; checkStateArray[24] = checkBoxA1_25; checkStateArray[25] = checkBoxA1_26; checkStateArray[26] = checkBoxA1_27; // 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); //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; bModeInOut[i] = false; } // Must Set Port C to Output: OutMask[0] = 0x02; bModeInOut[2] = true; toggleBtnArray[2].Text = "Toggle to Input Mode"; // $$$ Status = AIOUSB.DIO_Configure(DeviceIndex, TristateMask, OutMask, Data); 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: // 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 >= 0x8008) && (PID <= 0x800F)) { 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); // Update size of data: textBoxBlockSize.Text = richTextBoxData.TextLength.ToString(); } 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++) for (int i = 0; i < 32; i++) { if (checkStateArray[i] != null) { 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 } } /* ushort pReadValue; int k = 0; for (uint i = 0; i < 3; 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); //CountDisplay[k++].Text = formatString; } } */ } private void btnTristate_Click(object sender, EventArgs e) { // Two buttons A or Other B C D and Fast: // Need to stop the timer change board and GUI: myTimer.Stop(); UInt32 Status = 0; int i = 0; 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 tristate var; //TristateMask = 0x00; //for(i = 0 ; i < 2; i++) //{ if (bTristate[iTagindex] != 0) // bool byte { bTristate[iTagindex] = 0; TristateMask[0] = (byte)(TristateMask[0] &~ (1 << (int)iTagindex) ); btnSender.Text = "Enter Tristate Mode"; Tristatelabel[iTagindex].Text = "Not Tristate: I/O Buffers Enabled"; } else { bTristate[iTagindex] = 1; TristateMask[0] = (byte)(TristateMask[0] | (1 << (int)iTagindex)); btnSender.Text = " Exit Tristate Mode"; Tristatelabel[iTagindex].Text = "Tristate Mode: I/O Buffers Disabled"; } //} // Recreate Outmask for Configure: for (i = 0; i < numPorts; i++) { if (bModeInOut[i] == true) // if output mode { iOutMask = iOutMask | (1 << i); // Int32 isOutMask = (Int16)iOutMask; //safe downcast? } } //byte OutMaskArray[] {0} OutMask[0] = (byte)isOutMask; Status = AIOUSB.DIO_ConfigureEx(DeviceIndex, OutMask, Data, TristateMask); //Status = AIOUSB.DIO_ConfigureEx(DeviceIndex, out OutMask, out Data, out TristateMask); //AIOUSB.DIO_Configure(DeviceIndex, TristateMask, 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(); UInt32 Status; 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 (bModeInOut[iTagindex]) { bModeInOut[iTagindex] = false; btnSender.Text = "Toggle to Output Mode"; } else { bModeInOut[iTagindex] = true; btnSender.Text = "Toggle to Input Mode"; } UInt32 OtherIndex = 2; //C and D always have opposite directions, so if they change one, change the other to be opposite //If the board only has one mode, then only allow that one mode if (iTagindex == 2 || iTagindex == 3) { if (iTagindex == 2) OtherIndex = 3; if (bModeInOut[iTagindex] == true) { bModeInOut[OtherIndex] = false; toggleBtnArray[OtherIndex].Text = "Toggle to Output Mode"; } else { bModeInOut[OtherIndex] = true; toggleBtnArray[OtherIndex].Text = "Toggle to Input Mode"; } } // Recreate new Outmask for Configure: for (i = 0; i < numPorts; i++) { if (bModeInOut[i] == true) // if output mode { 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, TristateMask, ref isOutMask, Data); OutMask[0] = (byte)isOutMask; Status = AIOUSB.DIO_ConfigureEx(DeviceIndex, OutMask, Data, TristateMask); // Restart timer thread: 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(); } private String FormatFreq(double Freq) { string formatString; if (Freq >= 1000000) formatString = String.Format("{0,0:F2}", Freq / 1000000) + "MHz"; //else if (Freq >= 10000) formatString = String.Format("{0,0:F2}", Freq / 1000) + "KHz"; else if (Freq >= 1000) formatString = String.Format("{0,0:F2}", Freq / 1000) + "KHz"; //else if (Freq >= 10) formatString = String.Format("{0,0:F2}", Freq) + "Hz"; 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(); double CounterValue; String TempString; CounterValue = CountOutTrack[0].Value; TempString = FormatFreq( CounterValue * 1000); // Max 8MHz CountLabel[0].Text = "Output " + TempString; // Restart timer thread: myTimer.Enabled = true; } private void checkBoxPin_CheckedChanged(object sender, MouseEventArgs 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 is used like a bool // restart timer and Gui will update: myTimer.Enabled = true; } private void btnAddSampleData_Click(object sender, EventArgs e) { richTextBoxData.LoadFile("SampleDocument.rtf"); textBoxBlockSize.Text = richTextBoxData.TextLength.ToString(); } private void btnClearSampleData_Click(object sender, EventArgs e) { richTextBoxData.Clear(); textBoxBlockSize.Text = richTextBoxData.TextLength.ToString(); } private void btnSaveSampleData_Click(object sender, EventArgs e) { richTextBoxData.SaveFile("SampleDocument.rtf", RichTextBoxStreamType.RichText); textBoxBlockSize.Text = richTextBoxData.TextLength.ToString(); } private void btnSend_Click(object sender, EventArgs e) { // Get data from GUI then send it (just to demo streaming): UInt32 Status; Double ReadClockHz, WriteClockHz; UInt32 BytesTransferred; // number of 8 bit bytes that were actually sent UInt32 bIsRead = 0; // 0 == false == send // Set the clocks and also read back exactly what they were set to: ReadClockHz = 0; //External or "off" WriteClockHz = trackBarFreqA.Value * 1000; // Max 8MHz // (Hz) One of the 2 boards musts set a clock speed Status = AIOUSB.DIO_StreamSetClocks(DeviceIndex, out ReadClockHz, out WriteClockHz); //Get the data to send from the memo, but there are many other options: //if InHexCheck.Checked then Buf := HexToStr(Buf); //if (Length(Buf) and 1) <> 0 then Buf := Buf + #0; //Pad out the data to whole words, since the board sends a word at a time // Using unicode characters which are 16 bit so we can copy to UInt16 // can also convert to bytes and send to reduce time or compress. // Get string length for array size parameters: UInt32 Length = (UInt32)richTextBoxData.TextLength; // number of UInt16's to send // will send all of whatever is in memo so tell user textBoxBlockSize.Text = richTextBoxData.TextLength.ToString(); // Alternate methods: String strBuf; UInt16[] MyBuffer = new UInt16[Length]; // same size as the text Char[] MyChars = new Char[Length]; Byte[] MyEncodedBytes; strBuf = richTextBoxData.Text; // Convert string to char array: MyChars = strBuf.ToCharArray(0, strBuf.Length); // Copy to UInt16 array: for(int i=0 ; i