sankar012898
asked on
Modem Communication ! Urgent
I have to develop an application using MFC & VC++ & Windows APIs for communicating with the modem .If anybody has an
idea about this , please reply back with a detailed explanation .
Thank you ,
sankar
idea about this , please reply back with a detailed explanation .
Thank you ,
sankar
ASKER
Edited text of question
For Win32?
Here is some code for opening any comport and setup parameters.
nPort: 0=COM1, 1=COM2....
*************
HANDLE OpenCommPort(int nPort, int nBaudrateIdx)
{
HANDLE hPort;
CString strPort;
if(nPort<10)
strPort.Format(_T("COM%d") , nPort);
else
strPort.Format(_T("\\\\.\\ COM%d"), nPort);
// Open COM1.....COMx
hPort = CreateFile(strPort,GENERIC _READ|GENE RIC_WRITE,
0, /* comm devices must be opened w/exclusive-access */
NULL, /* no security attrs */
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
0,
NULL /* hTemplate must be NULL for comm devices */
);
if(hOPSPort == INVALID_HANDLE_VALUE)
return NULL;
// Setup timeout values for reading and writing to the port
COMMTIMEOUTS ctmo;
ctmo.ReadIntervalTimeout = 100;
ctmo.ReadTotalTimeoutMulti plier = 1;
ctmo.ReadTotalTimeoutConst ant = 2000;
ctmo.WriteTotalTimeoutMult iplier = 1;
ctmo.WriteTotalTimeoutCons tant = 1000;
SetCommTimeouts(hPort, &ctmo);
const int br[] = { 300, 1200, 2400, 4800, 9600, 14400, 19200 };
// Setup port to use CTS/RTS hardware flow control with 8 databits
// 1 stopbit and no parity.
DCB dcb;
ZeroMemory(&dcb, sizeof(dcb));
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = br[nBaudrateIdx];
dcb.fBinary = TRUE;
dcb.fParity = 0;
dcb.fOutxCtsFlow = 1;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = 0;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
if(!SetCommState(hPort, &dcb))
{
CloseHandle(hPort);
hPort = INVALID_HANDLE_VALUE;
return NULL;
}
return hPort;
}
********
To read and write to the port, just use the normal ReadFile()/WriteFile() functions
Here is an example of how to reset the modem:
BOOL ResetModem(HANDLE hPort)
{
CString sCmd = _T("Z\n");
DWORD dwIOLen;
if(WriteFile(hPort, sCmd, sCmd.GetLength(), &dwIoSize, NULL))
{
return TRUE;
}
return FALSE;
}
Off-cause you would want to check the responses from the modem when sending
commands or dialing but that is just basic Win32 IO.
nPort: 0=COM1, 1=COM2....
*************
HANDLE OpenCommPort(int nPort, int nBaudrateIdx)
{
HANDLE hPort;
CString strPort;
if(nPort<10)
strPort.Format(_T("COM%d")
else
strPort.Format(_T("\\\\.\\
// Open COM1.....COMx
hPort = CreateFile(strPort,GENERIC
0, /* comm devices must be opened w/exclusive-access */
NULL, /* no security attrs */
OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
0,
NULL /* hTemplate must be NULL for comm devices */
);
if(hOPSPort == INVALID_HANDLE_VALUE)
return NULL;
// Setup timeout values for reading and writing to the port
COMMTIMEOUTS ctmo;
ctmo.ReadIntervalTimeout = 100;
ctmo.ReadTotalTimeoutMulti
ctmo.ReadTotalTimeoutConst
ctmo.WriteTotalTimeoutMult
ctmo.WriteTotalTimeoutCons
SetCommTimeouts(hPort, &ctmo);
const int br[] = { 300, 1200, 2400, 4800, 9600, 14400, 19200 };
// Setup port to use CTS/RTS hardware flow control with 8 databits
// 1 stopbit and no parity.
DCB dcb;
ZeroMemory(&dcb, sizeof(dcb));
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = br[nBaudrateIdx];
dcb.fBinary = TRUE;
dcb.fParity = 0;
dcb.fOutxCtsFlow = 1;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = 0;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
if(!SetCommState(hPort, &dcb))
{
CloseHandle(hPort);
hPort = INVALID_HANDLE_VALUE;
return NULL;
}
return hPort;
}
********
To read and write to the port, just use the normal ReadFile()/WriteFile() functions
Here is an example of how to reset the modem:
BOOL ResetModem(HANDLE hPort)
{
CString sCmd = _T("Z\n");
DWORD dwIOLen;
if(WriteFile(hPort, sCmd, sCmd.GetLength(), &dwIoSize, NULL))
{
return TRUE;
}
return FALSE;
}
Off-cause you would want to check the responses from the modem when sending
commands or dialing but that is just basic Win32 IO.
Please refer to online manual about the following functions.
GetCommState
SetCommState
SetupComm
CreateFile
There is an example on the VC5 CD: \Devstudio\Vc\Samples\Sdk\ Win32\Comm
Theses sites can also give you some help.
http://www.qfax.com/
http://www.goodnet.com/~esnible/winsock.html
GetCommState
SetCommState
SetupComm
CreateFile
There is an example on the VC5 CD: \Devstudio\Vc\Samples\Sdk\
Theses sites can also give you some help.
http://www.qfax.com/
http://www.goodnet.com/~esnible/winsock.html
ASKER
hi piano_boxer & trestan,
Thanks for your reply . I understood your code .
Infact I have programmed the COM port myself in a similar way for communicating with Barcode readers and printers but I wanted to know how to dial a telephone number by a
program when the modem is connected to a com port and
also how to send data over telephone wires .
Thanks for your reply . I understood your code .
Infact I have programmed the COM port myself in a similar way for communicating with Barcode readers and printers but I wanted to know how to dial a telephone number by a
program when the modem is connected to a com port and
also how to send data over telephone wires .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi piano_boxer,
Thanks for your answer .
Thanks for your answer .
ASKER