函数要实现的功能:
先初始化串口,
在初始化MODEM:
对MODEM换码 +++
发送AT命令,设置MODEM 为自动应答状态。
欢迎高手相助
function initModem:boolean; //初始化MODEM
var
commtimeouts: TCommTimeouts;
dcb: Tdcb;
commportName,str: string;
ResultSet: BOOL;
initstr:pchar;
initstrlen:integer;
nWrite:dword;
hcommfile:thandle;
begin
//commportName:='COM1';
//open com
nwrite:=0;
hCommFile:=createFile('COM1',GENERIC_WRITE or generic_read,0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
if hcommFile=INVALID_HANDLE_VALUE then
messagebox(0,'打开串口错误!','注意',MB_OK);
//设置缓冲区大小
setupComm(hCommfile,1024,1024);
if GetFileType( hCommFile ) <> FILE_TYPE_CHAR then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '指定设备不是串口设备。 ' );
exit;
end;
//commportName := commportName+':9600,n,8,1';
//获得DCB的初始值
GetCommState(hCommfile,dcb);
//设置DCB的属性
FillMemory(@Dcb,sizeof(TDcb),0);
Dcb.DCBlength := sizeof(TDcb);
//BuildCommDCB(PChar(commportName),dcb); dcb.BaudRate := CBR_9600;
dcb.Parity := NOPARITY;
dcb.ByteSize := 8;
dcb.StopBits := ONESTOPBIT;
dcb.XonLim:=512; //XON阕值
dcb.XoffLim:=512; //XOFF阕值
dcb.wReserved1:=0; //保留,当前未用,必须置0。
//dcb.Flags :=1; //其它标志位
EscapeCommFunction(hcommfile,setrts);
EscapeCommFunction(hcommfile,setdtr); ResultSet := SetCommState( hCommFile, dcb );
if not ResultSet then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '不能设置串口参数。' );
exit;
end;
// 设置读超时,当两个字节的数据传送间隔超过250毫秒时读返回
// 如果通讯控制器未能连续的发送,增加这个数值
//获得通讯超时的缺省值
GetCommTimeouts(hCommFile,commtimeouts );
// 设置读超时,当两个字节的数据传送间隔超过250毫秒时读返回(5个变量)
// 如果通讯控制器未能连续的发送,增加这个数值
commtimeouts.ReadIntervalTimeout := 250;//FIntervalTimeout;
commtimeouts.ReadTotalTimeoutMultiplier := 0;
commtimeouts.ReadTotalTimeoutConstant := 0;
commtimeouts.WriteTotalTimeoutMultiplier := 0;
commtimeouts.WriteTotalTimeoutConstant := 0;
ResultSet := SetCommTimeouts( hCommFile, commtimeouts );
if not ResultSet then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '不能设置串口超时。' );
exit;
end;
//清缓冲区;
PurgeComm( hcommfile, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR ) ; str:='+++'#13;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
showmessage('kk');
//换码
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg');
result:=true;
end
else
begin
result:=false;
showmessage('sb');
//exit;
end; //初始化
//str:='ATE1M0Q0S0=1X4&C1&D3&K0S0'+chr(13)+chr(10);
//str:='ATE0V1Q0X4&D2S0=1&K0'+#13#10;
str:='ATE1S0=1'+#13#10;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg2');
result:=true;
end
else
begin
result:=false;
showmessage('sb2');
exit;
end;end;
先初始化串口,
在初始化MODEM:
对MODEM换码 +++
发送AT命令,设置MODEM 为自动应答状态。
欢迎高手相助
function initModem:boolean; //初始化MODEM
var
commtimeouts: TCommTimeouts;
dcb: Tdcb;
commportName,str: string;
ResultSet: BOOL;
initstr:pchar;
initstrlen:integer;
nWrite:dword;
hcommfile:thandle;
begin
//commportName:='COM1';
//open com
nwrite:=0;
hCommFile:=createFile('COM1',GENERIC_WRITE or generic_read,0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
if hcommFile=INVALID_HANDLE_VALUE then
messagebox(0,'打开串口错误!','注意',MB_OK);
//设置缓冲区大小
setupComm(hCommfile,1024,1024);
if GetFileType( hCommFile ) <> FILE_TYPE_CHAR then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '指定设备不是串口设备。 ' );
exit;
end;
//commportName := commportName+':9600,n,8,1';
//获得DCB的初始值
GetCommState(hCommfile,dcb);
//设置DCB的属性
FillMemory(@Dcb,sizeof(TDcb),0);
Dcb.DCBlength := sizeof(TDcb);
//BuildCommDCB(PChar(commportName),dcb); dcb.BaudRate := CBR_9600;
dcb.Parity := NOPARITY;
dcb.ByteSize := 8;
dcb.StopBits := ONESTOPBIT;
dcb.XonLim:=512; //XON阕值
dcb.XoffLim:=512; //XOFF阕值
dcb.wReserved1:=0; //保留,当前未用,必须置0。
//dcb.Flags :=1; //其它标志位
EscapeCommFunction(hcommfile,setrts);
EscapeCommFunction(hcommfile,setdtr); ResultSet := SetCommState( hCommFile, dcb );
if not ResultSet then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '不能设置串口参数。' );
exit;
end;
// 设置读超时,当两个字节的数据传送间隔超过250毫秒时读返回
// 如果通讯控制器未能连续的发送,增加这个数值
//获得通讯超时的缺省值
GetCommTimeouts(hCommFile,commtimeouts );
// 设置读超时,当两个字节的数据传送间隔超过250毫秒时读返回(5个变量)
// 如果通讯控制器未能连续的发送,增加这个数值
commtimeouts.ReadIntervalTimeout := 250;//FIntervalTimeout;
commtimeouts.ReadTotalTimeoutMultiplier := 0;
commtimeouts.ReadTotalTimeoutConstant := 0;
commtimeouts.WriteTotalTimeoutMultiplier := 0;
commtimeouts.WriteTotalTimeoutConstant := 0;
ResultSet := SetCommTimeouts( hCommFile, commtimeouts );
if not ResultSet then
begin
CloseHandle(hCommFile);
hCommFile := INVALID_HANDLE_VALUE;
ShowMessage( '不能设置串口超时。' );
exit;
end;
//清缓冲区;
PurgeComm( hcommfile, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR ) ; str:='+++'#13;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
showmessage('kk');
//换码
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg');
result:=true;
end
else
begin
result:=false;
showmessage('sb');
//exit;
end; //初始化
//str:='ATE1M0Q0S0=1X4&C1&D3&K0S0'+chr(13)+chr(10);
//str:='ATE0V1Q0X4&D2S0=1&K0'+#13#10;
str:='ATE1S0=1'+#13#10;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg2');
result:=true;
end
else
begin
result:=false;
showmessage('sb2');
exit;
end;end;
通过writefile()函数M向ODEM发送+++换码符
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg');
result:=true;
end
else
begin
result:=false;
showmessage('sb');
exit;
end;不知道错在哪里?
使用WINDOWS 的通讯API函数实现。
2、 +++ 是已经握手成功后处于数据通讯状态下,发送此命令使MODEM处于命令状态,否则你的ATxxx都作为数据被发出去了
3、 发完+++后延时1-2秒,MODEM才会切换状态
4、 +++ 不是MODEM初始化
是ATxxxx
初始化串口
开串口
(对于对于MODEM看成是透明操作)1、ATH0+#13
2、ATH1+#13
3、ATDT+MoveCallNumber+#13
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
showmessage('kk');
//换码
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg'); (断点在此)
result:=true;
end
else
begin
result:=false;
showmessage('sb');
//exit;
end;
//初始化
//str:='ATE1M0Q0S0=1X4&C1&D3&K0S0'+chr(13)+chr(10);
//str:='ATE0V1Q0X4&D2S0=1&K0'+#13#10;
str:='ATE1S0=1'+#13#10;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg2');
result:=true;
end
else
begin
result:=false;
showmessage('sb2');
exit;
end;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
showmessage('kk');
//换码
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg'); (断点在此)
result:=true;
end
else
begin
result:=false;
showmessage('sb');
//exit;
end;
//初始化
//str:='ATE1M0Q0S0=1X4&C1&D3&K0S0'+chr(13)+chr(10);
//str:='ATE0V1Q0X4&D2S0=1&K0'+#13#10;
str:='ATE1S0=1'+#13#10;
sleep(1000);
initstr:=pchar(str);
initstrlen:=length(initstr);
if writefile(hcommfile,initstr^,initstrlen,nwrite,NIL) then
begin
showmessage('初始化MODEM成功');
showmessage('cg2');
result:=true;
end
else
begin
result:=false;
showmessage('sb2');
exit;
end;
showmessage('cg'); (断点在此)这呀,
我看还是硬件的问题,可能用TAPI真的会好一些
我在超级终端中使用ATDT可以拨号的,
在下把COM口设置正确后,重新试了一下
但是就是不会执行到showmessage('cg'); (断点在此)。十分DCB设置有问题,或WRITEFILE()函数使用错误?
我在超级终端中使用ATDT可以拨号的,
在下把COM口设置正确后,重新试了一下
但是就是不会执行到showmessage('cg'); (断点在此)。十分DCB设置有问题,或WRITEFILE()函数使用错误?
你在超级终端中发什么,就在你的程序中发什么,是对应的ASCII码