?? w_main.pas
字號:
unit w_main;
interface
uses
sim_canshu,ShellAPI,TLHelp32,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB, ExtCtrls;
type
Tmain = class(TForm)
MSComm1: TMSComm;
Memo1: TMemo;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
ComboBox1: TComboBox;
GroupBox4: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
Button1: TButton;
Button2: TButton;
Memo2: TMemo;
Edit1: TEdit;
Button3: TButton;
Button4: TButton;
GroupBox5: TGroupBox;
Button5: TButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
RadioButton5: TRadioButton;
Button6: TButton;
GroupBox6: TGroupBox;
RadioButton6: TRadioButton;
Timer1: TTimer;
RadioButton7: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button6Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure RadioButton6Click(Sender: TObject);
procedure RadioButton7Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure WMBarIcon(var Message:TMessage);message MY_MESSAGE;
procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;
public
{ Public declarations }
end;
var
main: Tmain;
simat:sim_at ;
str1:string;
programpath:string;
procedure Delay(MSecs: Longint); //延時功能
function shibie(str:string):integer;
function chuli_str(strs:string):integer ;
implementation
{$R *.dfm}
procedure Tmain.FormCreate(Sender: TObject);
var
i,k:integer;
data_set_str:array[0..50] of Char;
begin
//獲取安裝目錄的路徑
programpath:=Application.ExeName;
k:=0 ;
for i:=1 to strlen(pchar(programpath)) do
begin
if copy(programpath,i,1)='\' then
k:=i;
end;
programpath:=copy(programpath,1,k);
//初始化界面
//波特率
GetPrivateProfileString('setcom','btl','',data_set_str,100,pchar(programpath+'sim.ini'));
combobox1.Text :=data_set_str ;
//端口
GetPrivateProfileString('setcom','comport','',data_set_str,100,pchar(programpath+'sim.ini'));
if data_set_str='1' then
begin
radiobutton1.Checked:=true ;
radiobutton2.Checked :=false;
end
else
begin
radiobutton1.Checked:=false ;
radiobutton2.Checked :=true;
end;
//短信格式(TEXT OR PDU)
GetPrivateProfileString('at','cmgf','',data_set_str,100,pchar(programpath+'sim.ini'));
if data_set_str='1' then
begin
radiobutton6.Checked :=true ;
radiobutton7.Checked :=false ;
end
else
begin
radiobutton6.Checked :=false ;
radiobutton7.Checked :=true ;
end;
//串行口初始化
mscomm1.InBufferCount :=0;
mscomm1.InputLen :=0;
mscomm1.RThreshold :=1;
mscomm1.SThreshold :=1;
mscomm1.Settings :=combobox1.Text ;
if radiobutton1.Checked =true then
begin
mscomm1.CommPort :=1;
end
else
mscomm1.CommPort :=2;
mscomm1.PortOpen :=true;
mscomm1.DTREnable :=true;
mscomm1.RTSEnable :=true;
//初始化短信貓
simat.simat_num:=at_cmgf;
simat.simat_str:= 'at+cmgf='+data_set_str+#13 ;
mscomm1.Output :='at+cmgf='+data_set_str+#13 ;
mscomm1.InputLen :=0;
str1:='';
init_sim_xinxi();
end;
procedure Tmain.Button2Click(Sender: TObject);
begin
mscomm1.PortOpen :=false;
mscomm1.DTREnable :=false;
mscomm1.RTSEnable :=false;
close;
end;
procedure Tmain.Button1Click(Sender: TObject);
begin
if mscomm1.PortOpen =true then
begin
mscomm1.PortOpen :=false;
mscomm1.DTREnable :=false;
mscomm1.RTSEnable :=false;
end;
mscomm1.Settings :=combobox1.Text ;
if radiobutton1.Checked =true then
begin
mscomm1.CommPort :=1;
end
else
mscomm1.CommPort :=2;
mscomm1.PortOpen :=true;
mscomm1.DTREnable :=true;
mscomm1.RTSEnable :=true;
end;
procedure Tmain.MSComm1Comm(Sender: TObject);
var
str:olevariant;
begin
//掃描串口查看是否有新的消息 ,接收短信
if mscomm1.CommEvent =2 then
begin
str:=mscomm1.Input ;
if str<>'' then str1:=str1+str;
memo1.Text :=memo1.Text +str ;
end;
end;
function shibie(str:string):integer;
begin
trim(str) ;
if str='' then
begin
shibie:=-1 ;
end
else
begin
shibie:=0 ;
end;
if copy(str,length(str)-3,2)='OK' then shibie:=1 ;
if copy(str,3,5)='+CMTI' then shibie:=2 ;
if copy(str,length(str)-6,5)='ERROR' then shibie:=3 ;
end;
procedure Tmain.Timer1Timer(Sender: TObject);
var
xiaoxi:string;
i,duanxin_num:integer;
begin
case shibie(str1) of
0:begin
//case simat_num of
//end;
end;
1:begin
case simat.simat_num of
at_cmgf:begin //設置短消息格式成功
memo1.Text :=memo1.Text +'短信格式設置成功!'+#13+#13;
str1:='';
end;
at_CMGS:begin //發送短消息成功
simat.simat_num:=at_cmgf;
simat.simat_str:='at+cmgf=1'+#13 ;
mscomm1.Output := simat.simat_str ;
str1:='';
end;
at_CMGD:begin //刪除短消息成功
memo1.Text :=memo1.Text+'短消息刪除成功' ;
str1:='';
end;
at_CMGL:begin //存儲的短消息列表成功
memo1.Text :=memo1.Text+'存儲的短消息都在此';
//成功讀取信息后對信息進行處理
xiaoxi:=str1;
str1:='';
//整理短信
duanxin_num:=chuli_str(xiaoxi);
//查詢數據
//1、手機里的第幾條短信;2、是否讀過;3、手機號;4、時間;5、短信內容
for i:= 1 to duanxin_num do
begin
//獲得發該條短信的手機號
sim_xinxi[3,i]:=copy(sim_xinxi[3,i],4,length(sim_xinxi[3,i])-3);
//根據該手機號到數據庫去查詢該號碼是否有權限查詢
//如果有權限時
sim_xinxi[5,i]:=copy(sim_xinxi[5,i],3,length(sim_xinxi[5,i])-4);
//查詢數據庫,并讀出相關的信息
//保存信息到備發數據庫
//刪除該條短信
simat.simat_num:=at_cmgd;
simat.simat_str:= 'at+cmgd='+inttostr(i)+#13 ;
mscomm1.Output := simat.simat_str ;
Delay(500);
end;
end;
at_CMGR:begin //讀短消息
memo1.Text := memo1.Text+ '讀取第'+'條短信成功';
//成功讀取信息后對信息進行處理
xiaoxi:=str1;
str1:='';
end;
end;
end;
2:begin
memo1.Text :=memo1.Text +'有新短消息!'+#13;
str1:='';
//有新消息來,發送命令讀取短消息
simat.simat_num:=at_cmgr;
simat.simat_str:= 'at+cmgr=1'+#13 ;
mscomm1.Output := simat.simat_str;
Delay(500); //延時0.5秒,讀信息。時間不夠可以加
end;
3:begin
memo1.Text :=memo1.Text +'錯誤的命令!';
str1:='';
end;
end;
//掃描備發數據庫,發送短信
end;
procedure Delay(MSecs: Longint);
//延時函數,MSecs單位為毫秒(千分之1秒)
var
FirstTickCount, Now: Longint;
begin
FirstTickCount := GetTickCount();
repeat
Application.ProcessMessages;
Now := GetTickCount();
until (Now - FirstTickCount >= MSecs) or (Now < FirstTickCount);
end;
procedure Tmain.FormClose(Sender: TObject; var Action: TCloseAction);
var
nid: TNotifyIconData;
begin
nid.cbSize := sizeof(nid); // nid變量的字節數
nid.cbSize := sizeof(nid); // nid變量的字節數
nid.uID := 1; //內部標識,與加入小圖標時的數一致
nid.Wnd := Handle; //主窗口句柄
Shell_NotifyIcon(NIM_DELETE,@nid); //去掉小圖標
end;
procedure Tmain.Button6Click(Sender: TObject);
var
lpData:PNotifyIconData;
begin
//如果用戶最小化窗口則將窗口隱藏并在任務欄上添加圖標
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
lpData.Wnd := main.Handle;
lpData.hIcon := Application.Icon.Handle;
lpData.uCallbackMessage := MY_MESSAGE;
lpData.uID :=0;
lpData.szTip := '被盜車輛短信查詢服務中心';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,lpData);
dispose(lpData);
main.Visible := False;
end;
procedure Tmain.WMBarIcon(var Message:TMessage);
var
lpData:PNotifyIconData;
begin
if (Message.LParam = WM_LBUTTONDOWN) then
begin
//如果用戶點擊任務欄圖標則將圖標刪除并回復窗口。
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;//SizeOf(PNotifyIconDataA);
lpData.Wnd := main.Handle;
lpData.hIcon := Application.Icon.Handle;
lpData.uCallbackMessage := MY_MESSAGE;
lpData.uID :=0;
lpData.szTip := '被盜車輛短信查詢服務中心';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_DELETE,lpData);
dispose(lpData);
main.Visible := True;
end;
end;
procedure Tmain.WMSysCommand(var Message:TMessage);
var
lpData:PNotifyIconData;
begin
if Message.WParam = SC_ICON then
begin
//如果用戶最小化窗口則將窗口隱藏并在任務欄上添加圖標
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
lpData.Wnd := main.Handle;
lpData.hIcon := Application.Icon.Handle;
lpData.uCallbackMessage := MY_MESSAGE;
lpData.uID :=0;
lpData.szTip := '被盜車輛短信查詢服務中心';
lpData.uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD,lpData);
dispose(lpData);
main.Visible := False;
end
else
begin
//如果是其它的SystemCommand消息則調用系統缺省處理函數處理之。
DefWindowProc(main.Handle,Message.Msg,Message.WParam,Message.LParam);
end;
end;
procedure Tmain.Button4Click(Sender: TObject);
begin
//if RadioButton3.Checked =true then cmgl_str:='REC UNREAD' ;
//if RadioButton4.Checked =true then cmgl_str:='REC READ' ;
//if RadioButton5.Checked =true then cmgl_str:='ALL' ;
simat.simat_num:=at_cmgl;
simat.simat_str:= 'at+cmgl="ALL"'+#13 ;
mscomm1.Output := simat.simat_str;
end;
procedure Tmain.RadioButton6Click(Sender: TObject);
begin
simat.simat_num:=at_cmgf;
simat.simat_str:= 'at+cmgf=1'+#13 ;
mscomm1.Output := simat.simat_str ;
end;
procedure Tmain.RadioButton7Click(Sender: TObject);
begin
simat.simat_num:=at_cmgf;
simat.simat_str:= 'at+cmgf=0'+#13 ;
mscomm1.Output := simat.simat_str ;
end;
procedure Tmain.Button3Click(Sender: TObject);
var
sim_addr,sim_phone,sim_asm,sim_str:string;
i:integer ;
begin
//切換成PDU格式
simat.simat_num:=at_cmgf;
simat.simat_str:= 'at+cmgf=0'+#13 ;
mscomm1.Output := simat.simat_str ;
//整理字符串
sim_addr:='0891683108706705f0'; //百色的短信中心號碼13500776500
//目標手機號碼轉換
sim_str:='86'+edit1.Text+'f' ;
sim_phone:='' ;
for i:=1 to 7 do
begin
sim_phone:=sim_phone+copy(sim_str,i*2,1)+copy(sim_str,i*2-1,1) ;
end ;
//Unicode化發送的內容
sim_asm:=str_Gb2UniCode(memo2.Text);
sim_asm:=IntToDigit(jianban(length(sim_asm)),16)+sim_asm ;
//組合
sim_phone:='11000D91' +sim_phone ;
sim_phone:=sim_phone+'000800'+sim_asm ;
Delay(50);
simat.simat_num:=at_cmgs ;
simat.simat_str:= 'at+cmgs='+inttostr(jianban(length(sim_phone)))+#13 ;
mscomm1.Output := simat.simat_str ;
Delay(100);
mscomm1.Output:=sim_addr+sim_phone+#26 ;
{//下面是TEXT格式發送短信,不支持中文發送
simat_num:=at_cmgs;
mscomm1.Output :='at+cmgs="'+edit1.Text+'"'+#13 ;
Delay(50);
mscomm1.Output :=memo2.Text +#26 ; }
end;
function chuli_str(strs:string):integer ;
var
i,j,k,n:integer;
yinhao_num:integer;
begin
if length(strs)<30 then
begin
chuli_str:=0;
exit;
end;
//去除命令
yinhao_num:=0 ;
n:=0;
for i:=1 to length(strs) do
begin
if copy(strs,i,1)='"' then
begin
yinhao_num:=yinhao_num+1 ;
end;
if yinhao_num=2 then
begin
n:=i ;
break;
end;
end;
strs:=copy(strs,n+2,length(strs)-n-2) ;
//解析內容
j:=1;
k:=1;
yinhao_num:=0;
for i:=1 to length(strs) do
begin
if yinhao_num=6 then
begin
if copy(strs,i,1)<>'+' then
begin
sim_xinxi[j,k]:=sim_xinxi[j,k]+ copy(strs,i,1) ;
end
else
begin
yinhao_num:=0;
j:=1;
k:=k+1;
end;
end;
if yinhao_num=5 then
begin
if copy(strs,i,1)<>'"' then
begin
sim_xinxi[j,k]:=sim_xinxi[j,k]+ copy(strs,i,1) ;
end
else
begin
yinhao_num:=yinhao_num+1;
j:=j+1;
end;
end;
if yinhao_num=4 then
begin
if copy(strs,i,1)='"' then yinhao_num:=yinhao_num+1;
end;
if yinhao_num=3 then
begin
if copy(strs,i,1)<>'"' then
begin
sim_xinxi[j,k]:=sim_xinxi[j,k]+ copy(strs,i,1) ;
end
else
begin
yinhao_num:=yinhao_num+1;
j:=j+1;
end;
end;
if yinhao_num=2 then
begin
if copy(strs,i,1)='"' then yinhao_num:=yinhao_num+1;
end;
if yinhao_num=1 then
begin
if copy(strs,i,1)<>'"' then
begin
sim_xinxi[j,k]:=sim_xinxi[j,k]+ copy(strs,i,1) ;
end
else
begin
yinhao_num:=yinhao_num+1;
j:=j+1;
end;
end;
if yinhao_num=0 then
begin
if copy(strs,i,1)<>'"' then
begin
sim_xinxi[j,k]:=sim_xinxi[j,k]+ copy(strs,i,1) ;
end
else
begin
yinhao_num:=yinhao_num+1;
j:=j+1;
end;
end;
end;
// 最后一個字符段去除"OK"
sim_xinxi[j,k]:=copy(sim_xinxi[j,k],1,length(sim_xinxi[j,k])-2);
//返回短信條數
chuli_str:=k;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -