Solved

List available COM ports

Posted on 1998-12-18
4
417 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

10 Experts available now in Live!

Get 1:1 Help Now