📄 modemdialer.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <time.h>
#include <Dos.h>
#include "ModemDialer.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TModemDialer::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TModemDialer::TModemDialer(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TModemDialer::Execute()
{
//---- Place thread code here ----
redial:
Stop=True;
Suspend();
Stop=False;
ErrorNo=-3;
Dial();
if( ErrorNo==-3 )Com->WriteChar(13,100);
goto redial;
}
//---------------------------------------------------------------------------
void TModemDialer::StopDial(void)
{
Stop=True;
}
void TModemDialer::sendATcommand(char *str)
{
unsigned int i;
for(i=0;i<strlen(str);i++)
{
Com->WriteChar(str[i],0);
};
Com->WriteChar(13,0);
}
int TModemDialer::waitATresult(char *result,int timeout)
{
char ch;
char buf[3];
char lastchar;
long starttimer,timer;
struct time t;
strcpy(result,"");
if( timeout>=0 )
{
gettime(&t);
starttimer=(((t.ti_hour)*60+t.ti_min)*60+t.ti_sec)*1000+t.ti_hund*10;
};
lastchar=0;
while( 1 )
{
if( timeout<0 )
{
if( Stop==True )return -3;
}
else
{
gettime(&t);
timer=(((t.ti_hour)*60+t.ti_min)*60+t.ti_sec)*1000+t.ti_hund*10;
if( timer-starttimer>timeout )return -1;
}
if( Com->ReadChar(&ch,0)!=0 )continue;
if( ch==13 )
{
if( lastchar==13 )strcpy( result,"" );
lastchar=13;
continue;
};
if( ch==10 )
{
if( strlen(result)==0 )continue;
return 0;
};
if( lastchar==13 )strcpy(result,"");
buf[0]=ch;buf[1]=0;
strcat(result,buf);
lastchar=0;
};
}
int TModemDialer::Dial(void)
{
char result[32];
char buf[64];
char ch;
ProgressMsg="正在初始化调制解调器...";
while( Com->ReadChar(&ch,100)==0 );
sendATcommand("ATZ");
if( waitATresult(result,3000)!=0 )
{
ErrorNo=-1;
ErrorMsg="初始化调制解调器错误!";
return -1;
};
if( strcmp(result,"OK")!=0 )
{
ErrorNo=-1;
ErrorMsg="初始化调制解调器错误!";
return -1;
};
strcpy(buf,"ATD");
strcat(buf,PhoneNumber.c_str());
ProgressMsg="正在拨号...";
sendATcommand(buf);
ProgressMsg="正在连接...";
if( waitATresult(result,-1)!=0 )
{
ErrorNo=-3;
return -1;
};
if( strcmp(result,"NO DIALTONE")==0 )
{
ErrorNo=-1;
ErrorMsg="无拨号音,请检查电话线是否接好";
return -1;
};
if( strcmp(result,"BUSY")==0 )
{
ErrorNo=-1;
ErrorMsg="忙音,请稍后再拨";
return -1;
};
if( memcmp(result,"CONNECT",7)==0 )
{
ErrorNo=0;
ProgressMsg="已连接";
return 0;
};
ErrorNo=-1;
ErrorMsg="连接失败";
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -