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

📄 shortsmsactiveimpl1.pas

📁 利用AT指令,直接控制GSM的控制.实现
💻 PAS
📖 第 1 页 / 共 4 页
字号:
            CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(Temp1)),3);
            Delete(ReceiveData,CMTINum1,16);
          except
            try
              CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(LeftBStr(Temp1,1))),3);
              Delete(ReceiveData,CMTINum1,15);
            except
              //CMGLAll:= CMGLAll + 1;
            end;
          end;
        end
        else
          Break;
      end;
    end
    else if CMTINum2 > 0 then
    begin
      Temp:= RightBStr(ReceiveData,Length(ReceiveData) - CMTINum2 - 11);
      NumTemp:= Pos(#13 + #10,Temp);
      if (NumTemp > 0) and (NumTemp < 5) then
      begin
        CMTIIndex:= CMTIIndex + RightBStr('000' + LeftBStr(Temp,NumTemp -1),3);

        Delete(ReceiveData,CMTINum2,13 + NumTemp);
      end
      else
      begin
        Temp1:= Trim(LeftBStr(Temp,2));
        if Length(Temp1) > 0 then
        begin
          try
            CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(Temp1)),3);
            Delete(ReceiveData,CMTINum1,16);
          except
            try
              CMTIIndex:= CMTIIndex + RightBStr('000' + IntToStr(StrToInt(LeftBStr(Temp1,1))),3);
              Delete(ReceiveData,CMTINum1,15);
            except
              //CMGLAll:= CMGLAll + 1;
            end;
          end;
        end
        else
          Break;
      end;
    end
    else
      Break;
  end;
end;

///////////////////////////////////////////////////////////////
//说  明:此过程是用来实现VB中的DoEvents功能
//函数名:DoEvents
//参  数:无
//返回值:无
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
procedure TShortSMSActiveX.DoEvents();
begin
  Application.ProcessMessages;
end;

///////////////////////////////////////////////////////////////
//说  明:此函数是用来返回系统目录
//函数名:GetSysDir
//参  数:无
//返回值:目录名
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
{function  TShortSMSActiveX.GetSysDir():string;  //返回系统目录
var
  path: array [0..MAX_PATH] of Char;
begin
  // 取系统目录
  GetSystemDirectory(path, MAX_PATH);
  Result:= string(path);
//  ShowMessage(string(path));
  // 取TEMP目录
//  GetTempPath(MAX_PATH, path);
//  ShowMessage(string(path));
end;}

///////////////////////////////////////////////////////////////
//说  明:此函数是用来得到系统是否已注册
//函数名:GetSysReg
//参  数:无
//返回值:Boolean
//        True:已注册
//        False:没有注册
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
{function  TShortSMSActiveX.GetSysReg(): Boolean; //
const
  cStartDate = 'StartDate';
  cLastDate = 'LastDateB';
  cUseDays = 'UseDaysWE';
  cUseTimes = 'UseTimes7'
  cRegNum = 'RegNumIBI';
  Pass1= '*&%$BV@&^56#%^&JH98';
  Pass2= 'BianWei!(&*!)@!';  //BianWei19781021
  Pass3= '$%sa&^as$s%dsf';
var
  SetIni: Tinifile;
  StartDateIdent,LastDateIdent,UseDaysIdent,UseTimesIdent,RegNumIdent: string;
  StartDateValue,LastDateValue,UseDaysValue,UseTimesValue,RegNumValue: string;
  eIMEINO: string;
  temp:string;
  tempI: word;
begin
  Result:= True;
    IMEINO     : string;                   //模块生产序列号
    IfReg      : Bool;                     //是否已注册
    UseDays    : Byte;                     //使用天数
    UseTimes   : Word;                     //使用次数

  if IMEINO = '' then
    IMEINO:= GetModemIMEINO;
  if IMEINO = '' then
  begin
    Result:= True;
    Exit;
  end;

  StartDateIdent:= Encrypt(cStartDate,Pass1);
  LastDateIdent:= Encrypt(cLastDate,Pass1);
  UseDaysIdent:= Encrypt(cUseDays,Pass1);
  RegNumIdent:= Encrypt(cRegNum,Pass1);
  UseTimesIdent:= Encrypt(cUseTimes,Pass1);
  eIMEINO:= Encrypt(IMEINO,Pass1);

  try
    SetIni:= TiniFile.Create(GetSysDir + '\SysReg.dll');
    StartDateValue:= SetIni.ReadString(IMEINO,StartDateIdent,Encrypt('12' + FormatDateTime('YYYYMMDD',Date()),Pass3));
    LastDateValue:= SetIni.ReadString(IMEINO,LastDateIdent,Encrypt('123' + FormatDateTime('YYYYMMDD',Date()),Pass3));
    UseTimesValue:= SetIni.ReadString(IMEINO,UseDaysIdent,Encrypt('1234' + '000',Pass3));
    UseDaysValue:= SetIni.ReadString(IMEINO,UseDaysIdent,Encrypt('1234567' + '00',Pass3));
    RegNumValue:= SetIni.ReadString(IMEINO,RegNumIdent,Encrypt('12345678901234',Pass3));

    SmartDateValue:= MidBStr(Decrypt(StartDateValue,Pass3),3,8);
    LastDateValue:= MidBStr(Decrypt(LastDateValue,Pass3),4,8);
    UseTimesValue:= MidBStr(Decrypt(UseTimesValue,Pass3),5,3);
    UseDaysValue:= MidBStr(Decrypt(UseDaysValue,Pass3),8,2);
    RegNumValue:= MidBStr(Decrypt(RegNumValue,Pass3),2,15);
    if StartDateValue = DateToStr(Date()) then
    begin
      StartDateValue:= FormatDateTime('YYYYMMDD',Date());
      Temp:= RandGetStr(6);
      StartDateValue:= LeftBStr(Temp,2) + StartDateValue + RightBStr(Temp,4);
    end;
    if

      LastDateValue:= FormatDateTime('YYYYMMDD',Date());
      Temp:= RandGetStr(6);
      LastDateValue:= LeftBStr(Temp,3) + LastDateValue + RightBStr(Temp,3);
      Temp:= RandGetStr(12);
      UseDaysValue:= LeftBStr(Temp,5) + '01' + RightBStr(Temp,7);
      Temp:= RandGetStr(10);
      UseTimesValue:= LeftBStr(Temp,4) + '0001' + RightBStr(Temp,6);
      RegNumValue:= RandGetStr(14);

      SetIni.WriteString(IMEINO,StartDateIdent,Encrypt(StartDateValue,Pass3));
      SetIni.WriteString(IMEINO,LastDateIdent,Encrypt(LastDateValue,Pass3));
      SetIni.WriteString(IMEINO,UseDaysIdent,Encrypt(UseDaysValue,Pass3));
      SetIni.WriteString(IMEINO,UseTimesIdent,Encrypt(UseTimesValue,Pass3));
      SetIni.WriteString(IMEINO,RegNumIdent,Encrypt(RegNumValue,Pass3));

      Result:= False;
    end
    else if MidBStr(Decrypt(LastDateValue,Pass3),4,8) <> FormatDateTime('YYYYMMDD',Date()) then
    begin
      Temp:= RandGetStr(6);
      LastDateValue:= LeftBStr(Temp,3) + FormatDateTime('YYYYMMDD',Date()) + RightBStr(Temp,3);

      try
        tempI:= StrToInt(MidBStr(UseDaysValue,5,2));
      except

      end;
    end;
  except
    Result:= True;
  end;
end;}

//接收事件
procedure TShortSMSActiveX.Comm1ReceiveData(Sender: TObject;
  Buffer: Pointer; BufferLength: Word);
var
  Temp: string;
begin
  SetLength(Temp,BufferLength);
  Move(Buffer^,PChar(Temp)^,BufferLength);
  ReceiveData:= ReceiveData + Temp;

//  memo1.Lines.add('《' + ReceiveData + '》《' + inttostr(Length(ReceiveData)) + '》');

  //判断是否已来到新短信息
  FindCMTI();

  if (Trim(ReceiveData) = 'OK') Or (Trim(ReceiveData) = 'ERROR') then
    ReceiveData:= '';
end;

///////////////////////////////////////////////////////////////
//说  明:此函数是用来配置并打开串口
//函数名:OpenComm
//参  数:1、CommNum:Byte;  串口号    默认值为1
//        2、BaudRate: Integer; 波特率  默认值为9600
//        3、Parity: Byte;      奇偶检验位 默认值为0  None
//                        值只能为0,1,2,3,4 : None, Odd, Even, Mark, Space;
//        4、DataBit: Byte;     数据位     默认值为8
//                        值只能为5,6,7,8
//        5、StopBit: Byte;     停止位     默认值为0
//                        值只能为0,1,2:1,1.5,2
//返回值:Shortint
//        >=0 打开串口成功
//        -1  打开串口失败
//        -2  串口号码错误,只能0<CommNum<=16
//        -3  波特率错误,  只能110,300,600,1200,2400,9600,14400,19200,28800,38400,56000,128000,256000;
//        -4  奇偶检验位错误,只能0<=Parity<=4
//        -5  数据位错误,     只能5<=DataBit<=8
//        -6  停止位错误,    只能0<=StopBit<=2
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
function TShortSMSActiveX.OpenComm(CommNum: Word; BaudRate: Integer;
  Parity: TSMSParity; DataBit: TSMSDataBit;
  StopBit: TSMSStopBit): Smallint;
var
  TempParity: TParity;
  TempDataBit:TByteSize;
  TempStopBit:TStopBits;
