www.pudn.com > Testgsm.rar > Unit1.pas


unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB, fc_gsm, pub_Funcs, ExtCtrls; 
 
type 
  TForm1 = class(TForm) 
    GroupBox1: TGroupBox; 
    SendMsg: TMemo; 
    Label1: TLabel; 
    RecNum: TEdit; 
    Label2: TLabel; 
    InfoCenter: TEdit; 
    Label3: TLabel; 
    BtnSend: TButton; 
    GroupBox2: TGroupBox; 
    Label4: TLabel; 
    Label5: TLabel; 
    Label6: TLabel; 
    RecMsg: TMemo; 
    SendNum: TEdit; 
    InfoCenter2: TEdit; 
    BtnRce: TButton; 
    ListBox1: TListBox; 
    Label7: TLabel; 
    ComboBox1: TComboBox; 
    BtnOpen: TButton; 
    BtnClose: TButton; 
    MSC: TMSComm; 
    Button1: TButton; 
    Label8: TLabel; 
    Button2: TButton; 
    Button3: TButton; 
    Timer1: TTimer; 
    procedure FormCreate(Sender: TObject); 
    procedure BtnSendClick(Sender: TObject); 
    procedure Button5Click(Sender: TObject); 
    procedure BtnOpenClick(Sender: TObject); 
    procedure BtnCloseClick(Sender: TObject); 
    procedure MSCComm(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
    procedure Button3Click(Sender: TObject); 
    procedure BtnRceClick(Sender: TObject); 
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton; 
      Shift: TShiftState; X, Y: Integer); 
    procedure Button2Click(Sender: TObject); 
    procedure Timer1Timer(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
  txSn: string; 
  comflag: integer; 
  ConnFlag: integer; 
  hThread: Thandle; //定义一个句柄 
  ThreadID: Dword; 
implementation 
{$R *.dfm} 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
  ConnFlag := 0; 
  ComFlag := 0; 
end; 
 
procedure TForm1.BtnSendClick(Sender: TObject); 
var 
  CententStr, CententStr2, infocentent, mobile: widestring; 
  tem: widestring; 
begin 
  if (ConnFlag = 1) and (ComFlag = 0) then 
  begin 
    comflag := 1; 
    if (length(RecNum.Text) <> 11) and (copy(RecNum.Text, 1, 2) <> '13') and (length('xx') = 5) then 
      Application.MessageBox('提示', '接收号码有误,请重新填写') 
    else 
    begin 
      if (copy(RecNum.Text, 1, 2) <> '13') or (copy(RecNum.Text, 1, 2) <> '15') then 
        mobile := ConverMobileNum(RecNum.Text, 0) 
      else 
        mobile := ConverMobileNum(RecNum.Text, 1); 
      infocentent := ConverInfoCenter(InfoCenter.Text); 
      tem := SendMsg.Text; 
      CententStr := ContentToUnicode(tem); 
      CententStr2 := codeaddcode(mobile, CententStr); 
      RecMsg.Text := infocentent + CententStr2; 
      MSC.Output := 'AT+CMGS=' + inttostr(length(CententStr2) div 2) + #13; 
      sleep(1000); 
      tem := MSC.Input; 
      if (length(tem) > 0) then 
        MSC.Output := RecMsg.Text + ^Z; 
      sleep(2000); 
      tem := MSC.Input; 
      if pos(tem, 'OK') > 0 then 
        Application.MessageBox('发送信息失败!', '提示', MB_OK + MB_ICONWARNING) 
      else 
        Application.MessageBox('发送信息成功!', '提示', MB_OK + MB_ICONQUESTION); 
      comflag := 0; 
    end; 
  end else 
    Application.MessageBox('串口未打开', '提示', MB_OK + MB_ICONWARNING); 
end; 
 
procedure TForm1.Button5Click(Sender: TObject); 
var 
  CententStr, CententStr2, infocentent, mobile: string; 
begin 
  mobile := ConverMobileNum(RecNum.Text, 1); 
  infocentent := ConverInfoCenter(InfoCenter.Text); 
  CententStr := ContentToUnicode(SendMsg.Text); 
  CententStr2 := codeaddcode(mobile, CententStr); 
  RecMsg.Text := infocentent + CententStr2; 
  MSC.Output := 'AT+CMGS=' + inttostr(length(CententStr2) div 2) + #13; 
  sleep(1000); 
  MSC.Output := RecMsg.Text + ^Z; 
end; 
 
 
procedure TForm1.BtnOpenClick(Sender: TObject); 
var 
  readstr: widestring; 
begin 
  try 
    comflag := 1; 
    MSC._CommPort := 8; 
    MSC.PortOpen := true; 
    MSC.Output := 'ATE0' + #13; 
    sleep(1000); 
    readstr := MSC.Input; 
    if pos(readstr, 'OK') > 0 then 
    begin 
      Label8.Caption := '与手机连接不成功'; 
      MSC.PortOpen := false; 
      exit; 
    end; 
    MSC.Output := 'AT+CGMF=0' + #13; 
    sleep(1000); 
    readstr := MSC.Input; 
    MSC.Output := 'AT+CGMM' + #13; 
    sleep(1000); 
    readstr := MSC.Input; 
    if (length(readstr) > 0) then 
      Label8.Caption := copy(readstr, 3, 4) + ' 连接中......' 
    else 
    begin 
      Label8.Caption := '与手机连接不成功'; 
      MSC.PortOpen := false; 
      comflag := 0; 
      ConnFlag := 0; 
      exit; 
    end; 
    ConnFlag := 1; 
    MSC.Output := 'AT+CSCA?' + #13; 
    sleep(1000); 
    readstr := MSC.Input; 
    InfoCenter.Text := copy(readstr, 14, 11); 
    BtnOpen.Enabled := false; 
    comflag := 0; 
  except 
    begin 
      BtnOpen.Enabled := true; 
      MSC.PortOpen := false; 
      comflag := 0; 
      ConnFlag := 0; 
    end; 
  end; 
end; 
 
procedure TForm1.BtnCloseClick(Sender: TObject); 
begin 
  if comflag <> 0 then exit; 
  MSC.PortOpen := false; 
  BtnOpen.Enabled := true; 
  Label8.Caption := '未连接......'; 
  ConnFlag := 0; 
end; 
 
procedure TForm1.MSCComm(Sender: TObject); 
begin 
  sleep(1000); 
  case MSC.CommEvent of 
    comEvReceive: 
      begin 
        InsertLogfile(MSC.Input); 
      end; 
  end; 
end; 
 
procedure TForm1.Button1Click(Sender: TObject); 
var 
  X: widestring; 
  c: Integer; 
  cc: string; 
begin 
  X := '我'; 
  c := ord(X[1]); 
 // chr 
  showmessage(inttostr(c)); 
  FmtStr(cc, '%4.4X', [c]); 
  showmessage(DecodeChinese('4F60597D0021')); 
end; 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  aa: widestring; 
  s4: string; 
  i: Longint; 
begin 
  showmessage(inttoBin(16)); 
  //showmessage(IntTostr(StrToint('$' + infocenter2.text))); 
 { i := 140; 
  s4 := Format('%X', [i]); 
  showmessage(s4); 
  if length(s4) < 2 then 
    s4 := '0' + s4; 
  showmessage(s4);      } 
  //aa := '内容内' + #$D#$A'容'; 
  //showmessage(inttostr(length(infocenter2.Text))); 
end; 
 
procedure TForm1.BtnRceClick(Sender: TObject); 
var 
  ct, i: Integer; 
  tem: string; 
  sitems: TStringList; 
begin 
  ct := 0; 
  MSC.RThreshold := 0; 
  ListBox1.Items.Clear; 
  MSC.Output := 'at+cmgl=4' + #13; 
  sleep(4000); 
  tem := MSC.Input; 
  sitems := SpString(tem, #$D#$A); 
  for i := 0 to sitems.Count - 1 do 
  begin 
    tem := sitems[i]; 
    if (length(tem) > 15) then 
    begin 
      inc(ct); 
      ListBox1.Items.Add(tem); 
      InsertLogfile(tem); 
    end; 
  end; 
  for i := 1 to ct do 
  begin 
    MSC.Output := 'at+cmgd=' + inttostr(i) + #13; 
    sleep(1000); 
    MSC.Input; 
  end; 
end; 
 
 
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; 
  Shift: TShiftState; X, Y: Integer); 
var 
  vars: TStringList; 
  tem: string; 
begin 
  if ListBox1.ItemIndex <> -1 then 
  begin 
    tem := DecodeInfo(ListBox1.Items.Strings[ListBox1.ItemIndex]); 
    vars := SpString(tem, '|_|'); 
    SendNum.Text := vars[0]; 
    InfoCenter2.Text := vars[1]; 
    RecMsg.Text := vars[2]; 
  end; 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
var 
  i: integer; 
begin 
  for i := 1 to 10 do 
  begin 
    MSC.Output := 'at+cmgd=' + inttostr(i) + #13; 
    sleep(1000); 
    MSC.Input; 
  end; 
end; 
 
procedure TForm1.Timer1Timer(Sender: TObject); 
begin 
  if (ConnFlag = 1) and (comflag = 0) then 
  begin 
    comflag := 1; 
    BtnRce.Click; 
    comflag := 0; 
  end; 
end; 
 
end.