I have been doing automatic control for nearly 20 years, and I cannot do without serial communication. Originally, when it was necessary to obtain the serial port of the computer, the method of reading the registry was used, which has always been used normally. Unless computer system problems cause redundant virtual serial ports or usb serial port numbers, just clean up the registry:
Description: The real serial port of my computer is com1, and the virtual serial port com8---14:
Download address : BCB serial port enumeration
It seems that it is not very noble to carry others, and I created my own method of sorting com numbers. Because the serial port numbers enumerated are not arranged according to com1/2/3----from small to large, you have to process them yourself.
My own method is the container, which releases the source program that has been enumerated and sorted using the registry:
#include <Registry.hpp> #include <vector> using namespace std; vector <int> com; TStringList* List=new TStringList(); TRegistry *reg = new TRegistry; reg->RootKey = HKEY_LOCAL_MACHINE; reg->OpenKey("HARDWARE\\DEVICEMAP\\SERIALCOMM",true); reg->GetValueNames(List); ComboBox1->Items->Clear(); for(int i=0;i<List->Count;i++) { com.push_back((reg->ReadString(List->Strings[i])).Delete(1,3).ToInt()); } sort (com.begin (), com.end ()); for(unsigned int i=0;i<com.size();i++) { ComboBox1->Items->Add("COM"+IntToStr(com[i])); } delete reg; delete List; ComboBox1->ItemIndex = 0;
No comments, no extra words.
Salute to colleagues who are still sticking to bcb6.0!