begin
  Result:= 0;
  if (CommNum <1) or (CommNum >16) then
  begin
    Result:= -2;
    Exit;
  end;
  if (BaudRate <1) or (BaudRate > 256000) then
  begin
    Result:= -3;
    Exit;
  end;
{  case BaudRate of
    110,300,600,1200,2400,9600,14400,19200,28800,38400,56000,128000,256000:
  else
    begin
      Result:= -3;
      Exit;
    end;
  end;}
  case Parity of
    0: TempParity:= None;
    1: TempParity:= Odd;
    2: TempParity:= Even;
    3: TempParity:= Mark;
    4: TempParity:= Space;
  else
    begin
      Result:= -4;
      Exit;
    end;
  end;
  case DataBit of
    5: TempDataBit:= _5;
    6: TempDataBit:= _6;
    7: TempDataBit:= _7;
    8: TempDataBit:= _8;
  else
    begin
      Result:= -5;
      Exit;
    end;
  end;
  case StopBit of
    0: TempStopBit:= _1;
    1: TempStopBit:= _1_5;
    2: TempStopBit:= _2;
  else
    begin
      Result:= -6;
      Exit;
    end;
  end;

  try
    Comm1.CommName:= 'COM' + IntToStr(CommNum);
    Comm1.BaudRate:= BaudRate;
    Comm1.Parity:= TempParity;
    Comm1.ByteSize:= TempDataBit;
    Comm1.StopBits:= TempStopBit;
    IfExecCmd:= False;
    ReceiveData:= '';
    Comm1.StartComm;
  except
    Result:= -1;
    Exit;
  end;
end;

///////////////////////////////////////////////////////////////
//说  明:此函数是用来关闭串口
//函数名:CloseComm
//参  数:无
//返回值:Integer
//        >=0 关闭串口成功
//        -1  关闭串口失败
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
function TShortSMSActiveX.CloseComm: Smallint;
begin
  try
    Comm1.StopComm;
    IfExecCmd:= False;
    ReceiveData:= '';
    Result:= 0;
  except
    Result:= -1;
  end;
end;

///////////////////////////////////////////////////////////////
//说  明:此函数是用来求出信号强度
//函数名:GetModemCSQ
//参  数:无
//返回值:Integer
//        0~99 信号强度
//        -1  求信号强度失败
//        -2  模块忙
//        -3  串口可能没有打开
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
function TShortSMSActiveX.GetModemCSQ: Smallint;
var
  OutData: PChar;
  CSQNum,OKNum: Integer;
  ATNum:        Integer;
  StartTime:    TDateTime;
  ErrNum:     Integer;
begin
  Result:= -1;
  try
    OutData:= 'AT+CSQ' + #13 + #10;
    if not IfExecCmd then
    begin
      IfExecCmd:= True;
      if Comm1.WriteCommData(OutData,Length(OutData)) then
      begin
        StartTime:= Now();

        while Now() <= StartTime + 1 * (1/24/60/60) do
        begin
          DoEvents;
          ATNum:= Pos('AT+CSQ',ReceiveData);
          CSQNum:= Pos(#13 + #10 + '+CSQ: ',ReceiveData);
          OKNum:= Pos(#13 + #10 + 'OK' + #13 + #10,ReceiveData);
          ErrNum:= Pos(#13 + #10 + 'ERROR' + #13 + #10,ReceiveData);

          if (ATNum > 0) and (CSQNum > ATNum) and (OKNum > CSQNum) then
          begin
            Result:= StrToInt(MidBStr(ReceiveData,CSQNum + 8,2));
            IfExecCmd:= False;
            Delete(ReceiveData,ATNum,OKNum - ATNum + 6);
            if Trim(ReceiveData) = '' then
              ReceiveData:= '';
            Exit;
          end
          else if (ATNum > 0) and (ErrNum > ATNum) and (CSQNum < 1) then
          begin
            IfExecCmd:= False;
            Delete(ReceiveData,ATNum,ErrNum - ATNum + 9);
            if Trim(ReceiveData) = '' then
              ReceiveData:= '';
            Exit;
          end;
        end;
      end;

      IfExecCmd:= False;
      ReceiveData:= '';
    end
    else
      Result:= -2;
  except
    IfExecCmd:= False;
    ReceiveData:= '';
    Result:= -3;
  end;
end;

/////////////////////////////////////////////////////////////
//说  明:此函数是用来判断模块是否登陆成功
//函数名:GetModemCREG
//参  数:无
//返回值:ShortInt
//        0:登陆成功
//        -1:登陆失败
//        -2:模块忙
//        -3:串口可能没有打开
//作  者:卞伟    2003年6月
/////////////////////////////////////////////////////////////
function TShortSMSActiveX.GetModemCREG: Smallint;
var
  OutData: PChar;
  CREGNum,ATNum,OKNum: Integer;
  StartTime: TDateTime;
  ErrNum:    Integer;
begin
  Result:= -1;
  try
    OutData:= 'AT+CREG?' + #13 + #10;
    if not IfExecCmd then
    begin
      IfExecCmd:= True;
      if Comm1.WriteCommData(OutData,Length(OutData)) then
      begin
        StartTime:= Now();

        while Now() <= StartTime + 1 * (1/24/60/60) do

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -