Solved

List available COM ports

Posted on 1998-12-18
4
419 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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