?? smsredskyimpl.pas
字號:
// s_Text:string;
Next_Str:widestring;
begin
i:=pos('+86',Str);
if i>0 then
TelNum:=copy(Str,i+3,11); //提取手機號碼 //傳遞給函數變量返回
//==============================================
Next_Str:=copy(Str,i+14,length(Str)-(i+4)+1);
i:=pos(#13,Next_Str);
if i>0 then
Next_Str:=copy(Next_Str,i+1,length(Next_Str)-(i+1)+1);
i:=pos(#13,Next_Str);
if i>0 then
Next_Str:=copy(Next_Str,1,i-1);
RecieveHandOK:=UniCode2GB(Next_Str); //返回回執的命令信息
end;
//===========(自定義函數)========2004-08-13 by 郭秀雙===========================
//功能:對COM口收到信息進行分類====〉〉〉〉提取
//參數:Recie_STR:Com口收到的信息
Function TRedSky.FromComInfo_SortChoose(Recie_STR:string):string;
var
i:integer;
SText,Num,ssS:widestring;
ResultSTR:widestring;
begin
ssS:=Recie_STR;
i:=pos('+CMGR',ssS);
if i>0 then
begin
//===================
i:=pos('0891',ssS);
if i>0 then
begin
SText:=CopyText(Num,ssS);
if Num<>'' then
ResultSTR:=Num;
if SText<>'' then
ResultSTR:=ResultSTR+','+UniCode2GB(SText);
end;
i:=Pos('$OK:TMSG',ssS);
if i>0 then
begin
SText:=RecieveOKTMSG(Num,ssS);
if SText<>'' then
ResultSTR:=Num+','+SText;
end
else
begin
SText:=RecieveHandOK(Num,ssS);
if SText<>'' then
ResultSTR:=Num+','+SText;
end;
end ;
//==============主動讀取短信時的信息提取( )==================================
i:=pos('+CMTI: "SM"',ssS);
if i>0 then
begin
SText:=ReadComSMS(ssS);
if SText<>'' then
ResultSTR:=Num+','+SText;
M.Lines.Add('ReadSMS:'+ResultSTR);
end;
//=============讀取全部短信息的提取( AT+CMGL="ALL" )======================
i:=pos('+CMGL',ssS);
if i>0 then
begin
i:=Pos('$OK:TMSG',ssS);
if i>0 then
begin
SText:=RecieveOKTMSG(Num,ssS);
if SText<>'' then
ResultSTR:=Num+','+SText;
end
else
begin
SText:=RecieveHandOK(Num,ssS);
if SText<>'' then
ResultSTR:=Num+','+SText;
end;
End;
//================觸發事件====2004-08-14 by guoxiushuang=============
if (Num<>'') and (SText<>'') then
FEvents.OnComReceiveSMS(Num,SText);
end;
procedure TRedSky.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
ss=array[1..500]of char ;
var str1:^ss;
i:integer;
s:array[0..15] of char ;
n:integer;
sss,SText: string;
begin
SetLength(Sss, BufferLength); //接收RS232的數據并顯示Memo1上。
Move(Buffer^, PChar(Sss)^, BufferLength);
//========進行字符串的分類處理===========
M_ComEvent.Lines.Add(ssS);
FromComInfo_SortChoose(ssS);
end;
//============GBK轉為漢字=======================================================
function TRedSky.WideStrToCh(S: String): string; //
var
C,C1,C2: String;
SS: WideString;
i: Word;
begin
i := 1;
while i <= (Length(S) - 2) do
begin
C1 := Copy(S, i, 2);
C2 := Copy(S, i + 2, 2);
C := Chr(StrToInt('$' + C1)) + Chr(StrToInt('$' + C2));
SS := SS + C;
i := i + 4;
end;
Result := SS;
end;
//============漢字轉成GBK碼=====================================================
Function TSendSMSThread.TextToGBK(Text:string):string;
var
t:widestring;
i:integer;
begin //晴天====C7E7CCEC
for i:=1 to Length(Text) do
t := t + IntToHex(ord(Text[i]),2);
TextToGBK:=t;
end;
//===========短信息編碼=============
//Pdu 編碼
function TSendSMSThread.EncodeGb(s: WideString): string;
var
i,len:Integer;
cur:Integer;
t:string;
begin
Result:='';
len:=Length(s);
i:=1;
while i<=len do
begin
cur:=ord(s[i]);
//BCD轉換
FmtStr(t,'%4.4X',[cur]);
Result:=Result+t;
inc(i);
end;
end;
//
// Pdu模式手機號碼編碼
//
//
// 用來交換移位
//
function TSendSMSThread.ConvertPhoneNum(PhoneNum: string): string;
var
Len, i : integer;
tmp,T_PhoneNum : widestring;
begin
Len := length(PhoneNum);
if Len=11 then
T_PhoneNum:='86'+PhoneNum
else
T_PhoneNum:=PhoneNum;
if (len <> 11) and (len <> 13) then
begin
ConvertPhoneNum := '';
exit;
end;
T_PhoneNum := T_PhoneNum + 'F';
Len := length(T_PhoneNum);
i := 1;
while i<=len do
begin
tmp := tmp + T_PhoneNum[i + 1] + T_PhoneNum[i];
inc(i, 2);
end;
ConvertPhoneNum := tmp;
end;
//===========(自定義函數)=====2004-08-11=======郭秀雙==============================
//格式: '11000D91'+ConvertPhoneNum(trim(CB_Tel.Text))+'0008A7'+L1(SendStr1)+SendStr1;
//功能:控制命令+派車單=轉換成編碼
//參數:Mission:控制命令; AlarmInfo:派車單
//附加:參數返回的時候前面增加了 長度字節
Function TSendSMSThread.MakeInfo_UnicodeGBK(SendTel,Mission,AlarmInfo:string):string;
var
M_Code,A_Code,S_Phone:widestring;
Len:string;
begin
if Length(SendTel)>=11 then
begin
if Length(SendTel)=11 then
SendTel:='86'+SendTel;
if Mission<>'' then
M_Code:=EncodeGb(WideString(Mission)); //控制命令
if AlarmInfo<>'' then
A_Code:=TextToGBK(AlarmInfo); //派車單
S_Phone:='11000D91'+ConvertPhoneNum(SendTel)+'0008A7'; //發送的目的電話號碼
//=================================
Len:=inttostr(length(M_Code+A_Code));
Len:=IntToHex(strtoint(Len) div 2, 2);
end;
M_Temp.Lines.Add('GBKCODE:'+S_Phone+Len+M_Code+A_Code);
MakeInfo_UnicodeGBK:=S_Phone+Len+M_Code+A_Code;
end;
//發送短信息線程
Constructor TSendSMSThread.Create(Suspended:Boolean;Phone,Mission,Text:string;Temp:Tmemo;Comname:Tcomm);
begin
//showmessage('ok-0');
inherited Create(Suspended);
ID_Phone:=Phone;
ID_Mission:=Mission;
ID_Text:=Text;
M_Temp:=Temp;
Comm1:=Comname;
// showmessage(ID_Phone+ID_Mission+ID_Text);
M_Temp.Lines.Add('警告!@@@@InitStartSendMode'+ID_Phone+ID_Mission+ID_Text);
//Form1.M_Send.Lines.Add('警告!!'+ID_Phone+'::'+ID_Mission+'::'+ID_Text);
FreeOnTerminate:=True;
end;
//=======(自定義函數)===2004-08-11====by 郭秀雙=================================
//功能:初始化Pdu 發送設置
//參數:Sign :: 1 :Text方式;0:Pdu方式
Function TSendSMSThread.InitStartSendMode(Sign:string):string;
var
str:shortstring;
len:word;
buf:pchar;
begin
//showmessage('ok0');
//=====開始發送====初始化PDU設置信息===================
str:='AT+CMGF='+Sign+chr(13);
// M_Temp.Lines.Add('修改發送模式:'+Sign);
buf:=@str;
inc(buf);
len:=length(str);
comm1.WriteCommData(buf,len);
sleep(200);
// showmessage(str);
end;
//=========(自定義函數)=====2004-08-11=====by 郭秀雙===========================
//功能:發送信息至Com口,發往目的車載MDT顯示屏上
//參數:
Function TSendSMSThread.SendInfoToCom(Text:string):string;
var
// SendLength:integer;
// str:shortstring;
// len:word;
// buf:pchar;
s:string;
begin
S:=Text;
// showmessage('ok5');
//=====開始發送====信息======pchar=============
comm1.WriteCommData(pchar(s),length(s));
//==============================
M_Temp.Lines.Add('警告!@@@@ToCom'+s);
end;
procedure TSendSMSThread.SendSMSToCom;
var
SMS:string;
SendLength:integer;
str_1,str_2:string; //widestring Shortstring
// len:word;
// buf:pchar;
begin
// showmessage('ok4');
if Length(ID_Text)<=112 then
begin
SMS:=MakeInfo_UnicodeGBK(trim(ID_Phone),ID_Mission,ID_Text);
SendLength:=length(SMS) div 2;
//第一段信息==========================
str_1:='AT+CMGS='+inttostr(SendLength)+chr(13); //+inttostr(SendLength)53
//第二段信息==========================
str_2:='00'+SMS+chr(26)+chr(13);
M_Temp.Lines.Add('警告!@@@@'+str_1);
M_Temp.Lines.Add('警告!@@@@'+str_2);
//======(1)初始化Pdu===========================
InitStartSendMode('0');
sleep(200);
//======(2)發送第一段信息======================
SendInfoToCom(str_1);
sleep(200);
//======(3)發送第二段信息======================
SendInfoToCom(str_2);
//M_Temp.Lines.Add('警告!@@@@'+str_1);
//M_Temp.Lines.Add('警告!@@@@'+str_2);
end
else
begin
//Form1.M_Send.Lines.Add('警告!信息長度超長!!!!');
//==========================================================
SMS:=MakeInfo_UnicodeGBK(trim(ID_Phone),ID_Mission,Copy(ID_Text,1,112));
SendLength:=length(SMS) div 2;
//第一段信息==========================
str_1:='AT+CMGS='+inttostr(SendLength)+chr(13); //+inttostr(SendLength)53
//第二段信息==========================
str_2:='00'+SMS+chr(26)+chr(13);
//Form1.M_Send.Lines.Add('警告!@@@'+str_2);
//======(1)初始化Pdu===========================
InitStartSendMode('0');
sleep(200);
//======(2)發送第一段信息======================
SendInfoToCom(str_1);
sleep(200);
//======(3)發送第二段信息======================
SendInfoToCom(str_2);
//-----------------------------------------------//
//-----------------------------------------------//
sleep(5000);
//-----------------------------------------------//
//-----------------------------------------------//
SMS:=MakeInfo_UnicodeGBK(trim(ID_Phone),ID_Mission,Copy(ID_Text,113,Length(ID_Text)-112));
SendLength:=length(SMS) div 2;
//第一段信息==========================
str_1:='AT+CMGS='+inttostr(SendLength)+chr(13); //+inttostr(SendLength)53
//第二段信息==========================
str_2:='00'+SMS+chr(26)+chr(13);
//M_Temp.Lines.Add('警告!@@@@'+str_1);
//M_Temp.Lines.Add('警告!@@@@'+str_2);
sleep(200);
//======(2)發送第一段信息======================
SendInfoToCom(str_1);
sleep(200);
//======(3)發送第二段信息======================
SendInfoToCom(str_2);
end;
end;
procedure TSendSMSThread.execute;
var
WaitReturn: DWORD;
begin
//showmessage('ok2');
FreeOnTerminate := True;
//進入同步,防止其他線程訪問
WaitReturn := WaitForSingleObject(SendMutex, INFINITE);
begin
WaitForSingleObject(SendMutex, INFINITE);
//執行發送信息==========
SendSMSToCom;
//====暫停1秒鐘==防止數據表鎖死=============
sleep(6000);
ReleaseMutex(SendMutex);//
sleep(1000);
//showmessage('ok3');
end;
end;
procedure TRedSky.SendSMS(const Phone, Mission, SMS: WideString);
begin
//showmessage('ok-00');
TSendSMSThread.Create(false,Phone,Mission,SMS,M,Comm1);
end;
//讀取GSM中所有的信息,并刪除
procedure TRedSky.ReadAllSMSFromGSM;
var
str:shortstring;
len:word;
buf:pchar;
begin
InitStartSendMode('1');
//=====開始發送====初始化PDU設置信息===================
str:='AT+CMGL="ALL"'+chr(13);
buf:=@str;
inc(buf);
len:=length(str);
comm1.WriteCommData(buf,len);
sleep(200);
end;
initialization
TActiveFormFactory.Create(
ComServer,
TActiveFormControl,
TRedSky,
Class_RedSky,
1,
'',
OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
tmApartment);
//非可視化控件 OLEMISC_INVISIBLEATRUNTIME
//可是化控件 OLEMISC_SIMPLEFRAME
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -