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

📄 w_main.pas

📁 短信猫收发短信的程序,通过串行口收发短信.其结构是趋向短信查询自动回传短信的系统
💻 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 + -