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

📄 usms.pas

📁 串口和AT指令的编程
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  i:integer;
begin
  strTmp := '';
  srcLen := length(src);
  {if (srcLen <> 11) and (srcLen <> 13) then
  begin
    ExchangeCode := '';
    exit;
  end;}

  src := src+'F';
  i := 1;
  while i<= srcLen do
  begin
    strTmp := strTmp + src[i+1]+src[i];
    inc(i,2);
  end;
  ExchangeCode := strTmp;
end;

function TSMSControl.SendInEncodePdu(SMSData:string;Mobile:string):boolean;
var
  Widesms:widestring;
  SendData:string;
  HeadData:string;
  ReceData:string;
  strTmp:string;
  strlen:integer;
  iNum:integer;
begin
  result := FALSE;
  SendData := '0891';
  strTmp := ExchangeCode(SMS.SMSSet.SMSCenter);
  SendData := SendData + strTmp;
  //SendData := SendData + '11000D91';   //5~6:号码长度;7~8:Type of Address
    SendData := SendData + '1100';
  SendData := SendData + IntToHex(Length(mobile), 2) + '81';
  strTmp := ExchangeCode(mobile);
  SendData := SendData + strTmp;
  SendData := SendData + '0008A7';
  Widesms := widestring(SMSData);
  strTmp := EnCodeGB(Widesms);
  strlen := length(strTmp) div 2;
  SendData := SendData + IntToHexStr(strlen)+strTmp;                            //16进制数
  strlen := (length(SendData) -18) div 2;                                       //不包括开头前9个字节
  HeadData := 'AT+CMGC='+IntToStr(strlen)+chr(13);                              //10进制数
  SendData := SendData + chr(26);

  SComm.ReadCom(ReceData,iNum,0,TRUE,0);                                        //先清接收缓冲区
  SComm.WriteCom(HeadData,0,FALSE,500);
  SComm.ReadCom(ReceData,iNum,3,FALSE,500);                                     //延时100ms
  if (copy(ReceData,3,1) <> '>') then
  begin
    result := FALSE;
    exit;
  end;

  SComm.WriteCom(SendData,0,FALSE,500);
  SComm.ReadCom(ReceData,iNum,10,FALSE,6000);                                   //延时100ms
  //' '#$D#$A'+CMGC: 3'#$D#$A#$D#$A'OK'#$D#$A     OK   时返回20个字节
  //' '#$D#$A'ERROR'#$D#$A                        Error时返回10个字节
  if copy(ReceData,4,5) = '+CMGC' then
  begin
    SComm.ReadCom(ReceData,iNum,10,FALSE,200);                                  //延时100ms
    result := TRUE;
  end;
end;

function TSMSControl.SendSMS(var strSMS:string;Mobile:string):boolean;
begin
  if SMS.SMSSet.SMSMode = 'PDU' then
  begin
    result := SendInEncodePdu(strSMS,Mobile);
  end else
  begin
    result := SendInEncodeText(strSMS,Mobile);
  end;
end;

function TSMSControl.CheckInText:boolean;
var
  ReceData:string;
  SendData:string;
  strTmp:string;
  StratIndexFlag:boolean;
  iNum:integer;
  i,j:integer;
begin
  result := FALSE;
  SComm.purgeReadWrite(0);
  SComm.purgeReadWrite(1);
  SendData := 'AT+CMGL=ALL'+chr(13);
  SComm.WriteCom(SendData,0,FALSE,200);
  SComm.ReadCom(ReceData,iNum,6,FALSE,500);
  if copy(ReceData,4,3) <> 'CMG' then exit;
  SComm.ReadCom(ReceData,iNum,140,FALSE,300);

  strTmp := '';
  StratIndexFlag := FALSE;
  RxMsg.Index := '';

  j := 0;
  for i:=1 to iNum do
  begin
    if copy(ReceData,i,1) = ',' then
      inc(j);

    if StratIndexFlag and (copy(ReceData,i,1) <> ',') then
    begin
      if copy(ReceData,i,1) <> ' ' then
      begin
        RxMsg.Index := RxMsg.Index + copy(ReceData,i,1)                         //取得短消息的索引号
      end;
    end else
    begin
      StratIndexFlag := FALSE;
    end;
    if copy(ReceData,i,1) = ':' then StratIndexFlag := TRUE;

    if j = 2 then break;
  end;
  RxMsg.Mobile := copy(ReceData,i+3,13);                                        //取得发送号码

  j := 0;
  for i:=1 to iNum do
  begin
    if copy(ReceData,i,1) = ',' then
      inc(j);
    if j = 4 then break;
  end;
  strTmp := copy(ReceData,i+2,20);                                              //取得发送时间
  RxMsg.Date := copy(strTmp,1,2)+copy(strTmp,4,2)+copy(strTmp,7,2)
               +copy(strTmp,10,2)+copy(strTmp,13,2)+copy(strTmp,16,2);

  for i:=1 to iNum do
  begin
    if copy(ReceData,i,2) = chr(13)+chr(10) then break;
  end;
  for j:=i+3 to iNum do
  begin
    if copy(ReceData,j,2) = chr(13)+chr(10) then break;
  end;
  RxMsg.SMSMsg := copy(ReceData,i+2,j-i-2);                                     //取得消息内容

  result:=TRUE;

  SendData := 'AT+CMGD='+RxMsg.Index+chr(13);                                   //删除当前信息
  for i:=0 to 2 do
  begin
    SComm.WriteCom(SendData,0,FALSE,200);
    SComm.ReadCom(ReceData,iNum,6,FALSE,500);
    sleep(100);
  end;
