Determining Comm Ports

Does anyone have any code that can determine what
comm ports are available on a computer.  I need
the source code.  Cheers
Who is Participating?
MatthewLConnect With a Mentor Commented:
Hi scotdance;

Is this what your looking for?

#include <stdio.h>

int isComInstalled( int PortNum );

void main( void )
      if( isComInstalled( 1 ) )
            printf("COM1 installed\n");

      if( isComInstalled( 2 ) )
            printf("COM2 installed\n");

      if( isComInstalled( 3 ) )
            printf("COM3 installed\n");

      if( isComInstalled( 4 ) )
            printf("COM4 installed\n");


int isComInstalled( int PortNum )
      unsigned int * baseAddr = (unsigned int *)0x400;
      baseAddr = baseAddr + 2*(PortNum-1);
      if( *baseAddr )
            return( 1 );
            return( 0 );
For which OS?
Hi scotdance;

I agree with jkr, we need more info.  If you have an ibm pc the bios stores the base addresses at the following locations

COM port       address where base address is stored
 1                       400 (hex)
 2                       402 (hex)
 3                       403 (hex)
 4                       404 (hex)

If a com port is not installed then the base address is set to 0.

so for example (16 bit world) printf( "COM1: %x", *(unsigned int *)0x400 ); will print out the current base address for COM1.

but as I agreed we don't know what platform or O/S you are using.

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

scotdanceAuthor Commented:
It's for DOS
See my answer in C++ section.
If you feel satisfied with that answer then give me points.
I am here to give you more info about
serial ports.
scotdanceAuthor Commented:
I need the code to put in a C program to do this.  Not to use the MSD.EXE method.
If you are using DOS OS then you can find the comport by doing this small exercise instead of writing application.

short the 2 and 3 pin of the serial port.  Assume COM1

type debug at the dos prompt then
hyphen prompt will appear
type o 0378 41

if the pins are shorted ie basically you are shorting the transmit and receive pins.  so you have to get back the chr you had sent.  Basically in the above said ex you are sending 41h ie A.
if you read the port by giving this commad you should get back same A
-i 3fd

I hope this answers your query.
Hi scotdance;

What's up? If you don't give us feedback on what's wrong with our proposals we can't fix them for you.

scotdanceAuthor Commented:
Thank MatthewL for you help it was good but I've another problem to solve now you might be able to help me with.

All Courses

From novice to tech pro — start learning today.