📄 commapi.cs
字号:
{
if (FullFramework)
{
return Convert.ToBoolean(WinCloseHandle(hPort));
}
else
{
return Convert.ToBoolean(CECloseHandle(hPort));
}
}
internal static bool SetupComm(IntPtr hPort, UInt32 dwInQueue, UInt32 dwOutQueue)
{
if (FullFramework)
{
return Convert.ToBoolean(WinSetupComm(hPort, dwInQueue, dwOutQueue));
}
else
{
return Convert.ToBoolean(CESetupComm(hPort, dwInQueue, dwOutQueue));
}
}
internal static bool SetCommState(IntPtr hPort, DCB dcb)
{
if (FullFramework)
{
return Convert.ToBoolean(WinSetCommState(hPort, dcb));
}
else
{
return Convert.ToBoolean(CESetCommState(hPort, dcb));
}
}
internal static bool GetCommState(IntPtr hPort, DCB dcb)
{
if (FullFramework)
{
return Convert.ToBoolean(WinGetCommState(hPort, dcb));
}
else
{
return Convert.ToBoolean(CEGetCommState(hPort, dcb));
}
}
internal static bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts)
{
if (FullFramework)
{
return Convert.ToBoolean(WinSetCommTimeouts(hPort, timeouts));
}
else
{
return Convert.ToBoolean(CESetCommTimeouts(hPort, timeouts));
}
}
internal static bool EscapeCommFunction(IntPtr hPort, CommEscapes escape)
{
if (FullFramework)
{
return Convert.ToBoolean(WinEscapeCommFunction(hPort, (uint)escape));
}
else
{
return Convert.ToBoolean(CEEscapeCommFunction(hPort, (uint)escape));
}
}
internal static IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName)
{
if (FullFramework)
{
return WinCreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
}
else
{
return CECreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
}
}
internal static bool SetEvent(IntPtr hEvent)
{
if (FullFramework)
{
return Convert.ToBoolean(WinEventModify(hEvent, (uint)EventFlags.EVENT_SET));
}
else
{
return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_SET));
}
}
internal static bool ResetEvent(IntPtr hEvent)
{
if (FullFramework)
{
return Convert.ToBoolean(WinEventModify(hEvent, (uint)EventFlags.EVENT_RESET));
}
else
{
return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_RESET));
}
}
internal static bool PulseEvent(IntPtr hEvent)
{
if (FullFramework)
{
return Convert.ToBoolean(WinEventModify(hEvent, (uint)EventFlags.EVENT_PULSE));
}
else
{
return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_PULSE));
}
}
internal static int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds)
{
if (FullFramework)
{
return WinWaitForSingleObject(hHandle, dwMilliseconds);
}
else
{
return CEWaitForSingleObject(hHandle, dwMilliseconds);
}
}
#region Helper methods
internal static bool FullFramework
{
get{return Environment.OSVersion.Platform == PlatformID.Win32Windows;}
}
#endregion
#region API Constants
internal const Int32 INVALID_HANDLE_VALUE = -1;
internal const UInt32 OPEN_EXISTING = 3;
internal const UInt32 GENERIC_READ = 0x80000000;
internal const UInt32 GENERIC_WRITE = 0x40000000;
#endregion
#region Windows CE API imports
[DllImport("coredll.dll", EntryPoint="WaitForSingleObject", SetLastError = true)]
private static extern int CEWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("coredll.dll", EntryPoint="EventModify", SetLastError = true)]
private static extern int CEEventModify(IntPtr hEvent, uint function);
[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr CECreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName);
[DllImport("coredll.dll", EntryPoint="EscapeCommFunction", SetLastError = true)]
private static extern int CEEscapeCommFunction(IntPtr hFile, UInt32 dwFunc);
[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="SetupComm", SetLastError = true)]
private static extern int CESetupComm(IntPtr hFile, UInt32 dwInQueue, UInt32 dwOutQueue);
[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, UInt32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError = true)]
private static extern int CEReadFile(IntPtr hFile, byte[] lpBuffer, UInt32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("coredll.dll", EntryPoint="SetCommMask", SetLastError = true)]
private static extern int CESetCommMask(IntPtr handle, CommEventFlags dwEvtMask);
[DllImport("coredll.dll", EntryPoint="GetCommModemStatus", SetLastError = true)]
extern private static int CEGetCommModemStatus(IntPtr hFile, ref uint lpModemStat);
[DllImport("coredll.dll", EntryPoint="ClearCommError", SetLastError = true)]
extern private static int CEClearCommError(IntPtr hFile, ref CommErrorFlags lpErrors, CommStat lpStat);
[DllImport("coredll.dll", EntryPoint="WaitCommEvent", SetLastError = true)]
private static extern int CEWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);
[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);
#endregion
#region Desktop Windows API imports
[DllImport("coredll.dll", EntryPoint="WaitForSingleObject", SetLastError = true)]
private static extern int WinWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("coredll.dll", EntryPoint="EventModify", SetLastError = true)]
private static extern int WinEventModify(IntPtr hEvent, uint function);
[DllImport("kernel32.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr WinCreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName);
[DllImport("kernel32.dll", EntryPoint="EscapeCommFunction", SetLastError = true)]
private static extern int WinEscapeCommFunction(IntPtr hFile, UInt32 dwFunc);
[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, UInt32 dwInQueue, UInt32 dwOutQueue);
[DllImport("kernel32.dll", EntryPoint="CloseHandle", SetLastError = true)]
private static extern int WinCloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", EntryPoint="WriteFile", SetLastError = true)]
extern private static int WinWriteFile(IntPtr hFile, byte[] lpBuffer, UInt32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll", EntryPoint="ReadFile", SetLastError = true)]
private static extern int WinReadFile(IntPtr hFile, byte[] lpBuffer, UInt32 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")]
extern private static int WinGetCommModemStatus(IntPtr hFile, ref uint lpModemStat);
[DllImport("kernel32.dll", EntryPoint="ClearCommError", SetLastError = true)]
extern private static int WinClearCommError(IntPtr hFile, ref CommErrorFlags lpErrors, CommStat lpStat);
[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="WaitCommEvent", SetLastError = true)]
private static extern int WinWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -