?? usms.pas
字號:
srcLen:integer;
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';
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 + -