end;

function TSMSControl.CheckInPdu:boolean;
var
  ReceData:string;
  ReceDataBack:string;
  SendData:string;
  strTmp:string;
  StratIndexFlag:boolean;
  strlen:integer;
  iNum:integer;
  iRet:integer;
  i,j:integer;
begin
  result := FALSE;
  SComm.purgeReadWrite(0);
  SComm.purgeReadWrite(1);
  SendData := 'AT+CMGL=4'+chr(13);
  SComm.WriteCom(SendData,0,FALSE,200);
  SComm.ReadCom(ReceData,iNum,6,FALSE,500);
  if copy(ReceData,4,3) <> 'CMG' then exit;
  iRet := SComm.ReadCom(ReceData,iNum,14,FALSE,200);
  if iRet < 0 then exit;

  ReceDataBack := ReceData;
  strTmp := '';
  StratIndexFlag := FALSE;
  RxMsg.Index := '';

  j := 0;
  for i:=1 to 14 do
  begin
    if copy(ReceData,i,1) = ',' then
      inc(j);

    if StratIndexFlag and (copy(ReceData,i,1) <> ',') then
    begin
      if copy(ReceData,i,1) <> ' ' then
      begin
        RxMsg.Index := RxMsg.Index + copy(ReceData,i,1)                         //取得短消息的索引号
      end;
    end else
    begin
      StratIndexFlag := FALSE;
    end;

    if copy(ReceData,i,1) = ':' then StratIndexFlag := TRUE;
    if j = 3 then
    begin
      for j:=i+1 to 20 do
      begin
        if copy(ReceData,j,2) = chr(13)+chr(10) then break;
      end;
    end;
    if j>3 then  break;
  end;

  strlen := StrToInt(copy(ReceData,i+1,j-i-1));
  strlen := (strlen+10)*2-(iNum-j-1);
  iRet := SComm.ReadCom(ReceData,iNum,strlen,FALSE,500);
  if iRet = 0 then
  begin
    ReceDataBack := ReceDataBack + ReceData;
    strlen := length(ReceDataBack);
    for i:=1 to strlen do
    begin
      if copy(ReceDataBack,i,2) = chr(13)+chr(10) then  break;
    end;
    i := i+2;

    if i < strlen-4 then
    begin
      ReceData := copy(ReceDataBack,i,strlen-i);
      for i:=1 to strlen-i do
      begin
        if copy(ReceData,i,1) = chr(13) then break;
      end;
      ReceData := copy(ReceData,1,i-1);                                         //取得PDU编码

      strlen := (StrToInt(copy(ReceData,1,2))+2)*2+1;
      strTmp := copy(ReceData,strlen,2);
      iNum := HexStrToInt(strTmp);
      if (iNum mod 2) >0 then inc(iNum);
      strlen := strlen + 4;
      strTmp := copy(ReceData,strlen,iNum);
      RxMsg.Mobile := ExchangeCode(strTmp);
      if copy(RxMsg.Mobile,length(RxMsg.Mobile),1) = 'F' then
        RxMsg.Mobile := copy(RxMsg.Mobile,1,length(RxMsg.Mobile)-1);            //取得发送号码

      strlen := strlen + iNum + 4;
      strTmp := copy(ReceData,strlen,12);
      RxMsg.Date := ExchangeCode(strTmp);                                       //取得发送时间

      strlen := strlen + 14;
      strTmp := copy(ReceData,strlen,2);
      iNum := HexStrToInt(strTmp)*2;
      strTmp := copy(ReceData,strlen+2,iNum);
      RxMsg.SMSMsg := DeCodeUnicode(strTmp);                                    //取得消息内容
      result:=TRUE;
    end;
{    SendData := 'AT+CMGD='+RxMsg.Index+chr(13);                                 //删除当前信息
    for i:=0 to 2 do
    begin
      SComm.WriteCom(SendData,0,FALSE,200);
      SComm.ReadCom(ReceData,iNum,6,FALSE,500);
      sleep(100);
    end;
}    
  end;
end;

function TSMSControl.CheckNewSMS:boolean;
begin
  if SMS.SMSSet.SMSMode = 'PDU' then
  begin
    result := CheckInPdu;
  end else
  begin
    result := CheckInText;
  end;
end;




end.



⌨️ 快捷键说明

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