📄 usms.~pas
字号:
//' '#$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.SendInEncodePdu(SMSData:string;Mobile:string):boolean;
var
Widesms:widestring;
SendData:string;
HeadData:string;
ReceData:string;
strTmp:string;
strlen:integer;
iNum:integer;
LenStr,I,LenWidStr,PhoneLen : Integer;
begin
result := FALSE;
SendData := '';
strTmp := ExchangeCode(SMS.SMSSet.SMSCenter);
strlen :=length(strTmp) div 2+1;
//SendData := '0'+IntToStr(strlen)+'91';
//SendData := SendData + strTmp;
strTmp := ExchangeCode(mobile);
SendData := SendData + '001100'+Format('%2.2X', [length(strTmp)-1])+'91';
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;
PhoneLen := Length(Mobile) div 2;
LenWidStr := Length(Widesms);
LenStr := LenWidStr * 2; //不包括开头前9个字节
//HeadData := 'AT+CMGS='+IntToStr(strlen)+chr(13); //10进制数
HeadData := 'AT+CMGS='+IntToStr(8+PhoneLen+LenStr)+#13;
SendData := SendData + chr(26);
// MessageDlg(SendData,mtWarning,[mbYes,mbNo],0) ;
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) = '+CMGS' 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);
//MessageDlg(SendData,mtInformation,[mbOk],0); //删除当前信息
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);
//MessageDlg(SendData,mtInformation,[mbOk],0);
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);
//MessageDlg(ReceData,mtInformation,[mbOk],0);
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);
//MessageDlg(strTmp,mtInformation,[mbOk],0);
iNum := HexStrToInt(strTmp);
if (iNum mod 2) >0 then inc(iNum);
strlen := strlen + 4;
strTmp := copy(ReceData,strlen,iNum);
RxMsg.Mobile := ExchangeCode(strTmp);
// MessageDlg(RxMsg.Mobile,mtInformation,[mbOk],0);
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,true,200);
SComm.ReadCom(ReceData,iNum,6,FALSE,500);
//MessageDlg(SendData+'========='+ReceData,mtInformation,[mbOk],0);
sleep(100);
end;
end;
end;
function TSMSControl.CheckNewSMS:boolean;
begin
result := CheckInPdu;
{
if SMS.SMSSet.SMSMode = 'PDU' then
begin
result := CheckInPdu;
end else
begin
result := CheckInText;
end;
}
end;
function TSMSControl.SEncodeMobNO(SmobNO: string): string;
var
TempPchar: Pchar;
i: integer;
Str: string;
begin
if (copy(smobno, 1, 1) = '+') then //判断是否包含国家编码
SmobNO := copy(smobno, 2, length(smobno) - 1); //去掉手机号码中的'+'
if ((length(SmobNO) mod 2) = 1) then
SmobNO := SmobNO + 'F';
TempPchar := Pchar(SmobNO); //将字符串 Char数组化
i := 0;
Str := '';
while i < length(TempPchar) do begin
Str := Str + TempPchar[i + 1] + TempPchar[i];
i := i + 2;
end;
result := Str;
end;
function TSMSControl.MakePDU(DestNumber, Content: PChar): String;
var
DestPhoneNum: string;
AddressLength: string;
TpUserData: string;
TPUserDataLength: string;
temp: WideString;
Tmp_Pchar,Result_String: PChar;
Rlt_str: string;
begin
if (DestNumber='') or (Content='') then
exit
else
begin
DestPhoneNum := SEncodeMobNO(DestNumber);
AddressLength := Format('%2.2X', [length(DestNumber)-1]);
//AddressLength := '0D';
temp := Content;
Tmp_Pchar := StrAlloc(1000);
if SMSEncode(temp, Tmp_Pchar) then
TpUserData := StrPas(Tmp_Pchar);
//if TpDcs='08' then
if true then
TPUserDataLength := Format('%2.2X', [Length(TpUserData) div 2])
else
TPUserDataLength := Format('%2.2X', [((Length(TpUserData) div 2)*8) div 7]);
Rlt_str := lenSmsInfo //00
+ firstOctet //11
+ TPMessageReference //00
+ AddressLength //0D
+ TypeOfAddress //91
+ DestPhoneNum //
+ TpId //00
+ TpDcs //08
+ TPValidityPeriod //AA->01
+ TPUserDataLength
+ TpUserData;//TpUserData have to Encoded;
// StrCopy(Result_String, PChar(Rlt_str));
// Result := True;
// LogStatus(Pchar('PDU String: '+ Rlt_str));
Result := Rlt_str;
end;
end;
function TSMSControl.SMSEncode(s: WideString; Result_Code: PChar): Boolean;
var
i, len, cur: Integer;
t, temp: string;
begin
if Length(s)>0 then
begin
len := Length(s);
i := 1;
while i <= len do
begin
cur := ord(s[i]);
//BCD convert
FmtStr(t, '%4.4X', [cur]);
temp := temp + t;
Inc(i);
end;
StrCopy(Result_Code, PChar(temp));
Result := True;
end
else
Result := False;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -