📄 win32events.cs
字号:
namespace SerialPorts
{
using System;
using System.Runtime.InteropServices;
using System.Threading;
internal class Win32Events
{
// Methods
internal Win32Events(IntPtr ol)
{
this.olPtr = ol;
this.getMask = 0;
this.setMask = 0;
this.evPtr = Marshal.AllocHGlobal(Marshal.SizeOf(this.getMask));
Marshal.WriteInt32(this.evPtr, 0);
}
~Win32Events()
{
Marshal.FreeHGlobal(this.evPtr);
}
internal bool Get(IntPtr handle)
{
if (!Win32Events.GetCommMask(handle, out this.evPtr))
{
int num1 = Marshal.GetLastWin32Error();
this.fault = "GetCommMask() Failed. System Returned Error Code: " + num1.ToString();
return false;
}
this.getMask = (uint) Marshal.ReadInt32(this.evPtr);
return true;
}
[DllImport("kernel32.dll")]
private static extern bool GetCommMask(IntPtr hFile, out IntPtr lpEvtMask);
internal bool Set(IntPtr handle, uint watchEvents)
{
if (!Win32Events.SetCommMask(handle, watchEvents))
{
int num1 = Marshal.GetLastWin32Error();
this.fault = "SetCommMask() Failed. System Returned Error Code: " + num1.ToString();
return false;
}
Marshal.WriteInt32(this.evPtr, 0);
this.setMask = watchEvents;
return true;
}
[DllImport("kernel32.dll")]
private static extern bool SetCommMask(IntPtr hFile, uint dwEvtMask);
internal bool Wait(IntPtr handle, AutoResetEvent signal)
{
if (!Win32Events.WaitCommEvent(handle, this.evPtr, this.olPtr))
{
int num1 = Marshal.GetLastWin32Error();
if (num1 == 0x3e5)
{
signal.WaitOne();
}
else
{
this.fault = "WaitCommEvent() Failed. System Returned Error Code: " + num1.ToString();
return false;
}
}
this.getMask = (uint) Marshal.ReadInt32(this.evPtr);
return true;
}
[DllImport("kernel32.dll", SetLastError=true)]
private static extern bool WaitCommEvent(IntPtr hFile, IntPtr lpEvtMask, IntPtr lpOverlapped);
// Properties
internal string Fault
{
get
{
return this.fault;
}
}
internal uint GetMask
{
get
{
return this.getMask;
}
set
{
this.getMask = value;
}
}
internal uint SetMask
{
get
{
return this.setMask;
}
set
{
this.setMask = value;
}
}
// Fields
internal const uint EV_BREAK = 0x40;
internal const uint EV_CTS = 8;
internal const uint EV_DEFAULT = 0x1fd;
internal const uint EV_DSR = 0x10;
internal const uint EV_ERR = 0x80;
internal const uint EV_MODEM = 0x138;
internal const uint EV_RING = 0x100;
internal const uint EV_RLSD = 0x20;
internal const uint EV_RXCHAR = 1;
internal const uint EV_RXFLAG = 2;
internal const uint EV_TXEMPTY = 4;
private IntPtr evPtr;
private string fault;
private uint getMask;
private IntPtr olPtr;
private uint setMask;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -