📄 unitrecievemsgthread.pas
字号:
{*******************************************************}
{ }
{ 名称:TRecieveMsgThread类 }
{ 功能: }
{ 1.接收收短信线程 }
{ 调用: }
{ 1.随主窗体创建,按启动按钮后开始执行 }
{*******************************************************}
unit UnitRecieveMsgThread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ComCtrls, RzListVw, RzStatus, UnitPublicFun, UnitModemPool, UnitModem;
type
TRecieveMsgThread = class(TThread)
private
FSleepInterval: integer;
FMPool: TModemPool;
FMsgList: TStrings;
FErrorType, FCommName, FDoType, FMsg: string;
FMsgNum: integer;
FMsgArray: array[0..(MAX_PER_DO_NUM-1)] of TRecieveSMS; //存放接收短信的记录数组
FAfterProcessing: TAfterProcessingEvent;
FShowMsg: TShowMsgEvent;
procedure CallAfterProcessing;
procedure CallShowMsg;
procedure RecieveMsg;
procedure ClearMsgArray;
protected
procedure Execute; override;
procedure DoAfterProcessing;
procedure DoShowMsg;
public
constructor Create(MPool: TModemPool; CreateSuspended: Boolean; SleepInterval: integer);
destructor Destroy; override;
property AfterProcessing: TAfterProcessingEvent read FAfterProcessing write FAfterProcessing;
property ShowMsg: TShowMsgEvent read FShowMsg write FShowMsg;
end;
implementation
uses UnitDM;
{ TRecieveMsgThread }
procedure TRecieveMsgThread.DoAfterProcessing;
begin
if Assigned(FAfterProcessing) then Synchronize(CallAfterProcessing);
end;
procedure TRecieveMsgThread.CallAfterProcessing;
begin
if Assigned(FAfterProcessing) then
FAfterProcessing(Self, DORECIEVEMSG, FMsgList);
end;
constructor TRecieveMsgThread.Create(MPool: TModemPool;
CreateSuspended: Boolean; SleepInterval: integer);
begin
FMPool := MPool;
FSleepInterval := SleepInterval;
FreeOnTerminate := True;
FDoType := DOTYPE_RECIEVEMSG;
FMsgList := TStringList.Create;
inherited Create(CreateSuspended);
end;
destructor TRecieveMsgThread.Destroy;
begin
FMsgList.Free;
inherited;
end;
procedure TRecieveMsgThread.Execute;
begin
while not Terminated do
begin
try
{
FErrorType := '';
FCommName := DOTYPE_RECIEVEMSG;
FMsg := '开始接收短信...';
DoShowMsg;
}
RecieveMsg;
{
FErrorType := '';
FCommName := DOTYPE_RECIEVEMSG;
FMsg := '结束接收短信!';
DoShowMsg;
}
Sleep(FSleepInterval);
except
continue;
end;
end;
end;
procedure TRecieveMsgThread.RecieveMsg;
var
i: integer;
bResult: boolean;
m: TModem;
begin
m := FMPool.LookupRandomModem;
if m=nil then
begin
FErrorType := DO_ERROR;
FCommName := SYSTEM_DEVICE;
FMsg := '查找通讯设备失败';
DoShowMsg;
Exit;
end;
FCommName := m.CommName;
FMsgNum := 0;
ClearMsgArray;
bResult := m.RecievePduSMS(True, FMsgNum, FMsgArray);
if not bResult then
begin
FErrorType := DO_ERROR;
FMsg := '接收短信错误';
DoShowMsg;
Exit;
end;
if FMsgNum<=0 then
begin
FErrorType := '';
FMsg := '等待接收短信...';
DoShowMsg;
Exit;
end;
for i:=0 to FMsgNum-1 do
begin
DM.AddResponse(FormatDateTime(FORMATDATETIME_DEF, Now), FMsgArray[i].MsgMobile, FMsgArray[i].MsgData, FMsgArray[i].MsgDate);
FMsgList.Clear;
FMsgList.Add(m.CommName);
FMsgList.Add(FMsgArray[i].MsgIndex);//序号
FMsgList.Add(FMsgArray[i].MsgMobile);//号码
FMsgList.Add(FMsgArray[i].MsgDate);//时间
FMsgList.Add(FMsgArray[i].MsgData);//内容
DoAfterProcessing;//触发主窗体调用事件
FErrorType := '';
FMsg := '接收短信成功['+FMsgArray[i].MsgMobile+']';
DoShowMsg;
if DM.FbAutoReply then//自动回复短信
begin
if DM.AddAutoReply(FormatDateTime(FORMATDATETIME_DEF, Now), FMsgArray[i].MsgMobile, FMsgArray[i].MsgData) then
begin
FErrorType := '';
FMsg := '自动回复检查['+FMsgArray[i].MsgMobile+']';
end
else
begin
FErrorType := DO_ERROR;
FMsg := '自动回复错误['+FMsgArray[i].MsgMobile+']';
end;
DoShowMsg;
end;
end;
end;
procedure TRecieveMsgThread.CallShowMsg;
begin
if Assigned(FShowMsg) then
FShowMsg(Self, FErrorType, FCommName, FDoType, FMsg);
end;
procedure TRecieveMsgThread.DoShowMsg;
begin
if Assigned(FShowMsg) then Synchronize(CallShowMsg);
end;
procedure TRecieveMsgThread.ClearMsgArray;
var
i: integer;
begin
for i:=0 to MAX_PER_DO_NUM-1 do
begin
FMsgArray[i].MsgIndex := NO_SMS;
FMsgArray[i].MsgDate := '';
FMsgArray[i].MsgMobile := '';
FMsgArray[i].MsgData := '';
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -