Solved

List available COM ports

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 67
drawing animated level bar based on numbers 3 104
Tvertscrollbox like a whatsapp layout 5 36
Delphi Seattle StructureView color 1 15
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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