void scan_all_uart(u8 *com_port_table)
{
int i;
HANDLE m_hComm;
DWORD dwCreateProperty;
//Handle to the comms port
int nPort = 0x00;
DWORD dwError = 0x00;
//Call CreateFile to open up the comms port
CString sPort;
dwCreateProperty = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED;
for( i=0x00; i<50; i++ ){
sPort.Format(_T("\\\\.\\COM%d"), nPort);
m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwCreateProperty, NULL);
if( m_hComm == INVALID_HANDLE_VALUE ){
dwError = ::GetLastError();
if( dwError == 2 ){
//机器上没有这个串口
}else if( dwError == 5 ){
TRACE("nPort1 = %d.\n", nPort);
com_port_table[nPort] = 0x02;
//有这个串口,但是已经被打开
}
}else{
//能够正常打开
TRACE( "m_hComm = 0x%x.\n", m_hComm );
TRACE("nPort2 = %d.\n", nPort);
com_port_table[nPort] = 0x01;
BOOL bSuccess = CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
if( !bSuccess ){
TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
}
}
nPort++;
}
}
void Cfile_extractDlg::OnBnClickedButton10()
{
int i;
u8 com_port_table[50];
memset(com_port_table,0x00,sizeof(com_port_table));
scan_all_uart(com_port_table);
for(i=0x00; i<50; i++){
if(com_port_table[i]){
TRACE("com_port_table[%d] = %d.\n", i, com_port_table[i]);
}
}
}