C#串口实时监听
因为是搞电控的,但是项目要用C#,在winCE下运行,在网上下了段API 读写串口的程序,打开串口和写串口都没问题;希望串口有数据时可以实时监听到,但是不知道怎么写实时监听串口的部分,请大家帮帮忙,
一,串口操作类主要内容
public bool OpenPort(string lpFileName,int baudRate,byte parity, byte byteSize, byte stopBits)
{
// OPEN THE COMM PORT.
hComm = CreateFile(lpFileName ,GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
// IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE)
{
return false;
}
SetupComm(hComm, MAXBLOCK, MAXBLOCK);
// SET THE COMM TIMEOUTS.
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
GetCommTimeouts(hComm,ref ctoCommPort);
ctoCommPort.ReadIntervalTimeout = Int32.MaxValue;
ctoCommPort.ReadTotalTimeoutConstant = 1000;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = -1;
ctoCommPort.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hComm,ref ctoCommPort);
DCB dcbCommPort = new DCB();
dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = baudRate;
dcbCommPort.Parity = parity;
dcbCommPort.fBinary = 0;
dcbCommPort.fParity = 0;
dcbCommPort.ByteSize = byteSize;
dcbCommPort.StopBits = stopBits;
dcbCommPort.fOutxCtsFlow = 0;
dcbCommPort.fOutxDsrFlow = 0;
dcbCommPort.fRtsControl = 1;
dcbCommPort.fDtrControl = 1;
SetCommState(hComm, ref dcbCommPort);
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT);
bOpened = true;
return true;
}
// 关闭串口
public bool ClosePort()
{
if (hComm == INVALID_HANDLE_VALUE)
{
return false;
}
if (CloseHandle(hComm))
{
hComm = INVALID_HANDLE_VALUE;
bOpened = false;
return true;
}
else
{
return false;
}
}
// 往串口写数据
public bool WritePort(byte[] WriteBytes,int dwLen)
{
if (hComm == INVALID_HANDLE_VALUE)
{
return false;
}
COMSTAT ComStat = new COMSTAT();
int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat);
if (dwErrorFlags != 0)
PurgeComm(hComm, PURGE_TXCLEAR | PURGE_TXABORT);
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
return WriteFile(hComm, WriteBytes, dwLen, ref BytesWritten, ref ovlCommPort);
}
// 从串口读数据
public int ReadPort(int NumBytes, byte[] commRead)
{
if (hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
COMSTAT ComStat = new COMSTAT();
int dwErrorFlags = 0;
ClearCommError(hComm, ref dwErrorFlags, ref ComStat);
if (dwErrorFlags != 0)
{
PurgeComm(hComm, PURGE_RXCLEAR | PURGE_RXABORT);
}
if (ComStat.cbInQue > 0)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
ReadFile(hComm, commRead, NumBytes, ref BytesRead, ref ovlCommPort);
return BytesRead;
}
else
{
return 0;
}
}
}
}
二,主界面程序 主要程序
namespace CommPort
{
public class Form1 : System.Windows.Forms.Form
{
private Class1 cl1 = new Class1();
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
cl1.OpenPort("COM2:",115200,0,8,1);
MessageBox.Show("open");
}
catch
{
MessageBox.Show("error");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
byte [] byread = new byte [100];
port.Read(ref byread,100);
}
private void button3_Click(object sender, System.EventArgs e)
{
string ss = "中国电子器件\n";
wdata = System.Text.Encoding.Default.GetBytes(ss);
cl1.WritePort(wdata, wdata.Length);
}
}
}
麻烦大家帮忙