📄 serialcomm.cs
字号:
uint num2 = 0;
byte[] buffer1 = new byte[1];
AutoResetEvent event1 = new AutoResetEvent(false);
this.rxOvr = new Win32Ovrlap(this.port.Handle, event1.Handle);
this.events = new Win32Events(this.rxOvr.MemPtr);
this.modem = new Win32Modem();
this.Signals();
this.recvrEvent.Set();
try
{
while (true)
{
if (this.events.Set(this.port.Handle, 0x1fd))
{
this.events.Wait(this.port.Handle, event1);
num1 = this.events.GetMask;
if ((num1 & 0x80) != 0)
{
if (!this.stats.Reset(this.port.Handle))
{
if (this.stats.Status == 0x10)
{
num1 &= 0xffffffbf;
}
else
{
this.CommError(this.stats.Fault);
}
}
}
else
{
if (((num1 & 1) != 0) && this.immediate)
{
do
{
num2 = 0;
if (this.port.Read(buffer1, 1, out num2, this.rxOvr.MemPtr) && (num2 == 1))
{
this.OnRxChar(buffer1);
}
}
while (num2 > 0);
}
if ((num1 & 4) != 0)
{
this.OnTxDone();
}
if ((num1 & 0x40) != 0)
{
this.OnBreak();
}
if ((num1 & 0x138) > 0)
{
this.modem.Get(this.port.Handle);
if ((num1 & 8) > 0)
{
this.OnCTS(this.modem.CtsState);
}
if ((num1 & 0x10) > 0)
{
this.OnDSR(this.modem.DsrState);
}
if ((num1 & 0x20) > 0)
{
this.OnRLSD(this.modem.RlsdState);
}
if ((num1 & 0x100) > 0)
{
this.OnRING(this.modem.RingState);
}
}
}
}
}
}
catch (Exception exception1)
{
if (exception1 is ThreadAbortException)
{
this.modem = null;
this.events = null;
this.rxOvr = null;
return;
}
this.CommError("Receiver Exception: " + exception1.Message);
return;
}
}
///接收串口信息,形参为out型byte数组,返回值为整型数据,返回值为均为0
public uint Recv(out byte[] b)
{
uint num1 = 0;
if (!this.portOpen)
{
this.CommError("Recv() Failed. " + this.portName + " Is Closed.");
b = new byte[1];
return num1;
}
uint num2 = this.Ready();
//不需单独调用Ready()函数,Ready函数会在接收函数中间接被调用。
if (num2 > 0)
{
b = new byte[num2];
if (!this.port.Read(b, num2, out num1, this.rxOvr.MemPtr))
{
this.CommError(this.port.Fault);
}
return num1;
}
b = new byte[1];
return num1;
}
/// <summary>
/// 重载了的接收函数,形参发生改变,为一个byte型数组,和一个uint型整数变量
/// </summary>
/// <param name="b"></param>
/// <param name="nBytes"></param>
/// <returns></returns>
public uint Recv(byte[] b, uint nBytes)
{
uint num1 = 0;
if (!this.portOpen)
{
this.CommError("Recv() Failed. " + this.portName + " Is Closed.");
return num1;
}
if ((this.Ready() >= nBytes) && !this.port.Read(b, nBytes, out num1, this.rxOvr.MemPtr))
{
this.CommError(this.port.Fault);
}
return num1;
}
/// <summary>
/// 向串口写数据,返回一uint变量,用于确定发送是否成功,形参为整形,
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
public uint Send(byte chr)
{
byte[] buffer1 = new byte[1] { chr } ;
return this.Send(buffer1);
}
///向串口写数据的重载函数,返回一uint变量,形参为一字符串
public uint Send(string s)
{
byte[] buffer1 = new byte[s.Length];
buffer1 = Encoding.ASCII.GetBytes(s);
return this.Send(buffer1);
}
/// <summary>
/// 向串口写数据的重载函数,返回一uint变量,形参为一byte型数组
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public uint Send(byte[] buffer)
{
uint num1 = 0;
if (!this.portOpen)
{
this.CommError("Send() Failed. " + this.portName + " Is Closed.");
return num1;
}
if (!this.port.Write(buffer, (uint) buffer.Length, out num1, this.txOvr))
{
this.CommError(this.port.Fault);
}
return num1;
}
/// <summary>
/// 用于发送信息E的函数,返回值BOOL型,形参为定义的Object型
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public bool SendE(ExtCodes code)
{
if (!this.portOpen)
{
this.CommError("SendE() Failed. " + this.portName + " Is Closed.");
}
else
{
if (this.escape.ExtFunc((uint) code))
{
return true;
}
this.CommError(this.escape.Fault);
}
return false;
}
/// <summary>
/// 发送信息I的函数,返回值BOOL,形参为一byte型整数
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
public bool SendI(byte chr)
{
if (!this.portOpen)
{
this.CommError("SendI() Failed. " + this.portName + " Is Closed.");
}
else
{
if (this.port.TxChar(chr))
{
return true;
}
this.CommError(this.port.Fault);
}
return false;
}
/// <summary>
/// 无返回值,信息值设置函数。
/// </summary>
public void Signals()
{
if ((this.modem != null) && (this.port != null))
{
this.modem.Get(this.port.Handle);
this.OnCTS(this.modem.CtsState);
this.OnDSR(this.modem.DsrState);
this.OnRLSD(this.modem.RlsdState);
this.OnRING(this.modem.RingState);
}
else
{
this.OnCTS(false);
this.OnDSR(false);
this.OnRLSD(false);
this.OnRING(false);
}
}
// Properties
public string Fault
{
get
{
return this.fault;
}
}
public bool IsOpen
{
get
{
return this.portOpen;
}
}
// Fields
private Win32DCB dcb;
private Win32Escape escape;
private Win32Events events;
private string fault;
private bool immediate;
private Win32Modem modem;
private Win32Com port;
private string portName;
private bool portOpen;
private Win32Props props;
private ManualResetEvent recvrEvent;
private Win32Ovrlap rxOvr;
private Thread rxThread;
private Win32Status stats;
private Win32Tmout tmout;
private Win32Ovrlap txOvr;
private ManualResetEvent writeEvent;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -