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 AIOWDMNet; // the namespace exposes the AIOWDM Class interface namespace DIO { public partial class Form1 : Form { // One and only Device Index: UInt32 Status = 0; public UInt32 DeviceIndex = AIOWDM.diFirst; Int32 NumCards = 0; const UInt32 MAX_CARDS = 10; UInt32 Address; // This the result of findcards and the address used bool RunFlag; public bool TimerEnabled = false; public ushort[] value = new ushort[] { 0, 0, 0 }; public int i = 0; public struct TCardData { public bool IsValid; public bool IsSelected; public UInt32 DeviceID; public UInt32 Base; }; public TCardData[] CardData = new TCardData[MAX_CARDS]; // only set up for 10 max here // 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(); public Form1() { InitializeComponent(); // Initialize default values: DeviceIndex = AIOWDM.diFirst; // Default to first device found // Ping the driver AIOWDM.SYS: if (AIOWDM.InPortB(0x61) == 0xAA55) { MessageBox.Show(" AIOWDM.SYS not detected.\n Please copy AIOWDM.SYS into [Windows]/system32/drivers and re-run this sample.\n Ensure that a board is installed properly.", "Warning"); } // Check device info: Int32 iNumCards = AIOWDM.GetNumCards(); FindCardsWDM(); textBoxStatus.Text = "This sample outputs a walking bit on Port A of the selected group. It then reads back the value from Port A. This should be identical to the output value due to on-chip readback. It also displays the Input from Port B. If a wrap plug is installed, Port B should read the same value. Otherwise, ground individual bits on Port B to see them show zeroes on the display."; // 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(); // not yet } private void Form1_Load(object sender, EventArgs e) { } private void FindCardsWDM() { bool found = false; char[] AddStr = new char[256]; // Vars for QueryCardInfo() : UInt32 CardNum; UInt32 DeviceID, Base; UInt32 NameSize; UInt16[] Name = new UInt16[256]; String strname; // Init: RunFlag = true; NumCards = 0; int DataIndex = 0; // Set CardData to 0 as needed max 10: for(int count = 0; count < MAX_CARDS; count++) { CardData[count].IsValid = false; CardData[count].IsSelected = false; CardData[count].DeviceID = 0; CardData[count].Base = 0; }; // Get the total number of cards installed: NumCards = AIOWDM.GetNumCards(); if (NumCards == 0) // no cards present { labelCardName.Text = "No Card Found"; textBoxStatus.Text = "No cards were found!"; textBoxStatus.Text += "This may mean the card is not installed. "; textBoxStatus.Text += "Check Device Manager for a card and its status"; textBoxStatus.Text += "You may consider rebooting your system."; comboBoxPort.Enabled = false; RunFlag = false; } else { // Loop through the cards and validate and store card data: for (CardNum = 0; CardNum != NumCards; CardNum++) { NameSize = 256; Status = AIOWDM.QueryCardInfo(CardNum, out DeviceID, out Base, out NameSize, out strname); // Populate list box with addresses found set flags: switch (DeviceID) { // Will only cover PCIe-DIO-24 family in this sample: case 0x0C52: labelCardName.Text = "PCIe-DIO-24 Parallel Digital I/O Card"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E53: labelCardName.Text = "PCIe-DIO-24S Parallel Digital I/O Card"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0C53: labelCardName.Text = "PCIe-DIO-24D Parallel Digital I/O Card"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E55: labelCardName.Text = "PCIe-DIO-24DC Parallel Digital I/O Card w/Counter"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E54: labelCardName.Text = "PCIe-DIO-24DS Parallel Digital I/O Card"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname;; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; case 0x0E56: labelCardName.Text = "PCIe-DIO-24DCS Parallel Digital I/O Card w/Counter"; strname = String.Format("Port 0: {0,4:X}", Base & 0xFFF8); comboBoxPort.Text = strname; if(CardNum <= MAX_CARDS) { CardData[DataIndex].IsValid = true; CardData[DataIndex].DeviceID = DeviceID; CardData[DataIndex].Base = Base; DataIndex++; } found = true; break; default: break; } }// end loop on validate present cards }//end else card present // If card(s) are present but no valid cards were found: if ((NumCards != 0) && (found == false) ) { labelCardName.Text = "No Valid Card Found."; textBoxStatus.Text = "No valid card was found!"; textBoxStatus.Text += "This may mean the card is not installed. "; textBoxStatus.Text += "Check Device Manager for a card and its status"; textBoxStatus.Text += "You may consider rebooting your system."; comboBoxPort.Enabled = false; RunFlag = false; } //comboBoxPort.SelectedIndex = 0; //m_AddressCombo.SetCurSel(0); if (RunFlag) { //UpdateData(true); // This is the result setting the active address to be used: Address = CardData[0].Base; //default to fist valid CardData[0].IsSelected = true; } } 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: int j; char[] msg = new char[] {'0','0','0','0','0','0','0','0'}; StringBuilder s2 = new StringBuilder(); StringBuilder s3 = new StringBuilder(); StringBuilder s4 = new StringBuilder(); value[0] = (ushort)(1 << i++); AIOWDM.OutPortB(Address, value[0]); // Driver Calls value[1] = AIOWDM.InPortB(Address); value[2] = AIOWDM.InPortB(Address + 1); i %= 8; for (j=0; j<8; j++) { if( ((int)value[0] & (int)(1 << j)) != 0 ) { msg[7-j] = '1'; } else { msg[7-j] = '0'; } } s2.Insert(0, msg); textBoxOutputA.Text = s2.ToString(); for (j=0; j<8; j++) { if (((int)value[1] & (int)(1 << j)) != 0) { msg[7 - j] = '1'; } else { msg[7 - j] = '0'; } } s3.Insert(0, msg); textBoxInputA.Text = s3.ToString(); for (j = 0; j < 8; j++) { if (((int)value[2] & (int)(1 << j)) != 0) { msg[7 - j] = '1'; } else { msg[7 - j] = '0'; } } s4.Insert(0, msg); textBoxInputB.Text = s4.ToString(); } private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e) { String SelStr; SelStr = comboBoxPort.GetItemText(comboBoxPort.SelectedIndex); // Select and set new address data: if (CardData[comboBoxPort.SelectedIndex].IsValid == true) { Address = CardData[comboBoxPort.SelectedIndex].Base; // Deselect all data and set new selected data: for (int count = 0; count < MAX_CARDS; count++) { CardData[count].IsSelected = false; } CardData[comboBoxPort.SelectedIndex].IsSelected = true; } textBoxStatus.Text += "New Port Selected: "; textBoxStatus.Text += SelStr; comboBoxPort.SelectedText = SelStr; } private void btnPerformIO_Click(object sender, EventArgs e) { if (TimerEnabled) { TimerEnabled = false; myTimer.Stop(); btnPerformIO.Text = "Perform I/O"; } else { TimerEnabled = true; AIOWDM.OutPortB(Address + 3, 0x82); // Driver call myTimer.Enabled = true; btnPerformIO.Text = "Stop I/O"; } } private void btnExit_Click(object sender, EventArgs e) { // Right now this is only called on button click not window close ALtF4 etc: // Kill the timer: myTimer.Stop(); myTimer.Enabled = false; myTimer.Dispose(); // Close the form and Application: this.Close(); } } }