private UInt32 GetPortValue(UInt16 portAddress) { UInt32 portValue = 0; if (hMod != IntPtr.Zero) { if (this.GetPortVal == null) { IntPtr pFunc = GetProcAddress(hMod, "GetPortVal"); if (pFunc != IntPtr.Zero) { this.GetPortVal = (GetPortValType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(GetPortValType)); } else { string msg = "GetPortValue(): Unable to lcoate procedure address."; Console.WriteLine(msg); // throw new Exception(msg); } } if (!this.GetPortVal(portAddress, &portValue, 1)) { string msg = "GetPortValue(): Error returned from GetPortVal()"; Console.WriteLine(msg); // throw new Exception(msg); } } return(portValue); }
private void btnGetValue_Click(object sender, EventArgs e) { IntPtr pFunc = GetProcAddress(hMod, "GetPortVal"); if (pFunc != IntPtr.Zero) { UInt16 PortAddr; UInt32 PortVal; PortAddr = UInt16.Parse(txtPortAddr.Text, System.Globalization.NumberStyles.HexNumber); GetPortValType GetPortVal = (GetPortValType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(GetPortValType)); // Call WinIo to get value //bool Result = GetPortVal(PortAddr, &PortVal, 1); bool Result = GetPortVal(PortAddr, &PortVal, 4); if (Result) { //txtValue.Text = PortVal.ToString("X"); txtValue.Text = PortVal.ToString("X8"); } else { MessageBox.Show("Error returned from GetPortVal", "DumpPort", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//等待鍵盤緩衝區為空 private static void KBCWait4IBE() { IntPtr pFunc = GetProcAddress(hMod, "GetPortVal"); if (pFunc != IntPtr.Zero) { GetPortValType GetPortVal = (GetPortValType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(GetPortValType)); UInt32 PortVal; do { bool flag = GetPortVal(0x64, &PortVal, 1); }while ((PortVal & 0x2) > 0); } }
public void Close() { if (hMod != IntPtr.Zero) { IntPtr pFunc = GetProcAddress(hMod, "ShutdownWinIo"); if (pFunc != IntPtr.Zero) { this.GetPortVal = null; this.SetPortVal = null; ShutdownWinIoType ShutdownWinIo = (ShutdownWinIoType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(ShutdownWinIoType)); ShutdownWinIo(); FreeLibrary(hMod); hMod = IntPtr.Zero; } } }
public bool Open(Form1 form) { form1 = form; if (IntPtr.Size == 4) { hMod = LoadLibrary("GPIO32.dll"); } else { hMod = LoadLibrary("GPIO64.dll"); } if (hMod != IntPtr.Zero) { IntPtr pFunc = GetProcAddress(hMod, "InitializeGPIO"); if (pFunc != IntPtr.Zero) { InitializeGPIOType InitializeGPIO = (InitializeGPIOType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(InitializeGPIOType)); if (InitializeGPIO()) { IntPtr pFuncSet = GetProcAddress(hMod, "SetPortVal"); SetPortVal = (SetPortValType)Marshal.GetDelegateForFunctionPointer(pFuncSet, typeof(SetPortValType)); IntPtr pFuncGet = GetProcAddress(hMod, "GetPortVal"); GetPortVal = (GetPortValType)Marshal.GetDelegateForFunctionPointer(pFuncGet, typeof(GetPortValType)); GPIOThread.DoWork += new DoWorkEventHandler(GPIOLoop); GPIOThread.WorkerSupportsCancellation = true; return(true); } } } FreeLibrary(hMod); return(false); }
private UInt32 GetPortValue(UInt16 portAddress) { UInt32 portValue = 0; if (hMod != IntPtr.Zero) { if (this.GetPortVal == null) { IntPtr pFunc = GetProcAddress(hMod, "GetPortVal"); if (pFunc != IntPtr.Zero) { this.GetPortVal = (GetPortValType)Marshal.GetDelegateForFunctionPointer(pFunc, typeof(GetPortValType)); } else { string msg = "GetPortValue(): Unable to lcoate procedure address."; Console.WriteLine(msg); // throw new Exception(msg); } } if (!this.GetPortVal(portAddress, &portValue, 1)) { string msg = "GetPortValue(): Error returned from GetPortVal()"; Console.WriteLine(msg); // throw new Exception(msg); } } return portValue; }