📄 adgsm.pas
字号:
{ Clear all Messages }
procedure TApdMessageStore.Clear;
begin
inherited;exit;
// Clear All messages
while Count > 0 do begin
if Objects[Count - 1] <> nil then begin
TApdSMSMessage(Objects[Count - 1]).Free;
Objects[Count - 1] := nil;
end;
Delete(Count - 1);
end;
end;
{ Just clears our message store - not the phone }
procedure TApdMessageStore.ClearStore;
begin
JustClearStore := True;
Clear;
JustClearStore := False;
end;
{ Create Message Store }
constructor TApdMessageStore.Create(GSMPhone : TApdCustomGSMPhone);
begin
FGSMPhone := GSMPhone;
JustClearStore := False;
end;
{ issues +CMGD to Delete Message at Index }
procedure TApdMessageStore.Delete(PhoneIndex: Integer);
var
I : Integer;
begin
//PhoneIndex := FGSMPhone.MessageStore.Messages[StoreIndex].MessageIndex;
if (FGSMPhone.FConnected) and not(JustClearStore) then
FGSMPhone.DeleteFromMemoryIndex(PhoneIndex)
else begin
for I := 0 to pred(Count) do
if Messages[I].MessageIndex = PhoneIndex then
Break;
if I < Count then begin
if Objects[I] <> nil then begin
TApdSMSMessage(Objects[I]).Free;
Objects[I] := nil;
end;
inherited Delete(I);
end;
end;
end;
{ Return the phone's message store capacity }
function TApdMessageStore.GetCapacity: Integer;
begin
result := FCapacity;
end;
{ Get message number of Index }
{$IFDEF AproBCB}
function TApdMessageStore.GetSMSMessage(Index: Integer): TApdSMSMessage;
{$ELSE}
function TApdMessageStore.GetMessage(Index: Integer): TApdSMSMessage;
{$ENDIF}
begin
Result := TApdSMSMessage(Objects[Index]);
end;
{ issues + to send all messages with a Status of ssUnsent }
procedure TApdCustomGSMPhone.SendAllMessages;
var
Res : Integer;
HighIndex: Integer;
I: Integer;
begin
if FGSMState > gsNone then begin
DoFail(secSMSBusy,-8100);
Exit;
end;
if not FConnected then begin
{ Do connection/configuration stuff }
FQuickConnect := False;
Connect;
repeat
Res := SafeYield;
until (FGSMState = gsNone) or (Res = wm_Quit);
end;
CmdIndex := 0;
{ tell the GSMPhone to send all messages in it's store }
HighIndex := FMessageStore.GetCapacity;
for I := 0 to HighIndex do begin
SendFromMemory(I);
end;
end;
procedure TApdCustomGSMPhone.ProcessResponse;
{ Convert String to Integer }
function GSMStrToInt(Str : string) : Integer;
begin
while Pos(' ', Str) > 0 do
Str[Pos(' ', Str)] := '0';
Result := StrToInt(Str);
end;
var
S : string;
STemp : string;
MsgRdy : Boolean;
{ Parse a message }
function ParseAMessage : Boolean;
{ Get next field in message }
function GetNextField( aDelimiter: String ): String;
var
aDelimiterPosition: Integer;
begin
{To retrieve the text from position 1 until the occurance of aDelimiter
while advancing the pointer in S so that we can just call this function
again and again. }
if S[1] = '"' then begin
aDelimiter := '"' + aDelimiter;
end;
aDelimiterPosition := Pos( aDelimiter, S );
Result := Copy( S, 1, aDelimiterPosition - 1 );
if ( Result <> '' ) and ( Result[ 1 ] = '"' ) then
Result := Copy( Result, 2, Length( Result ) );
if ( Result <> '' ) and ( Result[ Length( Result ) ] = '"' )then
Result := Copy( Result, 1, Length( Result ) - 1 );
S := Copy( S, aDelimiterPosition + Length( aDelimiter ), Length( S ));
end;
{ Start procedure ParseAMessage }
begin
if Pos (#13#10, S) = 1 then
GetNextField (#13#10);
if Pos('+CMGL:', S) = 1 then begin
// first line of the message
// format is +CMGL: Index, status, address, address name, timestamp
TempSMSMessage := TApdSMSMessage.Create;
S := Copy(S, Pos(':', S) + 1, Length(S));
// extract the index number
TempSMSMessage.MessageIndex := GSMStrToInt(GetNextField(','));
// extract the status
STemp := GetNextField( ',' );
if STemp = 'STO UNSENT' then TempSMSMessage.FStatus := ssUnsent
else if STemp = 'STO SENT' then TempSMSMessage.FStatus := ssSent
else if STemp = 'ALL' then TempSMSMessage.FStatus := ssAll
else if STemp = 'REC UNREAD' then TempSMSMessage.FStatus := srUnread
else if STemp = 'REC READ' then TempSMSMessage.FStatus := srRead
else TempSMSMessage.FStatus := ssUnknown; {!!.01}
// Read the address field with quotes (if any)
TempSMSMessage.FAddress := GetNextField( ',' );
// Name (??) field followed by ,
TempSMSMessage.FName := GetNextField( ',' );
// DateTime Field followed by ,
TempSMSMessage.FTimeStampStr := GetNextField( ',' );
if TempSMSMessage.FTimeStampStr = '' then
TempSMSMessage.FTimeStampStr := '<no timestamp>';
// Message Type followed by ,
STemp := GetNextField( ',' );
// Message Length followed by #13#10
STemp := GetNextField( #13#10 );
// Message followed by #13#10
TempSMSMessage.Message := GetNextField( #13#10 );
end else begin
// Message more than 1 line???
TempSMSMessage.Message := TempSMSMessage.Message + #13#10 +
GetNextField( #13#10 );
end;
MessageStore.AddObject(TempSMSMessage.TimeStampStr, TempSMSMessage);
Result := Pos('+CMGL:', S) > 0; {!!.04}
end;
begin
//State machine response handle
case FGSMState of
gsConfig: begin
// Just sent +CSMS=0, successful so far, send the next command
inc(CmdIndex);
if CmdIndex > High(GSMConfigAvail) then begin
// generate the OnComplete event, we're done configuring
ResponseStr := ''; {!!.02}
if not FQuickConnect then begin {!!.02}
FGSMState := gsNone; {!!.02}
ListAllMessages; {!!.02}
end; {!!.02}
FGSMState := gsConfig; {!!.02}
if Assigned(FOnGSMComplete) then {!!.02}
FOnGSMComplete(Self, FGSMState, FErrorCode); {!!.02}
FGSMState := gsNone;
FConfigList := False; {!!.02}
end else begin
// send the next command
ResponsePacket.StartString := GSMConfigResponse[CmdIndex];
ResponsePacket.EndString := #13;
ResponsePacket.Enabled := True;
//DelayTicks(4, True); {!!.04}
FComPort.Output := 'AT' + GSMConfigAvail[CmdIndex] + #13;
end;
end;
gsSend: begin
// Just sent CNMI, successful so far, send the next command
inc(CmdIndex);
if CmdIndex > High(GSMSendMessageResponse) then begin
// Sent message - see if another message is ready
if Assigned(FOnNextMessage) then begin
MsgRdy := False;
FOnNextMessage(Self, FErrorCode, MsgRdy);
if MsgRdy then begin
{ Will inc(CmdIndex) and start with the CSCA setting }
CmdIndex := 2;
PostMessage(FHandle, ApdGSMResponse, 0,0);
end;
end;
// generate the OnComplete event, we're done
if Assigned(FOnGSMComplete) then begin
FOnGSMComplete(Self, FGSMState, FErrorCode);
end;
FGSMState := gsNone;
ResponseStr := ''; {!!.02}
end else begin
if CmdIndex = High(GSMSendMessageCommands) - 1 then begin
// send the next command
ResponsePacket.StartString :=
GSMSendMessageResponse[CmdIndex];
ResponsePacket.EndString := '>'#32;
ResponsePacket.Enabled := True;
//DelayTicks(4, True); {!!.04}
S := SMSAddress;
if S = '' then begin {!!.02}
DoFail(secBadOperation,-8302); {!!.02}
Exit; {!!.02}
end; {!!.02}
if S[1] <> '"' then {begin} {!!.01}
S := '"' + S + '"'; {!!.01}
//else {!!.01}
//if S[2] <> '+' then {!!.01}
//S := '"+' + S + '"'; {!!.01}
//end; {!!.01}
S := S + #13#10;
FComPort.Output := 'AT' + GSMSendMessageCommands[CmdIndex]
+ S;
end else begin
if CmdIndex = High(GSMSendMessageCommands) then begin
ResponsePacket.StartString :=
GSMSendMessageResponse[CmdIndex];
ResponsePacket.EndString := #13#10;
ResponsePacket.Enabled := True;
//DelayTicks(4, True); {!!.04}
STemp := FMessage;
if STemp[Length(STemp)] <> #26 then
STemp := STemp + #26;
FComPort.Output := STemp;
end else begin
ResponsePacket.StartString :=
GSMSendMessageResponse[CmdIndex];
ResponsePacket.Enabled := True;
//DelayTicks(4, True); {!!.04}
if GSMSendMessageCommands[CmdIndex] = '+CSCA=' then begin
if (FSMSCenter <> '') and
(GSMSendMessageCommands[CmdIndex]='+CSCA=') then begin
if FSMSCenter[1] = '"' then {begin} {!!.01}
FSMSCenter := copy(FSMSCenter, 2, Length(FSMSCenter));
{*if FSMSCenter[2] = '+' then
{FSMSCenter := copy(FSMSCenter, 3,
{Length(FSMSCenter));*} {!!.01}
if FSMSCenter[Length(FSMSCenter)] = '"' then {!!.01}
FSMSCenter := copy(FSMSCenter, 1, {!!.01}
Length(FSMSCenter)-1); {!!.01}
//end; {!!.01}
FComport.Output := 'AT' +
GSMSendMessageCommands[CmdIndex]+'"' {!!.01}
+ FSMSCenter + '"' + #13
end else begin
inc(CmdIndex);
ResponsePacket.StartString :=
GSMSendMessageResponse[CmdIndex];
ResponsePacket.EndString := '>'#32;
ResponsePacket.Enabled := True;
//DelayTicks(4, True); {!!.04}
S := SMSAddress;
if S = '' then begin {!!.02}
DoFail(secBadOperation,-8302); {!!.02}
Exit; {!!.02}
end; {!!.02}
if S[1] <> '"' then {begin} {!!.01}
S := '"' + S + '"'; {!!.01}
//else {!!.01}
//if S[2] <> '+' then {!!.01}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -