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

📄 main.~pas

📁 GSM 模块软件开发包 开发包说明 开发包基于GSM模块开发
💻 ~PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls,Registry, ExtCtrls;

type
  TFrmMain = class(TForm)
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    EdtMobile: TEdit;
    Label1: TLabel;
    EdtMessage: TEdit;
    BtnSend: TButton;
    GroupBox2: TGroupBox;
    BtnOpen: TButton;
    BtnClose: TButton;
    CbPorts: TComboBox;
    StaticText1: TStaticText;
    Memo1: TMemo;
    Timer1: TTimer;
    BtnGetSCA: TButton;
    EdtNextMsgToSend: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    EdtUnSendCount: TEdit;
    StaticText2: TStaticText;
    EdtSCA: TEdit;
    BtnSetSCA: TButton;
    procedure BtnOpenClick(Sender: TObject);
    procedure BtnSendClick(Sender: TObject);
    procedure BtnCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure BtnGetSCAClick(Sender: TObject);
    procedure BtnSetSCAClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function OpenComm(CommIndex: LongInt): LongInt; stdcall;
function CloseComm(CommIndex: LongInt): LongInt; stdcall;
function SendMsg(CommIndex: LongInt; msg,mobileNo: Pchar;Msg_Index:LongInt;chinese:boolean): LongInt; stdcall;
function GetNewMsg(CommIndex: LongInt; NewMsg:Pchar): boolean; stdcall;
function GetUnSendCount(CommIndex:LongInt):LongInt;stdcall;
function GetSCA(CommIndex:LongInt;SCA:Pchar):Boolean;stdcall;
function SetSCA(CommIndex:LongInt;SCA:Pchar):Boolean;stdcall;
function ForceCloseComm(CommIndex:LongInt):LongInt;stdcall;
function GetFailedMsg(CommIndex:LongInt;Msg:Pchar):Boolean;stdcall;
function GetNextSendMsg(CommIndex:LongInt;Msg:Pchar;DeleteAfterRead:Boolean):Boolean;stdcall;

var
  FrmMain: TFrmMain;
  index:integer;
  ModuleStatus:array of Boolean;
implementation

{$R *.DFM}

function OpenComm; external 'AscendSMS.dll';
function CloseComm; external 'AscendSMS.dll';
function SendMsg; external 'AscendSMS.dll';
function GetNewMsg;external 'AscendSMS.dll';
function GetUnSendCount;external 'AscendSMS.dll';
function GetSCA;external 'AscendSMS.dll';
function SetSCA;external 'AscendSMS.dll';
function ForceCloseComm;external 'AscendSMS.dll';
function GetFailedMsg;external 'AscendSMS.dll';
function GetNextSendMsg;external 'AscendSMS.dll';

procedure TFrmMain.BtnOpenClick(Sender: TObject);
var Rst:Integer;
begin
	if ModuleStatus[CbPorts.ItemIndex] then
  begin
  	StatusBar1.Panels[0].Text:='模块已打开';
    Exit;
  end;
  Rst:=OpenComm(CbPorts.ItemIndex+1);
  if  Rst=0 then
  begin
    StatusBar1.Panels[0].Text := '打开模块成功';
    ModuleStatus[CbPorts.ItemIndex]:=True;
  end
  else
  begin
  	StatusBar1.Panels[0].Text := '打开模块失败';
    ShowMessage(IntToStr(Rst));
  end;
end;

procedure TFrmMain.BtnSendClick(Sender: TObject);
begin
	if not ModuleStatus[CbPorts.ItemIndex] then
  begin
  	StatusBar1.Panels[0].Text:='模块未打开';
    Exit;
  end;

  if SendMsg(CbPorts.ItemIndex+1,Pchar(EdtMessage.Text),Pchar(EdtMobile.Text),Index,True) = 0 then
  begin
    StatusBar1.Panels[1].Text := '发送成功';
    index:=Index+1;
  end
  else
    StatusBar1.Panels[1].Text := '发送失败';
end;

procedure TFrmMain.BtnCloseClick(Sender: TObject);
begin
	if not ModuleStatus[CbPorts.ItemIndex] then
  begin
  	StatusBar1.Panels[0].Text:='模块未打开';
    Exit;
  end;
  if CloseComm(CbPorts.ItemIndex+1) = 0 then
  begin
    StatusBar1.Panels[0].Text := '关闭模块成功';
    StatusBar1.Panels[1].Text := '';
    ModuleStatus[CbPorts.ItemIndex]:=False;
  end
  else
  if Application.MessageBox('关闭模块失败,可能还有未处理短信,是否强制关闭?','提示',MB_OKCANCEL)=ID_OK then
  	if ForceCloseComm(CbPorts.ItemIndex+1)=0 then
    begin
    	StatusBar1.Panels[0].Text:='模块已强制关闭';
    	ModuleStatus[CbPorts.ItemIndex]:=False;
    end
    else StatusBar1.Panels[0].Text:='强制关闭模块失败';
