using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; using PACNET; using pac_i8048WNet; namespace pac_i8048W_Dotnet { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region Parameters const int ChannelMax = 8; int iSlot = 0; uint TotalCnt;//total count uint[] RisingEdgeCnt = new uint[8]; uint[] FallingEdgeCnt = new uint[8]; uint SecondCnt = 0; #endregion private void Form1_Load(object sender, EventArgs e) { cmbSlot.SelectedIndex = 0; cmbTriggerType.SelectedIndex = 0; } private void txtInit_Click(object sender, EventArgs e)//Init i8048 { int TriType, ThreadPriority; byte BPRetriggerTime = 0; //Get slot number iSlot = cmbSlot.SelectedIndex; //Get trigger type TriType = cmbTriggerType.SelectedIndex; //Set thread priority ThreadPriority = 97; //Set backplane(bp) retrigger BPRetriggerTime = 10; /* The re-trigger level is used for the backplane interrupt of WinPAC. The CPU lost to receive the interrupt signal triggered by i8048W, and this re-trigger mechanism will make the backplane processer to re-send the interrupt signal to CPU again. It's recommended to set the re-trigger level to 10 (unit is 10 microsecond, 10*10 =100 microsecond) */ //Retrigger unit Backplane.pac_EnableRetrigger(BPRetriggerTime); TotalCnt = 0; for (int ch = 0; ch < ChannelMax; ch++)//給所有的count設定初值 { RisingEdgeCnt[ch] = 0; FallingEdgeCnt[ch] = 0; } if (pac8048W.Init(iSlot) == 0) //set trigger type in rising or falling { if (TriType == 0) { for (int ch = 0; ch < ChannelMax; ch++) { pac8048W.Set_RisingReg(iSlot, ch, 1); pac8048W.Set_FallingReg(iSlot, ch, 0); } } else { for (int ch = 0; ch < ChannelMax; ch++) { pac8048W.Set_FallingReg(iSlot, ch, 1); pac8048W.Set_RisingReg(iSlot, ch, 0); } } /* The default priority level of the interrupt service thread,Interrupt_Fun function for i8048W is 97 Many higher priority levels (247 through zero) are assigned to real-time applications, drivers, and system processes. . Range Description 0 through 96 Reserved for real-time above drivers. 97 through 152 Used by the default Windows CE-based device drivers. 153 through 247 Reserved for real-time below drivers. It's recommended to set the priority level between 97 and 152 for i8048w IST Please refer to http://msdn.microsoft.com/en-us/library/aa450596.aspx for mode details. */ pac8048W.PAC_CALLBACK_FUNC callbackFunc = new pac8048W.PAC_CALLBACK_FUNC(Interrupt_Fun); pac8048W.InstallISR(iSlot, callbackFunc, ThreadPriority); uint ec = ErrHandling.pac_GetLastError(); if (ec>0) MessageBox.Show(((ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X")); else { btnStop.Enabled = true; txtInit.Enabled = false; timer1.Enabled = true; } } else { MessageBox.Show ("pac8048W.Init failed"); } } private int Interrupt_Fun() //Interrupt Function { for (int ch = 0; ch < ChannelMax; ch++) { if (pac8048W.Read_RisingReg(iSlot, ch) != 0) //Read Rising Interrupt Status { RisingEdgeCnt[ch]++; TotalCnt++;//Total Interrupt count //Add user's ISR code for channels here. } if (pac8048W.Read_FallingReg(iSlot, ch) != 0) { FallingEdgeCnt[ch]++; TotalCnt++; //Add user's ISR code for channels here. } pac8048W.UnFreezeINT(iSlot, ch); } return 0; } private void timer1_Tick(object sender, EventArgs e) { int DI = pac8048W.DI_ALL(iSlot); SecondCnt++; txtCh0_REC.Text = RisingEdgeCnt[0].ToString(); txtCh1_REC.Text = RisingEdgeCnt[1].ToString(); txtCh2_REC.Text = RisingEdgeCnt[2].ToString(); txtCh3_REC.Text = RisingEdgeCnt[3].ToString(); txtCh4_REC.Text = RisingEdgeCnt[4].ToString(); txtCh5_REC.Text = RisingEdgeCnt[5].ToString(); txtCh6_REC.Text = RisingEdgeCnt[6].ToString(); txtCh7_REC.Text = RisingEdgeCnt[7].ToString(); txtCh0_FEC.Text = FallingEdgeCnt[0].ToString(); txtCh1_FEC.Text = FallingEdgeCnt[1].ToString(); txtCh2_FEC.Text = FallingEdgeCnt[2].ToString(); txtCh3_FEC.Text = FallingEdgeCnt[3].ToString(); txtCh4_FEC.Text = FallingEdgeCnt[4].ToString(); txtCh5_FEC.Text = FallingEdgeCnt[5].ToString(); txtCh6_FEC.Text = FallingEdgeCnt[6].ToString(); txtCh7_FEC.Text = FallingEdgeCnt[7].ToString(); txtTotalCnt.Text = TotalCnt.ToString(); } private void btnClearCnt_Click(object sender, EventArgs e)//Clear Count { SecondCnt = 0; for (int ch = 0; ch < ChannelMax; ch++) { RisingEdgeCnt[ch] = 0; FallingEdgeCnt[ch] = 0; } TotalCnt = 0; } private void btnStop_Click(object sender, EventArgs e) { timer1.Enabled = false; pac8048W.UnInstallISR(iSlot); btnStop.Enabled = false; txtInit.Enabled = true; } private void Form1_Closed(object sender, EventArgs e) { pac8048W.UnInstallISR(iSlot); } } }