C# 串口通讯 第一次通讯时通讯不上
下面的程序是一个C#串口通讯程序,但每次重启机器时通讯不上,用其它串口测试程序打开关闭串口后通讯正常,怀疑串口通讯参数设置不对,请教如何解决
public class Comm
{
public Comm()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string PortNum;
public int BaudRate;
public byte ByteSize;
public byte Parity; // 0-4=no,odd,even,mark,space
public byte StopBits; // 0,1,2 = 1, 1.5, 2
public int ReadTimeout;
//comm port win32 file handle
private int hComm = -1;
public bool Opened = false;
public bool Readed = false;
//win32 api constants
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int FILE_ATTRIBUTE_NORMAL = 0x00000080;
private const int FILE_FLAG_OVERLAPPED = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
// 打开串口 OPEN THE COMM PORT.
hComm = CreateFile(PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,OPEN_EXISTING, 0, 0); //FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED
// 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
if(hComm == INVALID_HANDLE_VALUE)
{
throw(new ApplicationException("非法操作,不能打开串口!"));
}
// 设置通信超时时间 SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm,ref ctoCommPort);
//ctoCommPort.ReadIntervalTimeout = 2000;
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0; //1000;
ctoCommPort.WriteTotalTimeoutMultiplier = 0; //1000;
ctoCommPort.WriteTotalTimeoutConstant = 0; //2500;
SetCommTimeouts(hComm,ref ctoCommPort);
// 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate=BaudRate;
dcbCommPort.flags=0;
//dcb.fBinary=1;
dcbCommPort.flags|=1;
if (Parity>0)
{
//dcb.fParity=1
dcbCommPort.flags|=2;
}
dcbCommPort.Parity=Parity;
dcbCommPort.ByteSize=ByteSize;
dcbCommPort.StopBits=StopBits;
if (!SetCommState(hComm, ref dcbCommPort))
{
//uint ErrorNum=GetLastError();
throw(new ApplicationException("非法操作,不能打开串口!"));
}
//unremark to see if setting took correctly
//DCB dcbCommPort2 = new DCB();
//GetCommState(hComm, ref dcbCommPort2);
Opened = true;
}
public void Close()
{
if (hComm!=INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
}
}
public byte[] Read(int NumBytes)
{
byte[] BufBytes;
byte[] OutBytes;
UInt32 dwError;
COMSTAT cs;
BufBytes = new byte[NumBytes];
if (hComm!=INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead=0;
if(!ClearCommError(hComm,out dwError,out cs))
{
throw(new ApplicationException("通过清串口错误获取串口缓冲区内数据数目失败"));
}
/*if (cs.cbInQue==0) throw(new ApplicationException("缓冲区内没有数据!"));
if (cs.cbInQue>NumBytes)
{
PurgeComm(hComm,PURGE_RXCLEAR);
throw(new ApplicationException("太大"));
}*/
if (cs.cbInQue==0) return null;
ReadFile(hComm,BufBytes,NumBytes,ref BytesRead,ref ovlCommPort);
Readed = true;
//MessageBox.Show(GetLastError().ToString());
//MessageBox.Show(BytesRead.ToString(), "接收到的字节数");
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes,OutBytes,BytesRead);
}
else
{
throw(new ApplicationException("串口未打开!"));
}
return OutBytes;
}
public void Write(byte[] WriteBytes)
{
if (hComm!=INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesWritten = 0;
WriteFile(hComm,WriteBytes,WriteBytes.Length,ref BytesWritten,ref ovlCommPort);
//MessageBox.Show(BytesWritten.ToString(), "发送的字节数");
}
else
{
throw(new ApplicationException("串口未打开!"));
}
}
public void ClearBuff()
{
PurgeComm(hComm,PURGE_RXCLEAR);
}
}