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 { UInt32 Status = 0; // Data used to discover cards in the system: const UInt32 MAX_CARDS = 10; public Int32 NumCards = 0; // This is the basic data that we will be using to interface with the card: public Int32 CardNum = 0; // The index of the one card we will be using 0 public UInt32 Offset = 0; // This the offset that will be used based on the base address of the card public bool RunFlag = false; public bool TimerEnabled = false; public byte[] value = new byte[] { 0, 0, 0 }; // 3 values used for read write public int i = 0; // This will hold the data for the card installed in the system: public struct TCardData { public bool IsValid; public bool IsSelected; public UInt32 DeviceID; public UInt32 Base; // This a result of findcards and the base address of the card }; public TCardData CardData; // only one struct public UInt32[] PortOffsets = new UInt32[5] { 0, 4, 8, 12, 16 }; // offsets for 5 max ports // 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: // Ping the driver AIOWDM.SYS: // This takes an absolute sddress not relative offset // This is the one place we actual might use all 16 bits returned for an error code AA55 // otherwise we only use an 8 bit byte of data per read and write: 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"); } 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."; // This will get all the card info: FindCardsWDM(); // 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 = 60; } private void Form1_Load(object sender, EventArgs e) { } private void FindCardsWDM() { bool found = false; // Local Vars for QueryCardInfo(): UInt32 DeviceID, Base; UInt32 NameSize = 256; UInt16[] Name = new UInt16[256]; String strname; // Set CardData to 0 as needed: CardData.IsValid = false; CardData.IsSelected = false; CardData.DeviceID = 0; CardData.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 { // Validate and store card data: Status = AIOWDM.QueryCardInfo(CardNum, out DeviceID, out Base, out NameSize, out strname); // Populate list box with addresse offsets found set flags: switch (DeviceID) { case 0xECA9: labelCardName.Text = "PCI-A12-16 Analog I/O Card with Digital"; comboBoxPort.Items.Add("Group 0 Base Offset 16"); comboBoxPort.SelectedIndex = 0; Offset = PortOffsets[4]; // this is hex 10 decimal 16 offset to DIO chip for PCIA12 CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0xECAA: labelCardName.Text = "PCI-A12-16 Analog I/O Card with Digital"; comboBoxPort.Items.Add("Group 0 Base Offset 16"); comboBoxPort.SelectedIndex = 0; Offset = PortOffsets[4]; // this is hex 10 decimal 16 offset to DIO chip for PCIA12 CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x2C58: labelCardName.Text = "PCI-DIO-96C3 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.Items.Add("Group 2 Base Offset 12"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x2C70: case 0x2C50: labelCardName.Text = "PCI-DIO-96CT Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.Items.Add("Group 2 Base Offset 12"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C50: labelCardName.Text = "PCI-DIO-24H Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C51: labelCardName.Text = "PCI-DIO-24D Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E50: labelCardName.Text = "PCI-DIO-24S Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E51: labelCardName.Text = "PCI-DIO-24H(C) Parallel Digital I/O Card w/Counter"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E52: labelCardName.Text = "PCI-DIO-24D(C) Parallel Digital I/O Card w/Counter"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C60: labelCardName.Text = "PCI-DIO-48 Parallel Digital I/O Card"; strname = String.Format("Port 0: {0,4:X}", (Base & 0xFFF8) ); comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E60: labelCardName.Text = "PCI-DIO-48S Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C52: labelCardName.Text = "PCIe-DIO-24 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E53: labelCardName.Text = "PCIe-DIO-24S Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C53: labelCardName.Text = "PCIe-DIO-24D Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E55: labelCardName.Text = "PCIe-DIO-24DC Parallel Digital I/O Card w/Counter"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E54: labelCardName.Text = "PCIe-DIO-24DS Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0E56: labelCardName.Text = "PCIe-DIO-24DCS Parallel Digital I/O Card w/Counter"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C61: labelCardName.Text = "PCIe-DIO-48 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; break; case 0x0E61: labelCardName.Text = "PCIe-DIO-48S Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C68: labelCardName.Text = "PCI-DIO-72 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C70: labelCardName.Text = "PCI-DIO-96 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.Items.Add("Group 3 Base Offset 12"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C69: labelCardName.Text = "P104-DIO-96 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.Items.Add("Group 3 Base Offset 12"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x0C78: labelCardName.Text = "PCI-DIO-120 Parallel Digital I/O Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.Items.Add("Group 1 Base Offset 4"); comboBoxPort.Items.Add("Group 2 Base Offset 8"); comboBoxPort.Items.Add("Group 3 Base Offset 12"); comboBoxPort.Items.Add("Group 4 Base Offset 16"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x2EE0: labelCardName.Text = "PCIe-DIO24S-CTR12 Digital/Counter Card"; comboBoxPort.Items.Add("Group 0 Base Offset 0"); comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; // PCI-DA12-8/16 cards Offset 20,21,22 Port A,B,C case 0x6CA8: labelCardName.Text = "PCI-DA12-8 Digital to Analog Output Card"; comboBoxPort.Items.Add("Group 0 Base Offset 20"); comboBoxPort.Items.Add("Group 1 Base Offset 21"); comboBoxPort.Items.Add("Group 2 Base Offset 22"); PortOffsets[0] = 20; PortOffsets[1] = 21; PortOffsets[2] = 22; comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; case 0x6CB0: labelCardName.Text = "PCI-DA12-16 Digital to Analog Output Card"; comboBoxPort.Items.Add("Group 0 Base Offset 20"); comboBoxPort.Items.Add("Group 1 Base Offset 21"); comboBoxPort.Items.Add("Group 2 Base Offset 22"); PortOffsets[0] = 20; PortOffsets[1] = 21; PortOffsets[2] = 22; comboBoxPort.SelectedIndex = 0; CardData.IsValid = true; CardData.DeviceID = DeviceID; CardData.Base = Base; found = true; break; default: break; }// end switch }//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; } if (RunFlag) { // This is the result setting the active address offset to be used: Offset = PortOffsets[0]; //default to first valid base offset (Port 0) CardData.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() { // This is the walking bit for board and display based on port selected: // We use 8 bits but pass a 16 bit value incase we get an AA55 error returned: ushort retval; 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] = (byte)(1 << i++); retval = AIOWDM.RelOutPortB(CardNum, Offset, value[0]); // Driver Calls //write a byte value[1] = (byte)AIOWDM.RelInPortB(CardNum, Offset); //read a byte into ushort value[2] = (byte)AIOWDM.RelInPortB(CardNum, Offset + 1); // read a byte into ushort 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(); Update(); } private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e) { String SelStr; SelStr = comboBoxPort.Items[comboBoxPort.SelectedIndex].ToString(); //Set new Offset from Array of correct offsets Offset = PortOffsets[comboBoxPort.SelectedIndex]; if (CardData.DeviceID == 0xECA9 || CardData.DeviceID == 0xECAA) { Offset = PortOffsets[4]; // this is hex 10 decimal 16 offset to DIO chip for PCIA12 } textBoxStatus.Text += "\r\n New Port Selected: "; textBoxStatus.Text += SelStr; } private void btnPerformIO_Click(object sender, EventArgs e) { if (TimerEnabled) { TimerEnabled = false; myTimer.Stop(); btnPerformIO.Text = "Perform I/O"; } else { TimerEnabled = true; AIOWDM.RelOutPortB(CardNum, Offset + 3, 0x82); // write to control register, 100000010, set B Input, A Output and mode etc 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(); } } }