Link to home
Start Free TrialLog in
Avatar of uri22
uri22

asked on

Assigning modem to port at win2k using createfile( ) - SOS

Hello
Im wrote a peace of code that sends/receives data using a modem.
I use to open the port was assign to the modem (COM1...)
At win2k the state is different and requires using the createFile().
Using that function requires the portName (not COM1...)
I tried to open by the name the modem ws defined but got a constant error.
Can someone send me an example of this function where the portName parameter is a modem name
Thanks
Avatar of DanRollins
DanRollins
Flag of United States of America image

It really does work exactly the way the documentation describes.  For instance, this program inits the modem and then causes it to dial a phone number:

#include <windows.h>
void main()
{
     HANDLE hCom = CreateFile(
          "COM2", // LPCTSTR lpFileName,   // file name
          GENERIC_READ | GENERIC_WRITE, // DWORD dwDesiredAccess,  // access mode
          0, // DWORD dwShareMode, // share mode
          0, // LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
          OPEN_EXISTING, // DWORD dwCreationDisposition,   // how to create
          0, // DWORD dwFlagsAndAttributes,// file attributes
          0  // HANDLE hTemplateFile   // handle to template file
     );

     char szModemInit[]= "AT&F\r";
     DWORD nActual= 0;
     BOOL fRet;
     fRet= WriteFile( hCom, szModemInit,strlen(szModemInit), &nActual, 0 );

     char buf[1000];
     fRet= ReadFile( hCom, buf, 999, &nActual, 0 );

     char szModemDial[]= "ATDT9,555-1234\r";
     fRet= WriteFile( hCom, szModemDial,strlen(szModemDial), &nActual, 0 );
}

-=-=-==-=--=
Notice that the name of the file is "COM1" or "COM2" etc (look in the Control Panel to learn the correct COMn port" and you need to use
   GENERIC_READ | GENERIC_WRITE,
and
  OPEN_EXISTING
as shown.  Note that it is possible that on your system, there is some software that already owns the COM port (eg, the system is waiting to answer an incomming Fax or data connection).  In that case, you need to disable that software ...OR... enter the ugly ugly world of TAPI because TAPI lets you share access to the modem.  TAPI also provides a way to init the modem without knowing the brand of modem, etc.  But it is ugly beyond description.

-- Dan
hi uri22,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Force/accepted by

Netminder
Community Support Moderator
Experts Exchange