bool TForm1 :: OpenPorts()
{
//打开并设置串口
//如果打开成功:返回true,否则返回false;
//如果打开成功:m_hPortsHandle含有一有效值,否则为m_hPortsHandle为NULL;
m_hPortsHandle = CreateFile(m_strPortsName.c_str(),
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING ,
FILE_FLAG_OVERLAPPED , //
NULL
);
if( m_hPortsHandle == INVALID_HANDLE_VALUE)
{
m_hPortsHandle = NULL;
return false;
}
if( SetupComm(m_hPortsHandle,1200,128) == 0)
{
Application -> MessageBox("SetupComm Failed!","提示",MB_OK);
CloseHandle(m_hPortsHandle);
m_hPortsHandle = NULL;
return false;
}
if( PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR) == 0)
{
Application -> MessageBox("PurgeComm() failed!","OpenPorts()",MB_OK);
CloseHandle(m_hPortsHandle);
m_hPortsHandle = NULL;
return false;
}
DCB Dcb ;
if( GetCommState(m_hPortsHandle,&Dcb) == FALSE)
{
Application -> MessageBox("GetCommState() failed","OpenAndSetCom()",MB_OK);
CloseHandle(m_hPortsHandle);
m_hPortsHandle = NULL;
return false;
}
// 设置串口参数
Dcb. DCBlength = sizeof(DCB);
Dcb . BaudRate = m_dwBaudRate;
Dcb . ByteSize = m_byByteSize;
Dcb . StopBits = m_byStopBits;
Dcb . Parity = m_byParity; // Even
if( SetCommState(m_hPortsHandle,&Dcb) == 0)
{
Application -> MessageBox("SetCommState() failed!","OpenAndSetCom()",MB_OK);
CloseHandle(m_hPortsHandle);
m_hPortsHandle = NULL;
return false;
}
return true;
}
bool TForm1 :: RetrieveData( unsigned char *Command, int CommandLength,
unsigned char *Answer, int AnswerLength)
{
//向软件狗中发送取数据命令.
if( m_hPortsHandle == NULL )
{
return false;
}
OVERLAPPED MyOverLapped;
MyOverLapped.hEvent = CreateEvent( NULL, TRUE,FALSE,NULL);
MyOverLapped.Offset = 0;
MyOverLapped.OffsetHigh = 0;
if( MyOverLapped.hEvent == NULL)
{
return false;
}
if( PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR) == 0)
{
Application -> MessageBox("PurgeComm() failed!","OpenPorts()",MB_OK);
CloseHandle( MyOverLapped.hEvent);
return false;
}
DWORD dwWrittenLength;
BOOL bWriteResult = WriteFile(m_hPortsHandle,Command,CommandLength,
&dwWrittenLength, &MyOverLapped);
if( bWriteResult == FALSE )
{
DWORD dwLastError = GetLastError();
if( dwLastError == ERROR_IO_PENDING )
{
DWORD dwWaitResult = WaitForSingleObject( MyOverLapped.hEvent,10000); //最多等待10秒钟
if( dwWaitResult == WAIT_FAILED ||
dwWaitResult == WAIT_ABANDONED ||
dwWaitResult == WAIT_TIMEOUT)
{
PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle( MyOverLapped.hEvent);
return false;
}
ResetEvent( MyOverLapped.hEvent ); //复位事件,以备ReadFile中使用
}
else
{
PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(MyOverLapped.hEvent);
return false;
}
}
// 从软件狗中取回数据
DWORD dwReadLength;
MyOverLapped . Offset = 0;
MyOverLapped . OffsetHigh = 0;
BOOL bReadResult = ReadFile( m_hPortsHandle,
Answer,
AnswerLength,
&dwReadLength,
&MyOverLapped
);
if( bReadResult == FALSE )
{
DWORD dwLastError = GetLastError();
if( dwLastError == ERROR_IO_PENDING )
{
DWORD dwWaitResult = WaitForSingleObject(MyOverLapped.hEvent,10000);
if( dwWaitResult == WAIT_FAILED || dwWaitResult == WAIT_ABANDONED ||
dwWaitResult == WAIT_TIMEOUT)
{
PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle( MyOverLapped.hEvent);
return false;
}
}
else
{
PurgeComm(m_hPortsHandle ,PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle( MyOverLapped.hEvent);
return false;
}
}
CloseHandle( MyOverLapped . hEvent);