using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using XPacNET; using System.Threading; namespace ReadTimeTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("mmtimer.dll")] static extern uint timeBeginPeriod(uint period); [DllImport("mmtimer.dll")] static extern uint timeEndPeriod(uint period); [DllImport("mmtimer.dll")] static extern uint timeSetEvent(uint uDelay, uint uResolution, TimerCallback lpTimeProc, UIntPtr dwUser, uint fuEvent); [DllImport("mmtimer.dll")] static extern uint timeKillEvent(uint uTimerID); public IntPtr hPort; public int iSlot, iDOChs; public uint uDOValue; //bool _bDOEnable; [Flags] public enum fuEvent : uint { TIME_ONESHOT = 0, //Event occurs once, after uDelay milliseconds. TIME_PERIODIC = 1, TIME_CALLBACK_FUNCTION = 0x0000, /* callback is function */ //TIME_CALLBACK_EVENT_SET = 0x0010, /* callback is event - use SetEvent */ //TIME_CALLBACK_EVENT_PULSE = 0x0020 /* callback is event - use PulseEvent */ } delegate void TimerCallback(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2); TimerCallback _timeproc; bool bled = false; void TimeProc_(uint uID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2) { /* if (_bDOEnable) { WinPAC.pac_WriteDO(hPort, iSlot, iDOChs, uDOValue); _bDOEnable = false; } else { WinPAC.pac_WriteDO(hPort, iSlot, iDOChs, 0); _bDOEnable = true; }*/ if (bled) { //XPacNET.XPac.pac_EnableLED(0, true); XPacNET.XPac.pac_WriteDO(hPort, iSlot, iDOChs, uDOValue); //XPacNET.XPac.pac_EnableLED(1, false); bled = false; } else { XPacNET.XPac.pac_WriteDO(hPort, iSlot, iDOChs, 0); //XPacNET.XPac.pac_EnableLED(0, false); //XPacNET.XPac.pac_EnableLED(1, true); bled = true; } } uint uiTimerID; private void btntimeSetEvent_Click(object sender, EventArgs e) { iSlot = Convert.ToInt32(cmbDOSlot.SelectedItem.ToString()); iDOChs = Convert.ToInt32(txtDOChannels.Text); uDOValue = Convert.ToUInt32(txtDOValue.Text, 16); hPort = XPacNET.XPac.uart_Open(""); //Use timeSetEvent to do real time operation timeBeginPeriod(1); fuEvent f = fuEvent.TIME_PERIODIC; _timeproc = TimeProc_; uiTimerID = timeSetEvent(1000, 1, _timeproc, UIntPtr.Zero, (uint)f); } private void btntimeKillEvent_Click(object sender, EventArgs e) { timeKillEvent(uiTimerID); timeEndPeriod(1); XPacNET.XPac.uart_Close(hPort); } [DllImport("XPAC_Utility.dll", EntryPoint = "pac_RegKeyExist")] public extern static bool pac_RegKeyExist(string KeyName); [DllImport("XPAC_Utility.dll", EntryPoint = "pac_RegCreateKey")] public extern static bool pac_RegCreateKey(string KeyName); [DllImport("XPAC_Utility.dll", EntryPoint = "pac_RegSetDWORD")] public extern static bool pac_RegSetDWORD(string KeyName, uint assignVal); private void btnPriority_Click(object sender, EventArgs e) { /* Set timeSetEvent priority */ bool ret; //Check if key exists ret = pac_RegKeyExist("HKEY_LOCAL_MACHINE\\System\\MMTIMER\\"); if (!ret) { //Create Key ret = pac_RegCreateKey("HKEY_LOCAL_MACHINE\\System\\MMTIMER\\"); if (!ret) MessageBox.Show("Create registry key fail.(\"HKEY_LOCAL_MACHINE\\System\\MMTIMER\\\")\n"); } //Set priority value uint uPriority = Convert.ToUInt32(txtPriority.Text); ret = pac_RegSetDWORD("HKEY_LOCAL_MACHINE\\System\\MMTIMER\\Priority256", uPriority); if (!ret) MessageBox.Show("Set priority value fail.(pac_RegSetDWORD() function fail)\n"); } } }