zwavers232.cs
来自「zwave 无线通讯协议 PC controller 控制器源码」· CS 代码 · 共 868 行 · 第 1/3 页
CS
868 行
Close();
GC.SuppressFinalize(this);
}
}
}
#endregion
#region CommAPI finalizer
/// <summary>
/// Use C# destructor syntax for finalization code.
/// This destructor will run only if the Dispose method does not get called.
/// </summary>
~CommAPI()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
// GC.SuppressFinalize(this);
}
#endregion
[DllImport("kernel32", EntryPoint = "LocalAlloc", SetLastError = true)]
internal static extern IntPtr LocalAlloc(int uFlags, int uBytes);
[DllImport("kernel32", EntryPoint = "LocalFree", SetLastError = true)]
internal static extern IntPtr LocalFree(IntPtr hMem);
}
#endregion
#region Windows CE (Pocket PC) CompactFramework implementation for CommAPI
internal class CECommAPI : CommAPI
{
override internal bool CreateFile(string port)
{
hPort = CECreateFileW(port, GENERIC_WRITE | GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
return hPort != (IntPtr)INVALID_HANDLE_VALUE;
}
override internal bool ReadFile(byte[] buffer, int cbToRead, ref Int32 cbRead, IntPtr lpOverlapped)
{
return Convert.ToBoolean(CEReadFile(hPort, buffer, cbToRead, ref cbRead, IntPtr.Zero));
}
override internal bool WriteFile(byte[] buffer, Int32 cbToWrite, ref Int32 cbWritten, IntPtr lpOverlapped)
{
return Convert.ToBoolean(CEWriteFile(hPort, buffer, cbToWrite, out cbWritten, IntPtr.Zero));
}
override internal bool Close()
{
bool rc = Convert.ToBoolean(CECloseHandle(hPort));
if( rc ) hPort = (System.IntPtr)INVALID_HANDLE_VALUE;
return rc;
}
override internal bool SetCommState(DCB dcb)
{
return Convert.ToBoolean(CESetCommState(hPort, dcb));
}
override internal bool GetCommState(DCB dcb)
{
return Convert.ToBoolean(CEGetCommState(hPort, dcb));
}
override internal bool SetCommTimeouts(CommTimeouts timeouts)
{
return Convert.ToBoolean(CESetCommTimeouts(hPort, timeouts));
}
internal override bool PurgeComm(PurgeFlags dwFlags)
{
return Convert.ToBoolean(CEPurgeComm(hPort, (int)dwFlags));
}
internal override bool WaitCommEvent(ref CommEventFlags flags)
{
return Convert.ToBoolean(CEWaitCommEvent(hPort, ref flags, IntPtr.Zero));
}
internal override bool SetCommMask(CommEventFlags dwEvtMask)
{
return Convert.ToBoolean(CESetCommMask(hPort, dwEvtMask));
}
internal override bool GetOverlappedResult(IntPtr lpOverlapped, out Int32 lpNumberOfBytesTransferred, bool bWait)
{
lpNumberOfBytesTransferred = 0;
return false;
}
internal override int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds)
{
return CEWaitForSingleObject(hHandle, dwMilliseconds);
}
internal override IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName)
{
return CECreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
}
#region Windows CE API P/Invoke imports
[DllImport("coredll.dll", EntryPoint = "CreateFileW", SetLastError = true)]
private static extern IntPtr CECreateFileW(
String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("coredll.dll", EntryPoint = "PurgeComm", SetLastError = true)]
private static extern int CEPurgeComm(IntPtr hPort, int dwFlags);
[DllImport("coredll.dll", EntryPoint = "SetCommTimeouts", SetLastError = true)]
private static extern int CESetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);
[DllImport("coredll.dll", EntryPoint = "GetCommState", SetLastError = true)]
private static extern int CEGetCommState(IntPtr hFile, DCB dcb);
[DllImport("coredll.dll", EntryPoint = "SetCommState", SetLastError = true)]
private static extern int CESetCommState(IntPtr hFile, DCB dcb);
[DllImport("coredll.dll", EntryPoint = "CloseHandle", SetLastError = true)]
private static extern int CECloseHandle(IntPtr hObject);
[DllImport("coredll.dll", EntryPoint = "WriteFile", SetLastError = true)]
private static extern int CEWriteFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, out Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("coredll.dll", EntryPoint = "ReadFile", SetLastError = true)]
private static extern int CEReadFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("coredll.dll", EntryPoint = "WaitCommEvent", SetLastError = true)]
private static extern int CEWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);
[DllImport("coredll.dll", EntryPoint = "SetCommMask", SetLastError = true)]
private static extern int CESetCommMask(IntPtr handle, CommEventFlags dwEvtMask);
[DllImport("coredll.dll", EntryPoint = "WaitForSingleObject", SetLastError = true)]
private static extern int CEWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("coredll.dll", EntryPoint = "CreateEvent", SetLastError = true)]
private static extern IntPtr CECreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName);
#endregion
}
#endregion
#region Windows XP/2000/Desktop Full .NET Framework implementation for CommAPI
internal class WinCommAPI : CommAPI
{
override internal bool CreateFile(string port)
{
hPort = WinCreateFileW(port, GENERIC_WRITE | GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
return hPort != (IntPtr)INVALID_HANDLE_VALUE;
}
override internal bool ReadFile(byte[] buffer, int cbToRead, ref Int32 cbRead, IntPtr lpOverlapped)
{
return Convert.ToBoolean(WinReadFile(hPort, buffer, cbToRead, ref cbRead, lpOverlapped));
}
override internal bool WriteFile(byte[] buffer, Int32 cbToWrite, ref Int32 cbWritten, IntPtr lpOverlapped)
{
return Convert.ToBoolean(WinWriteFile(hPort, buffer, cbToWrite, ref cbWritten, lpOverlapped));
}
override internal bool Close()
{
bool rc = Convert.ToBoolean(WinCloseHandle(hPort));
if (rc) hPort = (System.IntPtr)INVALID_HANDLE_VALUE;
return rc;
}
override internal bool SetCommState(DCB dcb)
{
return Convert.ToBoolean(WinSetCommState(hPort, dcb));
}
override internal bool GetCommState(DCB dcb)
{
return Convert.ToBoolean(WinGetCommState(hPort, dcb));
}
override internal bool SetCommTimeouts(CommTimeouts timeouts)
{
return Convert.ToBoolean(WinSetCommTimeouts(hPort, timeouts));
}
internal override bool PurgeComm(PurgeFlags dwFlags)
{
return Convert.ToBoolean(WinPurgeComm(hPort, (int)dwFlags));
}
internal override bool WaitCommEvent(ref CommEventFlags flags)
{
return Convert.ToBoolean(WinWaitCommEvent(hPort, ref flags, IntPtr.Zero));
}
internal override bool SetCommMask(CommEventFlags dwEvtMask)
{
return Convert.ToBoolean(WinSetCommMask(hPort, dwEvtMask));
}
internal override bool GetOverlappedResult(IntPtr lpOverlapped, out Int32 lpNumberOfBytesTransferred, bool bWait)
{
return WinGetOverlappedResult(hPort, lpOverlapped, out lpNumberOfBytesTransferred, bWait);
}
internal override int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds)
{
return WinWaitForSingleObject(hHandle, dwMilliseconds);
}
override internal IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName)
{
return WinCreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
}
#region Desktop Windows API P/Invoke imports
[ComVisible(true)]
[DllImport("kernel32.dll", EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr WinCreateFileW(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", EntryPoint = "PurgeComm", SetLastError = true)]
private static extern int WinPurgeComm(IntPtr hPort, int dwFlags);
[DllImport("kernel32.dll", EntryPoint = "SetCommTimeouts", SetLastError = true)]
private static extern int WinSetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);
[DllImport("kernel32.dll", EntryPoint = "GetCommState", SetLastError = true)]
private static extern int WinGetCommState(IntPtr hFile, DCB dcb);
[DllImport("kernel32.dll", EntryPoint = "SetCommState", SetLastError = true)]
private static extern int WinSetCommState(IntPtr hFile, DCB dcb);
[DllImport("kernel32.dll", EntryPoint = "SetupComm", SetLastError = true)]
private static extern int WinSetupComm(IntPtr hFile, Int32 dwInQueue, Int32 dwOutQueue);
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true)]
private static extern int WinCloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", EntryPoint = "WriteFile", SetLastError = true)]
private static extern int WinWriteFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll", EntryPoint = "ReadFile", SetLastError = true)]
private static extern int WinReadFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll", EntryPoint = "SetCommMask", SetLastError = true)]
private static extern int WinSetCommMask(IntPtr handle, CommEventFlags dwEvtMask);
[DllImport("kernel32.dll", EntryPoint = "WaitCommEvent", SetLastError = true)]
private static extern int WinWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);
[DllImport("kernel32.dll", EntryPoint = "GetOverlappedResult", SetLastError = true)]
private static extern bool WinGetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped, out Int32 lpNumberOfBytesTransferred, bool bWait);
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", SetLastError = true)]
private static extern int WinWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("kernel32.dll", EntryPoint = "CreateEvent", SetLastError = true)]
private static extern IntPtr WinCreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName);
#endregion
}
#endregion
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?