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

📄 u_msc.~pas

📁 串口发短信的例子
💻 ~PAS
字号:
unit U_MSC;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SPComm, OleCtrls, MSCommLib_TLB,StrUtils, ComCtrls,
  Menus;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button3: TButton;
    ComboBox1: TComboBox;
    Label1: TLabel;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Memo2: TMemo;
    ComboBox4: TComboBox;
    Label6: TLabel;
    ComboBox5: TComboBox;
    ComboBox6: TComboBox;
    Button2: TButton;
    StatusBar1: TStatusBar;
    Comm1: TComm;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    OpenDialog1: TOpenDialog;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    procedure Button1Click(Sender: TObject);
    procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
      BufferLength: Word);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox3Change(Sender: TObject);
    procedure ComboBox2Change(Sender: TObject);
    procedure ComboBox5Change(Sender: TObject);
    procedure ComboBox6Change(Sender: TObject);
    procedure ComboBox4Change(Sender: TObject);
    procedure Memo2Change(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ComboBox5KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure N3Click(Sender: TObject);
  private
    { Private declarations }
   function send_msg(Comm:TComm;csca_mobile_tem,mobile_tem,msg_tem:string):boolean;
   function com_write(Comm:TComm;str_tem:string):boolean;
  public
    { Public declarations }
     set_ini_list,all_no_list:TStringList;
     con_send:boolean;
     comm_state:integer;
  end;

var
  Form1: TForm1;

implementation
    uses u_sms;
{$R *.dfm}
function TForm1.send_msg(Comm:TComm;csca_mobile_tem,mobile_tem,msg_tem:string):boolean;
 var sss,str1:string ;
 len1:integer;
begin
 str1:='at+cmgs='+inttostr(send_msg_1(csca_mobile_tem,mobile_tem,msg_tem,sss));
 len1:=length(str1);
 Comm.WriteCommData(pchar(str1+#13),len1+1);
 sleep(200);
 str1:=trim(sss);
 len1:=length(str1);
 result:=Comm.WriteCommData(pchar(str1+#26),len1+1);
end;

function TForm1.com_write(Comm:TComm;str_tem:string):boolean;
begin
result:=Comm.WriteCommData(pchar(str_tem+#13),length(str_tem)+1)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin


if Button1.Caption='启动' then
begin
        Comm1.BaudRate:=strtoint(ComboBox3.Text);
        Comm1.CommName:=ComboBox1.Text;

      try
        Comm1.StartComm;
        sleep(200);
        if com_write(Comm1,'ate0')then comm_state:=1; 
        Button1.Caption:='关闭';
    except
       on E: Exception do showmessage('端口错误:'+e.message);
    end;
end
else
begin
     try

      Comm1.StopComm;
      Button1.Caption:='启动';
      StatusBar1.Panels.Items[0].Text:='未连接';

    except
    on E: Exception do showmessage('端口错误:'+e.message);
    end ;
end;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
  var rec_srting:string;
    tem_StringList:TStringList;
begin
 StatusBar1.Panels.Items[1].Text:=inttostr(all_no_list.Count);
 all_no_list.SaveToFile(ExtractFilePath(Application.ExeName)+'sy.txt');
IF  BufferLength>0 THEN
begin
rec_srting:= pchar(Buffer);
memo1.Lines.Add(pchar(Buffer));
end;

case   comm_state of
1:begin
    if (AnsiContainsStr(rec_srting,'OK')) THEN
     StatusBar1.Panels.Items[0].Text:=' 已连接';
  end;

10:begin
    if (AnsiContainsStr(rec_srting,'+CMGS:')) THEN
   begin
    if  (all_no_list.Count>0) then
    begin
    con_send:=true;
    all_no_list.Delete(0);

    end;
   end
   else if (AnsiContainsStr(rec_srting,'ERROR')) THEN
   begin
    if  (all_no_list.Count>0) then con_send:=true;
   end
   else
     con_send:=false;
  end;
else
  begin

  end;
end;


 if  (con_send)AND (all_no_list.Count>0) then
  begin
   try
    tem_StringList:=TStringList.Create;
    tem_StringList.Clear;
   except

   end;
    tem_StringList.DelimitedText:=all_no_list.Strings[0];
    if tem_StringList.Count=2 then
    send_msg(Comm1,ComboBox6.Text,tem_StringList.Strings[0],tem_StringList.Strings[1]);
    comm_state:=10;
    tem_StringList.Free;
  end
  else
  begin
   con_send:=false;
  end;
StatusBar1.Panels.Items[1].Text:=inttostr(all_no_list.Count);  
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Comm1.WriteCommData(pchar(ComboBox2.Text+#13),length(ComboBox2.Text)+1)
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

 if (send_msg(Comm1,ComboBox6.Text,ComboBox5.Text,memo2.Text))then
 showmessage('发送成功');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
try
set_ini_list:=TStringList.Create;
set_ini_list:=Read_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set',10);
except
set_ini_list.Free;
end;

all_no_list:=TStringList.Create();
all_no_list.Clear;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
all_no_list.SaveToFile(ExtractFilePath(Application.ExeName)+'sy.txt');
set_ini_list.Free;
all_no_list.Free;
end;

procedure TForm1.FormActivate(Sender: TObject);
var  tem_list:TStringList;
begin
tem_list:=TStringList.Create;

tem_list.DelimitedText:=set_ini_list.Strings[0];
ComboBox1.Text:=tem_list.Strings[0];
ComboBox1.Items.DelimitedText:=set_ini_list.Strings[0];

tem_list.DelimitedText:=set_ini_list.Strings[1];
ComboBox3.Text:=tem_list.Strings[0];
ComboBox3.Items.DelimitedText:=set_ini_list.Strings[1];

tem_list.DelimitedText:=set_ini_list.Strings[2];
ComboBox2.Text:=tem_list.Strings[0];
ComboBox2.Items.DelimitedText:=set_ini_list.Strings[2];

tem_list.DelimitedText:=set_ini_list.Strings[3];
ComboBox5.Text:=tem_list.Strings[0];
ComboBox5.Items.DelimitedText:=set_ini_list.Strings[3];

tem_list.DelimitedText:=set_ini_list.Strings[4];
ComboBox6.Text:=tem_list.Strings[0];
ComboBox6.Items.DelimitedText:=set_ini_list.Strings[4];

tem_list.DelimitedText:=set_ini_list.Strings[5];
ComboBox4.Text:=tem_list.Strings[0];
ComboBox4.Items.DelimitedText:=set_ini_list.Strings[5];

memo2.Text:=trim(set_ini_list.Strings[6]);


tem_list.Free;

IF  FileExists(ExtractFilePath(Application.ExeName)+'sy.txt')THEN
inport_no(ExtractFilePath(Application.ExeName)+'sy.txt','0',all_no_list);

StatusBar1.Panels.Items[1].Text:=inttostr(all_no_list.Count);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set0',trim(ComboBox1.Text)+','+trim(ComboBox1.Items.DelimitedText));

end;

procedure TForm1.ComboBox3Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set1',trim(ComboBox3.Text)+','+trim(ComboBox3.Items.DelimitedText));

end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set2',trim(ComboBox2.Text)+','+trim(ComboBox2.Items.DelimitedText));
end;

procedure TForm1.ComboBox5Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set3',trim(ComboBox5.Text)+','+trim(ComboBox5.Items.DelimitedText));
end;

procedure TForm1.ComboBox6Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set4',trim(ComboBox6.Text)+','+trim(ComboBox6.Items.DelimitedText));
end;

procedure TForm1.ComboBox4Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set5',trim(ComboBox4.Text)+','+trim(ComboBox4.Items.DelimitedText));
end;

procedure TForm1.Memo2Change(Sender: TObject);
begin
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set6',trim(memo2.Text));
end;

procedure TForm1.N2Click(Sender: TObject);
var path_str:string;

begin

  if OpenDialog1.Execute then
  begin
  path_str:=OpenDialog1.FileName;

    if FileExists(path_str) then
    begin
     inport_no(path_str,'0',all_no_list);
    end;

  end;
 all_no_list.SaveToFile(ExtractFilePath(Application.ExeName)+'sy.txt');
 StatusBar1.Panels.Items[1].Text:=inttostr(all_no_list.Count);
end;

procedure TForm1.N7Click(Sender: TObject);
var  tem_StringList:TStringList;
begin

 if all_no_list.Count>0 then
 begin
 try
  tem_StringList:=TStringList.Create;
  tem_StringList.Clear;
 except

 end;
  tem_StringList.DelimitedText:=all_no_list.Strings[0];
  if tem_StringList.Count=2 then
  send_msg(Comm1,ComboBox6.Text,tem_StringList.Strings[0],tem_StringList.Strings[1])
  else
  showmessage('文件格式错误!');
  
  tem_StringList.Free;
  comm_state:=10;
 end
 else
 showmessage('请导入数据!!');
end;

procedure TForm1.N6Click(Sender: TObject);
begin
if con_send then  con_send:=false;
end;

procedure TForm1.ComboBox5KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=13 then
begin
ComboBox5.Items.Add(ComboBox5.Text);
save_inifile(ExtractFilePath(Application.ExeName)+'setup.ini','Option','set3',trim(ComboBox5.Text)+','+trim(ComboBox5.Items.DelimitedText));

end;
end;

procedure TForm1.N3Click(Sender: TObject);
begin
all_no_list.Clear;
all_no_list.SaveToFile(ExtractFilePath(Application.ExeName)+'sy.txt');
StatusBar1.Panels.Items[1].Text:=inttostr(all_no_list.Count);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -