Win98 TAPI's modem programming

xiaoxiao2021-03-06  40

I. Introduction:

With the popularity of the Win98 operating system, communication between computers has become the requirements of most application development, which is the main reason is that Win98 can support most hardware, but also provide convenient programming interfaces for hardware operations. API), the communication means provided by the Win98 application interface (API) is roughly divided into the following categories: 1 WINSOCKAPI based on TCP / IP protocol, microcomputer communication on the LAN or the Internet can be implemented; 2 Processed communication technology : Dynamic Data Exchange (DDE); 3 Based on the communication technology of direct cable connections, the serial port, parallel port, and far infrared interface can be directly operated; 4 can be easily controlled based on telephone line communication application interface (TAPI / TELEPHONYAPI) Modem; from the current development status, the modem has become an important tool for long-distance communication. For this purpose, this Microsoft and Intel have developed a programming interface such as TAPI, and the program segment prepared using the API function applies to BorlandC . The compiler, and also inserted into the Visualc program, as a Win98 application staff, learn to communicate with the TAPI programming operation modem via telephone lines to communicate this technology is necessary. Below TAPI programming:

Second, the communication process description

1 Initialization line (communication should be initialized)

By using the LineInitialize function to initialize Tapi.dll to get the pointer HTAPI using the handle, pay attention to the definition of the callback function in the parameter (all the functions of the function can be obtained from the help of BC 5.0 and Visualc 5.0; Call the LineOpen function (parameter HTAPI) to get the line handle Hline; use the LinegetID (with parameter hline) to get the modem handle HMODEM

2 Configure the line (optional)

Call SetCommconfig (HMODEM) Change the settings of the modem

3 dial (executed by the call)

Use the linemakecall function (with hline) to dial, after completion, obtain the call handle Hcall (caller's call handle)

4 answer link (executed by the caller)

When the callback function of the called call gets the LineCallState_offering message, call the Lineanswer function to implement an automatic answer (the call handle HCALL is given by the parameters of the callback function)

5 Data Communication (both sides)

When the callback function receives the LineCallState_Connected message, first clear the receiving buffer, you can use the function to exchange data for the WriteFile and ReadFile function, pay attention to the parameter HFile to the modem handle HMODEM

6 hang up (one side)

If the communication is completed, it can call the function literal, null, 0) to stop the call, which also sends a linecallState_idle message to the callback function.

7 turn off the line (both sides)

The callback function of both parties should call the function linedeallocatecall (hcall) to release the resources of the corresponding call, when the callback function receives the lineCallState_Disconnected message, use lineclose (hline) to release the resources allocated by LineOpen, call LINESHUTDOWN ( HTAPI) Release the resources allocated by line equipment, hardware and software environment

The figure below shows the location of our application and the hardware and software environment involved:

Our communication applications operate via TAPI operation MODEM dial, response, link, hang-up control phone call, when developing DOS applications, we often use Hayes-compatible AT command sets to complete these operations, due to each modem manufacturer's command set They have made their own extensions, and thus, our DOS app can only operate a small part of the modem, and each manufacturer provides a Windows driver, so the application using TAPI can operate most of the modems; The communication API is a programming interface for the application to send and receive data.

Fourth, program process structure block diagram

Since Win98 is a multi-task operating system, our flowchart can only represent the execution of this application, the wait and detection in the program is actually waiting for the message provided by Win98, so it does not occupy the CPU time, in the following program As can be seen. In addition, the protocol of data exchange can be set by themselves, or the existing protocol can also be used.

V. Software

Since the framework of Windows programming is basically the same, we only describe a part of the source program involving communication:

1 should include:

#include

Please note that the properties of the project file should be Windows32-bit applications.

2 Some global variables involved in communication and type definitions:

CharRecbuf [20], BUF [20] // Buffer

DWORDERROR; / / error code

COMSTATSTATUS; / / Status Code

DWordNumline; // Allowable line device number

LineCallParamspara; // Call parameters

TMYDECFRAME * PWIN = NULL; // Main window pointer

HlineAppMyhtapi; // Line Application Handle

HlineMyhline; // Line handle

HandleMyhmode; // modem handle

Hcallmyhcall; // Call handle

Typedefstructtagmodemid {

Handlehmodem;

Charmodemname [1];

} Modemid;

3 Next to get the function definition of the modem handle

Because each modem is a sign string length, the function is used in the function of variable length, the processing method is to assign the SizeOf (Varstring) size space for the string pointer, and then use the space to hold the calling LinegetID Windows. The information returned, determines the required space size to reassign the space according to the return information, and then call the LineGetID again to get a complete logo string.

VoidgetHmodem (Hlinehline)

{MODEMIDFAR * MID;

Varstring * STR;

Longlid;

DWORDSIZE;

Charmark = 1;

Str = (varstring *) malloc (sizeof (varstring));

IF (! STR)

ReturnNun;

Str-> dwtotalsize = sizeof (varstring);

DO

{IF (LinegetId (Myhline, 0, Null, LineCallselect_Line, Str,

"Comm / Datamode") == 0) && (str-> dwtotalsizedwneedededsize))

{dwsize = str-> dwneededsize; free (str);

Str = (Varstring *) Malloc (dwsize);

IF (! STR)

{MyHmodem = NULL;

Mark = 2;

}

Str-> dwtotalsize = dwsize;

}

Elsemark = 0;

} while (mark == 1);

IF (Mark == 0)

{MID = (Modemidfar *) ((LPSTR) STR STR-> DWSTRINGOFFSET);

Myhmodem = mid-> hmodem;

}

Free (STR);

}

4 Add to the initialization process of the line in the main window initialization function:

PWIN = this; // Get the main window pointer

While (LineInitialize (& myhtapi, getModule () -> getInstance (),

(LINECALLBACK) MakeProcinstance (FarProc) lpfncallback,

GetModule () -> getInstance ()), "tryerr_reinit) == lineERR_REINIT

{SLEEP (1); // delay};

Error = lineopen (htapi, 0, & hline, 0x10004, 0, 0, linecallprivilege_monitor

LineCallPrivilege_owner, linemediamode_datamodem, null;

IF (Error! = 0)

{Sprintf (buf, "% lx", error);

Messagebox (buf, 0, mb_ok);

Else

{Gethmodem (myhline); // Get the value of MyHMODEM

IF (MyHMode! = NULL)

{Para.dwbearermode = linebearermode_voice;

Para.dwmediamode = linemediamode_datamode;

Para.dwtotalsize = SizeOf (LineCallParams);

Error = linemakecall (Myhline, & myhcall, "8880751", 0 ,?);

IF (Error! = 0)

{Sprintf (buf, "% lx", error);

Messagebox (buf, 0, mb_ok);

}

}

}

5 Definition of the call party callback function

VoidfarpascaltMyDecframe :: lpfncallback

(DWORDHDEVICE, DWORDDWMSG,

DWORDDWCALLBACKINSTANCE,

DWORDDWPARAM1, DWORDDWPARAM2,

DWORDDWPARAM3) //

Parameter defines the parameter definitions in the lineCallbackFunc function

{intRec_num = 0;

Switch (dwparam1)

{CaselineCallState_Connected:

DWordlen;

Clearcommerror (MyHModem, & Error, & status);

REC_NUM = status.cbinque;

Readfile (MyHMODEM, RECBUF, REC_NUM, & LEN, 0);

/ / It has been prepared for data communication to data communication, which can set a sign

Writefile (MyHModem, "Success", 7, & len, 0);

Readfile (MyHMODEM, RECBUF, 8, & LEN, 0);

PWIN-> MessageBox (RECBUF, 0, MB_OK); Break;

CaselineCallState_Idle:

LinedeallocateCall (MyHCALL);

Break;

CaselineCallState_disconnected:

Lineclose (MyHline);

LINESHUTDOWN (MyHTAPI);

Break;

}

}

6 Definition of the called party callback function

VoidfarpascaltmyDecframe :: lpfncallback (DWORDHDEVICE, DWORDDWMSG,

DWORDDWCALLBACKINSTANCE, DWORDDWPARAM1, DWORDDWPARAM2,

DWORDDWPARAM3)

{intRec_num = 0;

Switch (dwparam3)

{CaselineCallPrivileGe_owner:

Myhcall = (hcall) HDEvice;

Break;

} // Only a caller with a private privilege to call, you will answer the call.

Call handle here

Switch (dwparam1)

{CaselineCallState_Connected:

DWordlen;

Clearcommerror (MyHModem, & Error, & status);

REC_NUM = coms.cbinque;

Readfile (MyHMODEM, RECBUF, REC_NUM, & LEN, 0); / / Clear Receive Buffer

Readfile (MyHMODEM, RECBUF, 7, & LEN, 0);

Writefile (MyHModem, "Received", 8, & len, 0);

PWIN-> MessageBox (RECBUF, 0, MB_OK);

Break;

CaselineCallste_offering:

Lineanswer (MyHCall, NULL, 0);

Break; // Complete an automatic answer

CaselineCallState_Idle:

LinedeallocateCall (MyHCALL);

Break;

CaselineCallState_disconnected:

Lineclose (MyHline);

LINESHUTDOWN (MyHTAPI);

Break;

}

}

Sixth, improvement measures

The above program is used in synchronous read and write mode, as long as Writefile or ReadFile does not complete the specified I / O task, they will not return the process, in many cases, this is a waste of CPU time; improvement It is the use of the ClearcomMerror function to determine how many bytes in the serial port buffer in the system before reading, and the write mode uses an asynchronous mode, first define an Overlapped structure, which is obtained from BC 5.0. The structure is defined as follows:

Typedefstruct_overlapped {// o

DWordInternal;

DWordInternalHigh;

DWORDOFFSET;

DWORDOFFSETHIGH;

Handlehevent;

Overlapped;

We define overlappedmyovlp;

We only use the HEVENT members, other members are set; HEVENT is set to the event handle generated by CreateEvent (NULL, TRUE, FALSE, NULL); then call Writefile (MyHMode, "Received", 8, & Len, & Myovlp) ;

转载请注明原文地址:https://www.9cbs.com/read-54361.html

New Post(0)