Solved

List available COM ports

Posted on 1998-12-18
4
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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