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 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 // WDG sample specific : UInt16 IRQ; bool Running; UInt32 State; UInt32 Loop; bool Testing; // 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 program demonstrates how to program the 82C54 counter/timer chip on the Watchdog Timer board. Hit the Test button to start and the Exit button to exit."; // 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."; 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 0x22C0: labelCardName.Text = "PCI-WDG-CSM Watchdog Card"; textBoxWatchdog.Text = " Watchdog Status"; 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."; RunFlag = false; } if (RunFlag) { // This is the result setting the active address offset to be used: Offset = 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: UpdateState(); } private void UpdateState() { // This is a test sample: UInt16 readback; switch(State) { case 0: AIOWDM.RelInPortB(CardNum, Offset + 4); // clear any pending interrupts AIOWDM.RelOutPortB(CardNum, Offset + 7,0); // start counters counting State++; break; case 1: CtrMode(1,2); CtrLoad(1,0x10); // keep reloading counter if (++Loop == 100) State++; String text; text = Convert.ToString(Loop); textBoxUpdate.Text = " Updating counter 1 ... loop number: " + text;//, Loop); //$$$RBF break; case 2: readback = AIOWDM.RelInPortB(CardNum, Offset + 4); if ((readback & 0x01) == 0) { textBoxUpdate.Text = " Watchdog Timed Out"; btnPerformIO.Text = "Test"; Testing = false; State++; Loop = 0; } else textBoxUpdate.Text = " Waiting for Timeout"; break; case 3: Loop++; if (Loop == 5) { AIOWDM.RelInPortB(CardNum, Offset + 6); myTimer.Enabled = false; myTimer.Stop(); State = Loop = 0; } break; } } private void btnPerformIO_Click(object sender, EventArgs e) { if(myTimer.Enabled) { AIOWDM.RelInPortB(CardNum, Offset + 6); //turn off alarm AIOWDM.RelInPortB(CardNum, Offset + 7); State = Loop = 0; Testing = false; myTimer.Enabled = false; myTimer.Stop(); btnPerformIO.Text = "Test"; } else { SetCounter(); Testing = true; State = Loop = 0; textBoxWatchdog.Text = " Watchdog Okay"; myTimer.Enabled = true; btnPerformIO.Text = "Abort"; } } 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(); AIOWDM.RelInPortB(CardNum, Offset + 6); AIOWDM.RelInPortB(CardNum, Offset + 7); // Close the form and Application: this.Close(); } // CTR functions: private void CtrMode(byte cntr, byte mode) { AIOWDM.RelOutPortB(CardNum, Offset + 3, (byte)((cntr << 6) | 0x30 | (mode << 1))); } private void CtrLoad(int c, int val) { AIOWDM.RelOutPortB(CardNum, (uint)(Offset + c), (byte)(val & 0x00FF)); AIOWDM.RelOutPortB(CardNum, (uint)(Offset + c), (byte)((val >> 8) & 0x00FF)); } private void SetCounter() { AIOWDM.RelInPortB(CardNum, Offset + 7); // disable counter functions CtrMode(2,0); // set counter 2 to mode 0 AIOWDM.RelOutPortB(CardNum, Offset + 7, 0); // enable counters AIOWDM.RelInPortB(CardNum, Offset + 7); // disable counter functions AIOWDM.RelOutPortB(CardNum, Offset + 6, 0); // enable buzzer AIOWDM.RelOutPortB(CardNum, Offset + 0x0C, 0); // select low clock rate AIOWDM.RelInPortB(CardNum, Offset + 0x0D); // disable opto reset AIOWDM.RelInPortB(CardNum, Offset + 0x0E); // disable opto reset AIOWDM.RelInPortB(CardNum, Offset + 0x0F); // disable program outputs // program the counters for the REQUIRED modes CtrMode(0,3); // set counter 0 to mode 3 CtrLoad(0, 0xFFFF); // full load value CtrMode(1,2); // set counter 1 to mode 2 CtrLoad(1, 0x10); // load counter 1 with 10 hex CtrMode(2,1); // set counter 2 to mode 1 // Note:Not loading counter 2 creates an infinite reset duration // Set the reset duration by loading a value in counter 2 // Change counter 2 to mode 2 to set duration } } }