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

📄 unitmainform.pas

📁 串口短信猫收发软件。支持西门子TC35i设备
💻 PAS
📖 第 1 页 / 共 2 页
字号:

{*******************************************************}
{                                                       }
{  名称:TMainForm类                                    }
{  功能:                                               }
{      1.继承TForm类的主界面类                          }
{  调用:                                               }
{      1.MainForm是全局的,应用程序初始化后即自动创建   }
{                                                       }
{*******************************************************}

unit UnitMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus, ComCtrls, RzListVw, RzStatus, ExtCtrls,
  RzPanel, RzTray, ActnList, ImgList, RzButton, RzEdit, RzSplit, ToolWin,
  SyncObjs, IniFiles, RzTabs, RzLabel, Mask, RzBtnEdt, RzCmboBx, mmsystem,
  UnitPublicFun,
  UnitModemPool,
  UnitRecieveMsgThread, UnitSendMsgThread, UnitScanModemStatusThread, Grids;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N7: TMenuItem;
    RzStatusBar1: TRzStatusBar;
    RzStatusPane: TRzStatusPane;
    StatusPaneSendCount: TRzStatusPane;
    RzStatusPane2: TRzStatusPane;
    StatusPaneReceiveCount: TRzStatusPane;
    RzStatusPane4: TRzStatusPane;
    StatusPaneSystemRunTime: TRzStatusPane;
    Timer1: TTimer;
    RzTrayIcon: TRzTrayIcon;
    ActionList1: TActionList;
    PopupMenu1: TPopupMenu;
    D1: TMenuItem;
    C1: TMenuItem;
    MenuItem1: TMenuItem;
    A1: TMenuItem;
    MenuItem3: TMenuItem;
    X1: TMenuItem;
    Action_Exit: TAction;
    Action_Show: TAction;
    RzMarqueeStatus: TRzMarqueeStatus;
    Action_SendLog: TAction;
    Action_ReceiveLog: TAction;
    N8: TMenuItem;
    Action_About: TAction;
    Action_Setup: TAction;
    Action_SendSMS: TAction;
    N9: TMenuItem;
    d2: TMenuItem;
    s1: TMenuItem;
    f1: TMenuItem;
    Action_QuerySMS: TAction;
    Action_FeeSMS: TAction;
    ImageList1: TImageList;
    N10: TMenuItem;
    N11: TMenuItem;
    Action_Run: TAction;
    Action_Stop: TAction;
    menu_run: TMenuItem;
    menu_stop: TMenuItem;
    Action_ClearLog: TAction;
    N14: TMenuItem;
    N16: TMenuItem;
    RzToolbar1: TRzToolbar;
    BtnPlay: TRzToolButton;
    BtnStop: TRzToolButton;
    RzSpacer1: TRzSpacer;
    BtnUtilities: TRzToolButton;
    BtnDelete: TRzToolButton;
    RzSpacer2: TRzSpacer;
    BtnNewsgroups: TRzToolButton;
    BtnView: TRzToolButton;
    RzSpacer3: TRzSpacer;
    BtnInformation: TRzToolButton;
    BtnExit: TRzToolButton;
    RzSpacer4: TRzSpacer;
    RzPageControl1: TRzPageControl;
    TabSheetComm: TRzTabSheet;
    TabSheetSendSMS: TRzTabSheet;
    TabSheetRecieveSMS: TRzTabSheet;
    ListViewCommStat: TListView;
    ListViewReceiveLog: TRzListView;
    TabSheetDebug: TRzTabSheet;
    Action_Debug: TAction;
    ActionDebug1: TMenuItem;
    Panel3: TPanel;
    BtnStartDebug: TRzButton;
    BtnClearMsg: TRzButton;
    RzSplitter1: TRzSplitter;
    Panel2: TPanel;
    MemoRequestMsg: TRzMemo;
    Panel1: TPanel;
    MemoResponseMsg: TRzMemo;
    ImageList2: TImageList;
    ListViewSendLog: TRzListView;
    RzGlyphStatus: TRzGlyphStatus;
    StringGridListAll: TStringGrid;
    Action_CommStatus: TAction;
    N12: TMenuItem;
    N15: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N13: TMenuItem;
    BtnStopDebug: TRzButton;
    Action_AutoReply: TAction;
    N4: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Action_ExitExecute(Sender: TObject);
    procedure Action_ShowExecute(Sender: TObject);
    procedure Action_SendLogExecute(Sender: TObject);
    procedure Action_ReceiveLogExecute(Sender: TObject);
    procedure Action_AboutExecute(Sender: TObject);
    procedure Action_SendSMSExecute(Sender: TObject);
    procedure Action_QuerySMSExecute(Sender: TObject);
    procedure Action_FeeSMSExecute(Sender: TObject);
    procedure Action_RunExecute(Sender: TObject);
    procedure Action_StopExecute(Sender: TObject);
    procedure Action_ClearLogExecute(Sender: TObject);
    procedure ListViewCommStatCustomDrawItem(Sender: TCustomListView;
      Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure Action_SetupExecute(Sender: TObject);
    procedure BtnClearMsgClick(Sender: TObject);
    procedure Action_DebugExecute(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Action_CommStatusExecute(Sender: TObject);
    procedure BtnStartDebugClick(Sender: TObject);
    procedure BtnStopDebugClick(Sender: TObject);
    procedure ListViewReceiveLogDblClick(Sender: TObject);
    procedure ListViewSendLogDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Action_AutoReplyExecute(Sender: TObject);
    procedure RzTrayIconRestoreApp(Sender: TObject);
  private
    FDebug: boolean;
    FStringGridNowLine: integer;
    FSendMsgThread: TSendMsgThread;                                             //发送短信线程
    FRecieveMsgThread: TRecieveMsgThread;                                       //接收短信线程
    FScanModemStatusThread: TScanModemStatusThread;                             //检测设备状态线程
    FModemPool: TModemPool;                                                     //通讯设备连接池对象
    FCreationTime: TDateTime;                                                   //系统对象创建时的当前时间
    procedure LoadAllModemFromIniFile;                                          //根据配置文件初始化Modem连接池
    procedure InitListViewCommStat;                                             //初始化通讯设备显示
    procedure InitStringGridListAll;                                            //初始化显示消息表格标题等信息
    procedure StartAllThread;                                                   //创建后台线程
    procedure StopAllThread;                                                    //关闭后台线程
    procedure UpdateRunStopBtn(bRun: boolean);                                  //同步启动停止按钮状态
    procedure GetIntervalTime(const StartTime: TDateTime;
                                var Day, Hour, Min, Sec: Word);                 //获取系统运行时长
    procedure UpdateListView_CommStat(com, net, status: string);
    procedure AddListView_SendLog(s0,s1,s2,s3,s4: string);
    procedure AddStringGridListAll(s0,s1,s2,s3: string);
    procedure AddListView_ReceiveLog(s0,s1,s2,s3,s4: string);
    procedure AppException(Sender: TObject; E: Exception);
    procedure ThreadDoAfterProcessing(Sender: TObject; iDoType: integer; MsgList: TStrings);
    procedure ShowMsg(Sender: TObject; ErrorType, CommName, DoType, Msg: string);//显示消息事件
    procedure ShowDebug(Sender: TObject; com, request, response: string);//显示调试信息
  end;

var
  MainForm: TMainForm;
  
implementation

uses UnitSplashForm, UnitModem, UnitATCommandListForm,
  UnitSendSMSForm, UnitFeeCtrlForm, UnitQueryCountForm, UnitAutoReplyForm;

{$R *.dfm}

procedure TMainForm.AppException(Sender: TObject; E: Exception);
const
  SOUND_FILE = 'alarm.wav';
var
  i: integer;
begin
  //出现异常后声光告警

  //图标闪动
  RzTrayIcon.Animate := True;

  //播放告警声
  if not FileExists(ExtractFilePath(Application.ExeName) + SOUND_FILE) then Exit;
  for i:=1 to ALARMSOUND_COUNT do
  begin
    sndPlaySound(PChar(ExtractFilePath(Application.ExeName) + SOUND_FILE), SND_SYNC);
    Sleep(1000);
  end;
  
//  Application.MessageBox(PChar(E.Message), '系统异常', MB_OK + MB_ICONERROR);
//  Application.ShowException(E);
//  Application.Terminate;
end;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=27 then
  begin
    RzTrayIcon.IconIndex := 0;
    RzTrayIcon.MinimizeApp;
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Application.OnException := AppException;
  Caption := Application.Title;

  FCreationTime := Now();
  StatusPaneSystemRunTime.Hint := '本次启动时间: ' + FormatDateTime(FORMATDATETIME_DEF, FCreationTime);
  RzStatusPane4.Hint := StatusPaneSystemRunTime.Hint;                           //鼠标停靠时显示

  RzTrayIcon.Animate := False;
  RzTrayIcon.Hint := Application.Title;
  RzPageControl1.ActivePage := TabSheetComm;
  BtnClearMsg.Click;

  InitStringGridListAll;                                                        //初始化显示消息表格标题栏

  FModemPool := TModemPool.Create;                                              //创建通讯设备连接池
  LoadAllModemFromIniFile;
  
  FRecieveMsgThread := nil;
  FSendMsgThread := nil;
  FScanModemStatusThread := nil;

  Timer1.Enabled := Enabled;                                                    //打开计算系统运行时长定时器
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  if not FModemPool.StartAllModem then raise Exception.Create('连接通讯设备出错');
  InitListViewCommStat;
end;

procedure TMainForm.RzTrayIconRestoreApp(Sender: TObject);
begin
  RzTrayIcon.IconIndex := 0;
  RzTrayIcon.Animate := False;
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  //确认退出
  if Application.MessageBox('是否真的要退出系统?','提示',MB_OKCancel + MB_DEFBUTTON2 + MB_IconQuestion)=ID_OK then
    CanClose := True
  else
    CanClose := False;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //退出系统
  Timer1.Enabled := False;
  RzTrayIcon.IconIndex := 1;  
  RzTrayIcon.Animate := False;
  RzTrayIcon.Enabled := False;

  StopAllThread;
  FModemPool.StopAllModem;  
  FModemPool.Free;

  Application.ProcessMessages;
  Action := caFree;
end;

procedure TMainForm.LoadAllModemFromIniFile;
var
  i, iDeviceCount: Integer;
  sIniFileFullPath: string;
  IniFile: TIniFile;
begin
  if FModemPool=Nil then Exit;

  sIniFileFullPath := ExtractFilePath(Application.ExeName) + SYSINIFILE;
  IniFile := Nil;
  try
    IniFile := TIniFile.Create(sIniFileFullPath);

    if not FileExists(sIniFileFullPath) then                                    //如果ini配置文件不存在,则自动创建并写默认值
    begin
      IniFile.WriteInteger('System', 'TrySendCount', 0);                        //默认发送失败重试次数
      IniFile.WriteInteger('System', 'DeviceCount', 1);                         //默认设备数目
      IniFile.WriteInteger('System', 'SendIntervalTime', 1000);                 //默认发送间隔时长
      IniFile.WriteInteger('System', 'RecieveIntervalTime', 1000);              //默认接收间隔时长
      IniFile.WriteInteger('Device1', 'DeviceType', 0);                         //默认设备类型
      IniFile.WriteString('Device1', 'ComPort', 'COM1');                        //默认通讯端口
      IniFile.WriteString('Device1', 'NetDefine', '0');                         //默认网络类型:0-中国联通,1-中国移动
      FModemPool.AddModem(0, 'COM1', 0);
      Exit;
    end;

    FModemPool.ClearAllModem;
    PublicFun.FiTrySendCount := IniFile.ReadInteger('System', 'TrySendCount', 0);//失败尝试发送次数
    PublicFun.FiSendIntervalTime := IniFile.ReadInteger('System', 'SendIntervalTime', 1000);//发送间隔时长
    PublicFun.FiRecieveIntervalTime := IniFile.ReadInteger('System', 'RecieveIntervalTime', 1000);//接收间隔时长
    iDeviceCount := IniFile.ReadInteger('System', 'DeviceCount', 1);
    for i:=0 to iDeviceCount-1 do                                               //循环读取通讯设备参数
    begin
      if not (IniFile.ReadInteger('Device'+IntToStr(i+1), 'NetDefine', 0) in [0, 1]) then
      begin
        Application.MessageBox('配置文件错误,网络类型参数NetDefine值必须为0或1(0-中国联通,1-中国移动)','提示',MB_OK + MB_ICONWARNING);
        raise Exception.Create('配置文件错误,网络类型参数NetDefine值必须为0或1(0-中国联通,1-中国移动)');
      end;
      FModemPool.AddModem( IniFile.ReadInteger('Device'+IntToStr(i+1), 'DeviceType', 0),
                           IniFile.ReadString('Device'+IntToStr(i+1), 'ComPort', 'COM1'),
                           IniFile.ReadInteger('Device'+IntToStr(i+1), 'NetDefine', 0)
                          );
    end;

  finally
    IniFile.Free;
  end;
end;

procedure TMainForm.InitListViewCommStat;
var
  i: integer;
  ListItem: TListItem;
  m: TModem;
begin
  ListViewCommStat.Items.Clear;
  //新增设备列表
  for i:=0 to FModemPool.ModemList.Count-1 do
  begin
    m := TModem(FModemPool.ModemList.Objects[i]);
    if m<>nil then
    begin
      m.DebugEvent := ShowDebug;//事件映射
      ListItem := ListViewCommStat.Items.Add;
      ListItem.SubItems.Add(m.CommName);//端口
      ListItem.SubItems.Add(m.GetModemInfo);//设备名称
      ListItem.SubItems.Add(m.GetModemStatus);//状态
      case m.NetDefine of//电信运营商
        0: ListItem.SubItems.Add(CHINAUNICOM_NAME);
        1: ListItem.SubItems.Add(CHINAMOBILE_NAME);
        else ListItem.SubItems.Add('不详');
      end;
      ListItem.SubItems.Add(m.GetSIMID);//SIM卡序列号
      ListItem.SubItems.Add(m.GetSIMServerNo);//SIM卡服务中心地址
      ListItem.SubItems.Add(m.GetNetSignal);//网络信号接收强度
    end;
  end;
  if ListViewCommStat.CanFocus then ListViewCommStat.SetFocus;
  ListViewCommStat.Selected := nil;
  ListItem.Selected := True;
  ListItem.Focused := True;
end;

procedure TMainForm.GetIntervalTime(const StartTime: TDateTime; var Day, Hour, Min, Sec: Word);
var
  D, H, M, S: Double;
begin
  //计算系统运行时长
  D := Now - StartTime;
  H := (D - Trunc(D)) * 24;
  M := (H - Trunc(H)) * 60;
  S := (M - Trunc(M)) * 60;
  Day := Trunc(D);
  Hour := Trunc(H);
  Min := Trunc(M);
  Sec := Trunc(S);
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
var
  Day, Hour, Min, Sec: Word;
begin
  //定时器计算系统运行时长
  GetIntervalTime(FCreationTime, Day, Hour, Min, Sec);
  StatusPaneSystemRunTime.Caption := IntToStr(Day)
                                   + ' 天 '
                                   + IntToStr(Hour)
                                   + ' 小时 '
                                   + IntToStr(Min)
                                   + ' 分 '
                                   + IntToStr(Sec)
                                   + ' 秒 ';
end;

procedure TMainForm.Action_SetupExecute(Sender: TObject);
begin
  //系统配置
  WinExec('rundll32.exe shell32.dll,Control_RunDLL Modem.cpl', 9);  
end;

procedure TMainForm.UpdateRunStopBtn(bRun: boolean);
begin
  if bRun then
  begin
    RzTrayIcon.Animate := False;  
    menu_run.Enabled := False;
    BtnPlay.Enabled := False;
    menu_stop.Enabled := True;
    BtnStop.Enabled := True;
  end
  else
  begin
    menu_run.Enabled := True;
    BtnPlay.Enabled := True;
    menu_stop.Enabled := False;
    BtnStop.Enabled := False;
  end;
end;

procedure TMainForm.Action_RunExecute(Sender: TObject);
var
  i: integer;
  tc: TCursor;
begin
  //启动
  tc := Screen.Cursor;
  Screen.Cursor := crSQLWait;

  try
    UpdateRunStopBtn(True);
    for i:=0 to FModemPool.ModemList.Count-1 do
      UpdateListView_CommStat(FModemPool.ModemList.Strings[i], TModem(FModemPool.ModemList.Objects[i]).GetNetSignal, COMM_OPEN_STATUS);
    StartAllThread;
    Screen.Cursor := tc;

⌨️ 快捷键说明

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