Board index » cppbuilder » serial ports
Russell G
![]() CBuilder Developer |
Russell G
![]() CBuilder Developer |
serial ports2004-08-07 01:03:44 AM cppbuilder35 I have an app that will let the user assign hardware to different serial ports. I just want to to find out what serial ports Windows thinks it has. What is the best way to do this? Russ G |
Stephan
![]() CBuilder Developer |
2004-08-07 01:29:58 AM
Re:serial ports
Hi
Having played with a couple of enumerators, and versions of windows, here's a simple and most robust version I have found // Search for existant com ports TStringList * results = new TStringList; results->Clear(); // make sure it's empty before using enumerate( results ); // Build the menu choices for( int index = 0; index < results->Count; index++ ) { TMenuItem *NewMenuItem = new TMenuItem(Options); NewMenuItem->Caption = results->Strings[index]; NewMenuItem->Name = results->Strings[index]; NewMenuItem->OnClick = COMPortClick; Options->Add( NewMenuItem ); }; results->Clear(); delete results; void __fastcall TMainForm::enumerate( TStringList * results ) { AnsiString PortName; for ( int i = 1; i < 256; i++ ) { //Form the Raw device name PortName.printf("COM%i", i); //Try to open the port BOOL bSuccess = FALSE; HANDLE hPort = ::CreateFile(PortName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (hPort == INVALID_HANDLE_VALUE) { DWORD dwError = GetLastError(); //Check to see if the error was because some other app had the port open or a general failure if (dwError == ERROR_ACCESS_DENIED || dwError == ERROR_GEN_FAILURE || dwError == ERROR_SHARING_VIOLATION) bSuccess = TRUE; } else { //The port was opened successfully bSuccess = TRUE; //Don't forget to close the port, since we are going to do nothing with it anyway CloseHandle(hPort); } //Add the port number to the array which will be returned if (bSuccess) results->Add(PortName); //results->Add(PortName); } } Have Fun Stephan "Russell G" < XXXX@XXXXX.COM >wrote in message QuoteI have an app that will let the user assign hardware to different serial |