end;

procedure TFrmMain.FormShow(Sender: TObject);
var
  RegFile : TRegIniFile;
  Ports : TStringList;
  i : Integer;
begin
  RegFile := TRegIniFile.Create;
  Ports := TStringList.Create;
  try
    RegFile.RootKey := HKey_Local_Machine;
    if RegFile.OpenKey('\Hardware\Devicemap',False) then
    begin
      RegFile.ReadSectionValues('SerialComm', Ports);
      if Ports.Count > 0 then
      begin
        SetLength(ModuleStatus,Ports.Count);
        for i := 0 to Ports.Count-1 do
        begin
          CbPorts.Items.Add('COM'+IntToStr(i+1));
          ModuleStatus[i]:=False;
        end;
        CbPorts.ItemIndex := 0;
      end;
    end
    else
      ShowMessage('无法从注册表中获取串口配置');
  finally
    RegFile.CloseKey;
    RegFile.Free;
    Ports.Free;
  end;
  Index:=1;
end;

procedure TFrmMain.Timer1Timer(Sender: TObject);
var
  RecNewMsg,SendFailedMsg,UnSendMsg:pchar;
  i:Integer;
begin
  GetMem(RecNewMsg,200);
  GetMem(SendFailedMsg,200);
  GetMem(UnSendMsg,200);
  for i:=0 to Length(ModuleStatus)-1 do
  begin
  	if ModuleStatus[i] and GetNewMsg(i+1, RecNewMsg) then
  		Memo1.Lines.Add('收到短信:'+RecNewMsg)
    else if RecNewMsg='Module Error' then
    begin
    	Memo1.Lines.Add('出错提示:'+'模块'+IntToStr(i+1)+'无响应! '+DateTimeToStr(now));
      ModuleStatus[i]:=False;
    end;
    if ModuleStatus[i] and GetFailedMsg(i+1,SendFailedMsg) then
    	Memo1.Lines.Add('发送短信失败:'+SendFailedMsg);
  end;

	if ModuleStatus[CbPorts.ItemIndex] then
  begin
  	try
    	EdtUnSendCount.Text:=IntToStr(GetUnSendCount(CbPorts.ItemIndex+1))
    except
    	EdtUnSendCount.Text:='';
    end;
   end
  else EdtUnSendCount.Text:='';

  if ModuleStatus[CbPorts.ItemIndex] then
  begin
  	if  GetNextSendMsg(CbPorts.ItemIndex+1,UnSendMsg,False) then
     EdtNextMsgToSend.Text:=UnSendMsg
  	else EdtNextMsgToSend.Text:='';
  end
  else EdtNextMsgToSend.Text:='';

  FreeMem(UnSendMsg);
  FreeMem(RecNewMsg);
  FreeMem(SendFailedMsg);
end;

procedure TFrmMain.BtnGetSCAClick(Sender: TObject);
var SCA:Pchar;
begin
	if not ModuleStatus[CbPorts.ItemIndex] then
  begin
  	StatusBar1.Panels[0].Text:='模块未打开';
    Exit;
  end;

  GetMem(SCA,64);
  if GetSCA(CbPorts.ItemIndex+1,SCA) and (SCA<>'') then
  begin
  	EdtSCA.Text:=SCA;
    StatusBar1.Panels[0].Text:='读取短信中心号码成功';
  end
  else StatusBar1.Panels[0].Text:='读取短信中心号码失败';
  FreeMem(SCA);
end;

procedure TFrmMain.BtnSetSCAClick(Sender: TObject);
begin
	if not ModuleStatus[CbPorts.ItemIndex] then
  begin
  	StatusBar1.Panels[0].Text:='模块未打开';
    Exit;
  end;

  if (EdtSCA.Text='') or (EdtSCA.Text=' ') then
  begin
  	StatusBar1.Panels[0].Text:='短信中心号码不能为空';
    Exit;
  end;

  if not SetSCA(CbPorts.ItemIndex+1,Pchar(EdtSCA.Text)) then
  	StatusBar1.Panels[0].Text:='设置短信中心号码失败'
  else StatusBar1.Panels[0].Text:='设置短信中心号码成功';
end;

end.

⌨️ 快捷键说明

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