110,566
社区成员
发帖
与我相关
我的任务
分享
//打开串口
public int Com_Open(string ComSet, int BaudRate)
{
int hCommFile;
DCB dcb = new DCB();
COMMTIMEOUTS TimeOuts = new COMMTIMEOUTS();
hCommFile = CreateFile( ComSet,
GENERIC_READ | GENERIC_WRITE,
0, //{not shared}
0, //{no security ??}
OPEN_EXISTING,
0,
0 );
if(hCommFile == INVALID_HANDLE_VALUE)
{
throw(new ApplicationException("Comm Port Can Not Be Opened"));
}
// Is this a valid comm handle?
if(GetFileType( hCommFile ) != FILE_TYPE_CHAR)
{
CloseHandle( hCommFile );
//throw(new ApplicationException("File handle is not a comm handle "));
return INVALID_HANDLE_VALUE;
}
if(!SetupComm( hCommFile, 4096, 4096 ))
{
CloseHandle( hCommFile );
//throw(new ApplicationException("Cannot setup comm buffer"));
return INVALID_HANDLE_VALUE;
}
// purge any information in the buffer
PurgeComm( hCommFile, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
// Setting the time-out value
GetCommTimeouts( hCommFile, ref TimeOuts );
// The CommTimeout numbers will very likely change if you are
// coding to meet some kind of specification where
// you need to reply within a certain amount of time after
// recieving the last byte. However, If 1/4th of a second
// goes by between recieving two characters, its a good
// indication that the transmitting end has finished, even
// assuming a 1200 baud modem.
TimeOuts.ReadIntervalTimeout=500;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=5000;
SetCommTimeouts(hCommFile, ref TimeOuts); // 设置超时
// Configure the comm settings.
// NOTE: Most Comm settings can be set through TAPI, but this means that
// the CommFile will have to be passed to this component.
GetCommState(hCommFile, ref dcb);
dcb.BaudRate = 28800;//BaudRate; // 波特率为
dcb.ByteSize=8; // 每个字符有8位
dcb.Parity = PAIRTY_EVEN; //奇偶校验
dcb.StopBits=ONESTOPBIT; //一个停止位
SetCommState(hCommFile, ref dcb);
Opened = true;
return hCommFile;
}
//写串口
public int ComReadFile(int ihCommFile, byte[] Readbuff, int ReadLen)
{
if (ihCommFile != INVALID_HANDLE_VALUE)
{
int NumberOfBytesTransferred = 0;
OVERLAPPED ovlCommPort = new OVERLAPPED();
if (ReadFile(ihCommFile, Readbuff, ReadLen, ref NumberOfBytesTransferred, ref ovlCommPort))//
{
if (ReadLen == NumberOfBytesTransferred)
{
return NumberOfBytesTransferred;
}
else return NumberOfBytesTransferred;
}
else return 0;
}
else return 0;
}
//向串口读数据
public int ComReadFile(int ihCommFile, byte[] Readbuff, int ReadLen)
{
uint dwStart=0, dwStop=0;
if (ihCommFile != INVALID_HANDLE_VALUE)
{
int NumberOfBytesTransferred = 0;
OVERLAPPED ovlCommPort = new OVERLAPPED();
COMSTAT ComStat = new COMSTAT();
dwStart = GetTickCount();
dwStop = dwStart;
while (dwStop - 200 < dwStart) //相当于等待超时,最大等待800
{
int dwErrorFlags = 0;
ClearCommError(ihCommFile, ref dwErrorFlags, ref ComStat);
if (dwErrorFlags != 0)
PurgeComm(ihCommFile, PURGE_RXCLEAR | PURGE_RXABORT);
if (ComStat.cbInQue > 0)
break;
dwStop = GetTickCount();
}
if (ComStat.cbInQue > 0)
{
if (ReadFile(ihCommFile, Readbuff, ReadLen, ref NumberOfBytesTransferred, ref ovlCommPort))//
{
if (ReadLen == NumberOfBytesTransferred)
{
return NumberOfBytesTransferred;
}
else return NumberOfBytesTransferred;
}
else return 0;
}
else return 0;
}
else return 0;
}