⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modemanswer.cpp

📁 通过串口发送接受文件
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <dos.h>
#include "ModemAnswer.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 TModemAnswer::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------
__fastcall TModemAnswer::TModemAnswer(bool CreateSuspended)
        : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TModemAnswer::Execute()
{
        //---- Place thread code here ----
reanswer:
  Stop=True;
  Suspend();
  AnswerNow=False;
  RingCount=0;
  Stop=False;
  ErrorNo=-3;
  Answer();
  if( ErrorNo==-3 )Com->WriteChar(13,100);
  goto reanswer;
}
//---------------------------------------------------------------------------
void TModemAnswer::sendATcommand(char *str)
{
  unsigned int i;
  for(i=0;i<strlen(str);i++)
    {
    Com->WriteChar(str[i],0);
    };
  Com->WriteChar(13,0);
}

int TModemAnswer::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( AnswerNow==True )
      {
      ProgressMsg="正在应答...";
      AnswerNow=False;
      Answering=True;
      sendATcommand("ATA");
      };
    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;
      if( WaitRing==True )
        {
        if( strcmp(result,"RING")==0 )
          {
          RingCount++;
          strcpy(result,"");
          continue;
          };
        };
      return 0;
      };
    if( lastchar==13 )strcpy(result,"");
    buf[0]=ch;buf[1]=0;
    strcat(result,buf);
    lastchar=0;
    };
}

void TModemAnswer::StopAnswer(void)
{
  Stop=True;
}

int TModemAnswer::Answer(void)
{
  char result[32];
  char ch;
  WaitRing=False;
  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;
    };
  WaitRing=True;
  ProgressMsg="正在等待电话拨入...";
  if( waitATresult(result,-1)!=0 )
    {
    WaitRing=False;
    Answering=False;
    ErrorNo=-3;
    return -1;
    };
  WaitRing=False;
  Answering=False;
  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 + -