Solved

List available COM ports

Posted on 1998-12-18
4
418 Views
Last Modified: 2010-04-04
I have a screen from which users select a COM port. Instead of just listing COM1... COM8 in a listbox, I would like to display available com ports only.
0
Comment
Question by:alexandram
  • 2
4 Comments
 
LVL 4

Expert Comment

by:dwwang
ID: 1351844
below is in the MSDN article "Serial Communications in Win32":

                   One thing to note about port names is that traditionally they have
                   been COM1, COM2, COM3, or COM4. The Win32 API does not provide any
                   mechanism for determining what ports exist on a system. Windows NT and
                   Windows 95 keep track of installed ports differently from one another,
                   so any one method would not be portable across all Win32 platforms.
                   Some systems even have more ports than the traditional maximum of
                   four. Hardware vendors and serial-device-driver writers are free to
                   name the ports anything they like. For this reason, it is best that
                   users have the ability to specify the port name they want to use. If a
                   port does not exist, an error will occur (ERROR_FILE_NOT_FOUND) after
                   attempting to open the port, and the user should be notified that the
                   port isn't available.

So that you should enumerate them by checking the registry:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Class\Ports, or
HKEY_LOCAL_MACHINE\Hardware\devicemap\serialcomm

However, in Win95/NT, the location might be slightly different.
0
 

Author Comment

by:alexandram
ID: 1351845
Thank you. I guess I won't bother since my application runs on both 95 and NT.
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 100 total points
ID: 1351846
from the groups (tested on D3, NT4):

procedure TForm1.Button1Click(Sender: TObject);
const
MaxPortsExpected = 8; // WIN95 max = 9; NT =256
var
HCom : THandle;
DCB  : TDCB;
PortNum : integer;
PortName: string;
begin
  for PortNum := 1 to MaxPortsExpected do
  begin
   PortName := 'COM'+IntToStr(PortNum);
   hCom := CreateFile(PChar(PortName),
                     GENERIC_READ or GENERIC_WRITE,
                     0,
                     NIL,
                     OPEN_EXISTING,
                     FILE_FLAG_OVERLAPPED,
                     0);
    if hCom = INVALID_HANDLE_VALUE then
      ShowMessage( PortName +' : not present, or already in use' )
    else
    begin
      GetCommState(hCom, DCB);
      with DCB do begin
          ShowMessage( PortName +' : available'+#13
                     +'Baud:'    + IntToStr(BaudRate) +#13
                     +'ByteSize:'+ IntToStr(ByteSize) +#13
                     + 'Parity :'+ IntToStr(Parity));
      end;
      CloseHandle( hCom );
    end;
  end;
end;

0
 

Author Comment

by:alexandram
ID: 1351847
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 79
Simple Delphi Question 9 83
How to define IfThen functions in one common unit? 4 44
update joined tables 2 31
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now