📄 gsmcom.pas
字号:
startPort,endPort: Integer;
begin
PortList.Clear;
sl := TStringList.Create;
sl1 := TStringList.Create;
s := Str;
ExtractStrings([';'],[],Pchar(s),sl);
for i:=0 to sl.Count-1 do
begin
sl1.Clear;
ExtractStrings(['.'],[],PChar(sl[i]),sl1);
if sl1.Count=2 then
begin
try
startPort := StrToInt(sl1[0]);
EndPort := StrToInt(sl1[1]);
for j:=startPort to endPort do
PortList.Add(Format('%02.2d',[j]));
except
end;
end
else if sl1.Count = 1 then
begin
try
PortList.Add(Format('%02.2d',[StrToInt(sl1[0])]));
except
end;
end;
end;
sl1.Free;
sl.Free;
end;
Procedure RefreshWaitSendListView(ListView: TListView; SendItems: TSendItems);
var
i: Integer;
AList: TList;
AListItem: TListItem;
ASendItem: TGSMSendItem;
DialTimeStr: String;
begin
ListView.Items.BeginUpdate;
ListView.Clear;
try
AList := SendItems.LockList;
for i:=0 to AList.Count-1 do
begin
ASendItem :=TGSMSendItem(AList.Items[i]);
AListItem := ListView.Items.Add;
AListItem.Caption := ASendItem.MobileNo;
case ASendItem.GSMMode of
gsmMessage:
begin
AListItem.SubItems.Add('短信');
AListItem.SubItems.Add(TGSMMessageItem(ASendItem).SMSMessage);
end;
gsmDial:
begin
AListItem.SubItems.Add('拨号');
DialTimeStr := IntToStr(TGSMDialItem(ASendItem).DialTime div 1000)+'秒';
if TGSMDialItem(ASendItem).isRepeat then
if TGSMDialItem(ASendItem).RepeatCount=-1 then
AListItem.SubItems.Add('无限次'+' | 拨打时长'+DialTimeStr)
else
AListItem.SubItems.Add(IntToStr(TGSMDialItem(ASendItem).RepeatCount)+'次'+' | 拨打时长'+DialTimeStr)
else
AListItem.SubItems.Add('仅一次'+' | 拨打时长'+DialTimeStr)
end;
end;
end;
SendItems.UnlockList;
except
SendItems.UnlockList;
end;
ListView.Items.EndUpdate;
end;
Procedure RefreshListView(ListView: TListView; SendItems: TSendItems; ForceRefresh: Boolean);
var
i: Integer;
AList: TList;
AListItem: TListItem;
StartPos,EndPos: Integer;
begin
AList := SendItems.LockList;
if ForceRefresh or (ListView.Items.Count>AList.Count) then
begin
ListView.Clear;
StartPos := 0;
EndPos := AList.Count;
end
else
begin
StartPos := ListView.Items.Count;
EndPos := AList.Count;
end;
ListView.Items.BeginUpdate;
for i:=StartPos to EndPos-1 do
begin
AListItem := ListView.Items.Add;
AListItem.Caption := TGSMSendItem(AList.Items[i]).MobileNo;
end;
SendItems.UnlockList;
ListView.Items.EndUpdate;
end;
{ TGSMCom }
procedure TGSMCom.ActiveGSM;
begin
FApdGSMPhone.Connect;
// FGSMList.PostGSMComAction(self,gaActived,'','');
end;
procedure TGSMCom.GSMPhoneGSMComplete(Pager: TApdCustomGSMPhone;
State: TGSMStates; ErrorCode: Integer);
begin
case State of
gsSend:
begin
Sleep(1000);
FIsSended := true;
end;
gsConfig:
begin
FIsReady := true;
FIsSended := true;
end;
end;
end;
function TGSMCom.CheckGSM: Boolean;
begin
FIsReady := true;
Result := true;
end;
constructor TGSMCom.Create(ASet: TGSMSet;PortNo: Integer;CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FGSMSet := ASet;
FPortNo := PortNo;
FExpired := 1080; //60s expired
FLastTestTime := Now;
FIsReady := false;
FPause := false;
FreeOnTerminate := true;
FGSMDialingTime := 0;
end;
destructor TGSMCom.Destroy;
begin
end;
procedure TGSMCom.Execute;
function GetOneItem: TGSMSendItem;
begin
Result := nil;
if (FGSMSet<>nil) and (FGSMSet.Task<>nil) then
Result := FGSMSet.Task.GetOneItem;
end;
var
AGSMSendItem: TGSMSendItem;
NeedResetGSM: Boolean;
ErrorCount: Integer;
SuccCount: Integer;
RepeatError: Integer;
Tick: Cardinal;
begin
NeedResetGSM := DoInitGSM;
AGSMSendItem := nil;
ErrorCount := 0;
SuccCount := 0;
RepeatError := 0;
while (not Terminated) do
begin
if NeedResetGSM then
begin
NeedResetGSM := DoInitGSM;
if RepeatError<>0 then
RepeatError := RepeatError + 1;
if RepeatError >=20 then
PostMessage(MainForm.Handle,WM_TOOMANYERROR,FGSMSet.Index,RepeatError);
end
else if FApdGSMPhone.GSMState <> gsNone then
NeedResetGSM := DoInitGSM
else
begin
AGSMSendItem := GetOneItem;
if AGSMSendItem<>nil then
begin
case AGSMSendItem.GSMMode of
gsmMessage:
NeedResetGSM := not DoMessage(AGSMSendItem as TGSMMessageItem);
gsmDial:
begin
NeedResetGSM := not DoDial(AGSMSendItem as TGSMDialItem);
if GlobDialLowSpeed then
tick := GetTickCount;
while (not Terminated) and (GetTickCount - Tick< GlobDialDelayTime) do
begin
Application.ProcessMessages;
Sleep(SLEEPTIME);
end;
end;
end;
if NeedResetGSM then
begin
RepeatError := 1;
ErrorCount := ErrorCount + 1;
PostMessage(MainForm.Handle,WM_SENDERROR,FGSMSet.Index,ErrorCount);
Synchronize(UpdateSendFailState);
FGSMSet.FailCount := FGSMSet.FailCount + 1;
end
else
begin
RepeatError := 0;
SuccCount := SuccCount + 1;
PostMessage(MainForm.Handle,WM_SENDSUCC,FGSMSet.Index,SuccCount);
if AGSMSendItem.GSMMode <> gsmDial then
begin
Synchronize(UpdateSendedState);
FGSMSet.SendCount := FGSMSet.SendCount + 1;
end;
end;
AGSMSendItem.Free;
AGSMSendItem := nil;
FMobileNo := '';
FMSG := '';
end;
end;
Sleep(300);
end; // while
sleep(100);
Synchronize(UpdateStopState);
FreeGSMPhone;
Synchronize(UpdateStoppedState);
end;
function TGSMCom.GetComPort: Integer;
begin
Result := FPortNo;
end;
function TGSMCom.GetIsReady: Boolean;
begin
if MilliSecondsBetween(FLastTestTime,now)>FExpired then
CheckGSM;
Result := FIsReady;
end;
procedure TGSMCom.GSMPhoneNextMessage(Pager: TApdCustomGSMPhone;
ErrorCode: Integer; var NextMessageReady: Boolean);
begin
NextMessageReady := false;
end;
procedure TGSMCom.SendGSMMessage(MobileNo, MSG: String);
begin
FApdGSMPhone.SMSAddress := MobileNo;
FApdGSMPhone.SMSMessage := MSG;
FApdGSMPhone.SendMessage;
end;
procedure TGSMCom.SetComPort(const Value: Integer);
begin
if FPortNo <>Value then
begin
FApdComPort.ComNumber := Value;
FPortNo := Value;
end;
end;
procedure TGSMCom.InitGSMPhone;
var
IniFile: TINIFile;
begin
FreeGSMPhone;
FApdComPort := TApdComPort.Create(nil);
FApdComPort.AutoOpen := false;
FApdComPort.Baud := 9600;
FApdComPort.ComNumber := FPortNo;
FApdComPort.Logging := tlOff;
FApdComPort.PromptForPort := false;
FApdGSMPhone := TApdGSMPhone.Create(nil);
FApdGSMPhone.GSMMode := gmPDU;
IniFile := TIniFile.Create(WizINIFile);
FApdGSMPhone.SMSCenter := IniFile.ReadString('COMMON','SMSCenter','8613800100500');
IniFile.Free;
FApdGSMPhone.ComPort := FApdComPort;
FApdGSMPhone.QuickConnect := true;
FApdGSMPhone.TestConnect;
end;
procedure TGSMCom.SetPause(const Value: Boolean);
begin
FPause := Value;
end;
procedure TGSMCom.UpdateGSMComState(action: TGSMAction; MobileNo,
MSG: String);
var
AListItem: TListItem;
begin
AListItem := MainForm.GComListView.Items.Item[FGSMSet.Index];
if AListItem<>nil then
begin
case action of
gaActive :
ListItemString(AListItem,'激活',' ',' ','');
gaActived:
ListItemString(AListItem,'准备发送',' ',' ','');
gaSending:
ListItemString(AListItem,'发送',FMobileNo,FMSG,'');
gaSended:
ListItemString(AListItem,'发送成功',FMobileNo,FMSG,'');
gaSendFail:
ListItemString(AListItem,'发送失败',FMobileNo,FMSG,'');
gaDestroy:
ListItemString(AListItem,'停止中...',' ',' ','');
gaDestroyed:
if FGSMSet.KeepIT then
ListItemString(AListItem,'保留',' ',' ','')
else
ListItemString(AListItem,'已停止',' ',' ','');
gaReActive:
ListItemString(AListItem,'出错,重启',' ',' ','');
gaPause:
ListItemString(AListItem,'暂停',' ',' ','');
gaDial:
ListItemString(AListItem,'拨打',FMobileNo,MSG,'');
gaDialTime:
ListItemString(AListItem,'拨打',FMobileNo+': '+MSG+'秒','','');
gaHangup:
ListItemString(AListItem,'挂机',FMobileNo,'','');
gaDialError:
ListItemString(AListItem,'拨打',FMobileNo+' 失败','','');
else
ListItemString(AListItem,'不详','','','');
end;
if (MainForm.GSMComCombo.ItemIndex=0) or (MainForm.GSMComCombo.ItemIndex= FGSMSet.Index+1) then
begin
case action of
gaActived:
if MainForm.SetCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 准备发送');
gaSended:
if MainForm.SuccessCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 发送成功 : '+FMobileNo + ' '+FMSG);
gaSendFail:
if MainForm.ErrorCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 发送失败 : '+FMobileNo + ' '+FMSG);
gaPause:
if MainForm.SetCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 暂停');
gaDestroyed:
if MainForm.SetCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 停止');
gaReActive:
if MainForm.ErrorCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 重置');
gaDial:
if MainForm.SuccessCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 拨打:'+FMobileNo);
gaHangup:
if MainForm.SuccessCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 挂机:'+FMobileNo);
gaDialError:
if MainForm.ErrorCheckBox.Checked then
MainForm.WriteLog('['+Format('GSM%.2d',[FGSMSet.Port])+'] 拨打:'+FMobileNo+' 失败');
end;
end;
end;
end;
procedure TGSMCom.UpdateSendedState;
begin
UpdateGSMComState(gaSended,FMobileNo,FMSG);
end;
procedure TGSMCom.UpdateSendingState;
begin
UpdateGSMComState(gaSending,FMobileNo,FMSG);
end;
procedure TGSMCom.UpdateResetState;
begin
UpdateGSMComState(gaReActive,'','');
end;
procedure TGSMCom.UpdateActivedState;
begin
UpdateGSMComState(gaActived,'','');
end;
procedure TGSMCom.UpdatePauseState;
begin
UpdateGSMComState(gaPause,'','');
end;
procedure TGSMCom.FreeGSMPhone;
begin
try
if FApdComPort<>nil then
begin
FApdComPort.Open := false;
FApdComPort.Free;
sleep(200);
end;
except
FApdComPort := nil;
end;
FApdComPort := nil;
try
if FApdGSMPhone <>nil then
begin
FApdGSMPhone.Free;
sleep(200);
end;
except
FApdGSMPhone := nil;
end;
FApdComPort := nil;
end;
procedure TGSMCom.UpdateStopState;
begin
UpdateGSMComState(gaDestroy,'','');
end;
procedure TGSMCom.UpdateActiveState;
begin
UpdateGSMComState(gaActive,'','');
end;
procedure TGSMCom.UpdateStoppedState;
begin
FGSMSet.InThread := false;
UpdateGSMComState(gaDestroyed,'','');
end;
procedure TGSMCom.UpdateSendFailState;
begin
UpdateGSMComState(gaSendFail,FMobileNo,FMSG);
end;
function TGSMCom.DoDial(WaitItem: TGSMDialItem): Boolean;
var
i: Integer;
Tick: Integer;
begin
Result := true;
FMobileNo := WaitItem.MobileNo;
FApdGSMPhone.SMSAddress := FMobileNo;
FGSMDialingTime := 0;
try
FApdGSMPhone.SMSAddress := WaitItem.MobileNo;
FMSG := '共'+IntToStr(WaitItem.DialTime div 1000)+'秒';
Synchronize(UpdateDialState);
FApdGSMPhone.Dial;
if FApdGSMPhone.GSMState<>gsDialed then
begin
Synchronize(UpdateDialErrorState);
// Result := false;
end
else
begin
Tick := GetTickCount;
while (not Terminated) and (GetTickCount - Tick< WaitItem.DialTime) do
begin
if FApdGSMPhone.GSMState<>gsDialed then
break;
FGSMDialingTime := (GetTickCount - Tick) div 1000;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -