Serial communications: a 32bit modem dialer

Back Up Next

Author: Eric Aling
Accessed:

With the 16bit PB on Win3.x, accessing your communication ports was quite simple. With Win95 en WinNT, this is quite different because these operation systems are handling communication ports in a other way, namely as files. Herefore, you have to use standard Win32 API functions. Another way is not possible. Secondly, because communication ports are 'strange' files, you have to handle them in a special way. First of all, here are the needed Win32 API functions:

/* To create a file */
function long CreateFileA( 
ref string lpszName,
long fdwAccess,
long fdwShareMode, 
long lpsa, 
long fdwCreate,
long fdwAttrsAndFlags,
long hTemplateFile ) library "kernel32.dll"
/* to write to the file */
function boolean WriteFile(
long hFile,
ref string lpBuffer,
long nNumberOfBytesToWrite,
ref long lpNumberOfBytesWritten,
st_overlapped lpOverlapped ) library "kernel32.dll"
/* To close the file */
function long GetLastError() library "kernel32.dll"
/* To get error information */
function boolean CloseHandle(long hObject ) library "kernel32.dll"
st_overlapped is a structure we need when writing to thecommunication ports. It is defined as follows:
$PBExportHeader$st_overlapped.srs
global type st_overlapped from structure
long Internal
long Internalhigh
long offset
long offsethigh
long hevent
end type
Now, the dialing itself is actually quite simple, you only have to know how to do it. This is quite good explained in the Win32 API helpfile that comes with the Watcom C++ compiler, which also comes as C++ Class stuff of PowerBuilder. Here's the code to dial a number:
/* declare the needed variables */
long ll_comid
long lnull
st_overlapped lst_overlapped
long ll_written
string ls_Port
string ls_Number
string ls_CRLF = "~r~n"
/* some constant, see helpfile for more information */
long GENERAL_WRITE = 1073741824
long SHARE_MODE = 0
long OPEN_EXISTING = 3
long FILE_FLAG_OVERLAPPED = 1073741824
/* port and number to dial */
ls_Port = "COM2"
ls_Number = "ATDT 0306090146" + ls_CRLF
setnull(lnull)
/* open the port by creating the 'file' */
ll_Comid = CreateFileA(ls_port,GENERAL_WRITE,SHARE_MODE,lnull,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,lnull)
IF ll_ComId >= 0 THEN
/* write the number to the port */
writefile(ll_ComId, ls_Number,len( ls_Number),ll_written, lst_overlapped)
messagebox("Yo!","Press Enter To Disconnect")
writefile(ll_ComId, ls_CRLF,len(ls_CRLF),ll_written, lst_overlapped)
ELSE
/* display error */
messagebox(string(ll_Comid),getlasterror())
END IF
/* close always */
closehandle(ll_ComId)

That's all there is to it!!!

Back Up Next