📄 adgsm.pas
字号:
(***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower Async Professional
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 1991-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
{*********************************************************}
{* ADGSM.PAS 4.06 *}
{*********************************************************}
{* TApdGSMPhone component *}
{*********************************************************}
(*
The TApdGSMPhone component in APRO 4 implements SMS text messaging through the
text-mode interface defined in the GSM Technical Specification 07.05, version
5.1.0, dated December 1996. There are several variations of this spec, used
throughout Nokia, Siemens, Ericsson, etc models.
We have tested the Nokia 8290 in-house, but the Nokia 7190, 8890, 6210 and 9110
models should work as well. Phones from other manufacturers will also work, as
long as they implement the text-mode interface. About 1/4 of the current phones
are capable of being connected to a PC (through IR or serial cable), about 1/3
of those are text-mode only, 1/3 are PDU mode only, and the other 1/3 support
both text and PDU mode. Some phones (such as the Nokia 5190) support SMS, but
they use a proprietary protocol, which APRO does not support. Note also that
APRO can't communicate successfully through an FBUS cable (usually found with
Nokia phones).
To test your phone, connect the phone to your PC through the serial cable or
IR device (consult your phone's documentation for details on how to connect).
Enter "AT"<CR> into a terminal window to verify the connection is established
(you should receive "OK" from the phone), then enter "AT+CMGF=?"<CR>. The
response should contain a "1", indicating that it supports text-mode. If both
of these tests pass, then your phone meets the basic requirements.
Command and response sequences are defined by the GSMXxx array consts, we
iterate from 0 to High, so you can remove certain commands from the sequence
by simply deleting that command from the array.
PDU mode was added after 4.05 and did not undergo the normal testing cycle.
Changes specific to PDU mode are marked with {!!.PDU}
Much of the information on PDU format came from http://www.dreamfabric.com/sms/
with ParseAPDUMessage parsing out the PDU messages for the listing of messages.
BuildPDUMessage will build a PDU message before sending.
*)
{Global defines potentially affecting this unit}
{$I AWDEFINE.INC}
unit adgsm;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
OoMisc, AdPort, AdPacket, AdExcept;
{$IFDEF VER110}
{ BCB3 needs this included here }
{$I AdExcept.inc}
{$ENDIF}
const
ApdGSMResponse = WM_USER + 100;
ExpiredTime = 200;
DialExpiredTime = 100;
SLEEPTIME = 1000;
type
TGSMStates = (gsNone, gsConfig, gsSendAll, gsListAll, gsSend, gsSendFStore,
gsWrite, gsDelete, gsNofify, gsDialing, gsDialed,gsHangUp, gsTest);
TApdSMSStatus = (srUnread, srRead, ssUnsent, ssSent, ssAll, ssUnknown);
{ GSM modes }
TGSMMode = (gmDetect, gmPDU, gmText); {!!.PDU}
TGSMModeSet = set of TGSMMode; {!!.PDU}
TApdCustomGSMPhone = class;
TApdGSMNextMessageEvent = procedure (Pager : TApdCustomGSMPhone;
ErrorCode : Integer; var NextMessageReady : Boolean) of object;
TApdGSMNewMessageEvent = procedure (Pager : TApdCustomGSMPhone;
FIndex : Integer; Message : string) of object;
TApdGSMMessageListEvent = procedure (Sender : TObject) of object;
TApdGSMCompleteEvent = procedure (Pager : TApdCustomGSMPhone;
State : TGSMStates; ErrorCode : Integer) of object;
TApdSMSMessage = class(TObject)
private
FMessageIndex: Integer;
FAddress: string;
FMessage: string;
FName: string;
FStatus: TApdSMSStatus;
FTimeStampStr: string;
FTimeStamp: TDateTime;
{ private declarations }
protected
{ protected declarations }
function GetMessageAsPDU : string; {!!.PDU}
procedure SetMessageAsPDU (v : string); {!!.PDU}
public
{ public declarations }
property Address : string
read FAddress write FAddress;
property Message : string
read FMessage write FMessage;
property MessageAsPDU : string {!!.PDU}
read GetMessageAsPDU write SetMessageAsPDU; {!!.PDU}
property MessageIndex : Integer
read FMessageIndex write FMessageIndex;
property Name : string
read FName write FName;
property Status : TApdSMSStatus
read FStatus write FStatus;
property TimeStamp : TDateTime
read FTimeStamp write FTimeStamp;
property TimeStampStr : string
read FTimeStampStr write FTimeStampStr;
end;
TApdMessageStore = class(TStringList)
private
{ private declarations }
FCapacity : Integer;
FGSMPhone : TApdCustomGSMPhone;
function GetMessage(Index: Integer): TApdSMSMessage;
procedure SetMessage(Index: Integer; const Value : TApdSMSMessage);
procedure SetMSCapacity(const Value: Integer);
protected
{ protected declarations }
JustClearStore : Boolean;
function GetCapacity : Integer; override;
procedure ClearStore;
public
{ public declarations }
constructor Create(GSMPhone : TApdCustomGSMPhone);
function AddMessage(const Dest, Msg : string) : Integer;
procedure Clear; override;
procedure Delete(PhoneIndex: Integer); override;
property Messages[Index: Integer]: TApdSMSMessage
read GetMessage write SetMessage; default;
property Capacity : Integer read FCapacity write SetMSCapacity;
end;
TApdCustomGSMPhone = class(TApdBaseComponent)
private
{ Private declarations }
FOnNewMessage : TApdGSMNewMessageEvent;
FOnNextMessage : TApdGSMNextMessageEvent;
FOnMessageList : TApdGSMMessageListEvent;
FOnGSMComplete: TApdGSMCompleteEvent;
FComPort: TApdCustomComPort;
FNeedNewMessage : Integer; // Flag to get a new message {!!.04}
FRecNewMess : String; {!!.06}
FConnected : Boolean; // Flag for connection status
FErrorCode: Integer;
FGSMState: TGSMStates;
FGSMMode: TGSMMode; {!!.PDU}
FHandle: THandle;
FMessage : string; // Defines Message to be sent
FMessageStore: TApdMessageStore;
FNotifyOnNewMessage: Boolean;
FQueryModemOnly: Boolean; {!!.06}
// True won't sychronize message store
FQuickConnect: Boolean;
// True won't give list of messages when first connected
FConfigList: Boolean; {!!.02}
FSMSAddress: string; // Phone number
FSMSCenter: string; // Service Center
FTempWriteMess: string; // Temp store when WriteToMemory
FPDUMode : Boolean; // PDU Mode {!!.PDU}
ResponsePacket : TApdDataPacket;
ErrorPacket : TApdDataPacket;
NotifyPacket : TApdDataPacket; {!!.02}
DialErrorPacket: TApdDataPacket;
TempSMSMessage : TApdSMSMessage;
procedure SetMessage(const Value: string);
procedure SetCenter(const Value: string);
procedure SetNotifyOnNewMessage(const Value: Boolean);
procedure SetGSMMode(const NewMode : TGSMMode); {!!.PDU}
protected
{ Protected declarations }
CmdIndex : Integer;
ResponseStr : string;
NotifyStr : string; {!!.02}
FSupportedGSMModes : TGSMModeSet; {!!.PDU}
procedure CheckPort;
procedure WndProc(var Message: TMessage);
procedure Notification(AComponent : TComponent;
Operation: TOperation); override;
procedure ResponseStringPacket(Sender: TObject; Data : string);
procedure NotifyStringPacket(Sender: TObject; Data : string); {!!.02}
procedure SetPDUMode (v : Boolean); {!!.PDU}
function GetGSMMode : TGSMMode; {!!.PDU}
procedure ErrorStringPacket(Sender: TObject; Data : string);
procedure DialErrorStringPacket(Sender: TObject; Data : string);
procedure DoFail(const Msg: string; const ErrCode: Integer);
{ these methods manage the phone's message store }
procedure DeleteFromMemoryIndex(PhoneIndex : Integer);
property Handle : THandle read FHandle;
procedure SetState(NewState : TGSMStates);
public
{ Public declarations }
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure SendMessage;
procedure Dial;
Procedure HangUp;
procedure SendAllMessages;
procedure ListAllMessages;
procedure TestConnect;
procedure Connect;
procedure SendFromMemory(TheIndex : Integer);
procedure WriteToMemory(const Dest, Msg: string);
procedure ProcessResponse;
procedure Synchronize;
procedure QueryModem;
function StatusToStr(StatusString : TApdSMSStatus) : string;
property ComPort : TApdCustomComPort
read FComPort write FComPort;
property SMSAddress : string
read FSMSAddress write FSMSAddress;
property SMSMessage : string
read FMessage write SetMessage;
property SMSCenter : string
read FSMSCenter write SetCenter;
property NotifyOnNewMessage : Boolean
read FNotifyOnNewMessage
write SetNotifyOnNewMessage default False; {!!.02}
property MessageStore : TApdMessageStore
read FMessageStore write FMessageStore;
property QuickConnect : Boolean
read FQuickConnect write FQuickConnect default False;
property GSMMode : TGSMMode {!!.PDU}
read GetGSMMode write SetGSMMode; {!!.PDU}
{ read only properties }
property SMSErrorCode : Integer
read FErrorCode;
property GSMState : TGSMStates
read FGSMState;
property OnNewMessage : TApdGSMNewMessageEvent
read FOnNewMessage write FOnNewMessage;
property OnNextMessage : TApdGSMNextMessageEvent
read FOnNextMessage write FOnNextMessage;
property OnMessageList : TApdGSMMessageListEvent
read FOnMessageList write FOnMessageList;
property OnGSMComplete : TApdGSMCompleteEvent
read FOnGSMComplete write FOnGSMComplete;
end;
TApdGSMPhone = class(TApdCustomGSMPhone)
public
constructor Create(Owner : TComponent); override;
destructor Destroy; override;
published
property ComPort;
property QuickConnect;
property GSMMode; {!!.PDU}
property SMSAddress;
property SMSMessage;
property SMSCenter;
property NotifyOnNewMessage;
{published events}
property OnNewMessage;
property OnNextMessage;
property OnMessageList;
property OnGSMComplete;
end;
function StringToPDU (v : string) : string; {!!.PDU}
function PDUToString (v : string) : string; {!!.PDU}
function StringToPDUCN(V: String): String ;
function PDUToStringCN(V: String): String;
implementation
const
{ +CMS ERROR: = Message Service Failure Result Code }
GSMConfigAvail : array[0..6] of string =
('E0', // Turn off echo
'+CMGF=?', // Support Text,PDU mode? {!!.PDU}
'+CMGF=', // Set appropriate mode
'+CSMS=0', // Select Message Service
//'+CPMS=?', // Preferred Message Storage {!!.06}
{!!.PDU} // Moved from GSMSendMessageCommands
'+CNMI= 2,1,0,1,0', // New message indication =2,1,0,1,0
// or = 2,,2,,0
'+CSMP=,167,0,0', // variable 17 for status 32 for no status
//'+CSCS?', // Set Character Set {!!.06}
'+CSDH=1'); // Show Text Code Parameters
//'+CSMP?'); // Get Default values for SMS-SUBMIT {!!.06}
GSMConfigResponse : array[0..6] of string =
('OK', // No Response Expected, just OK
'+CMGF: ', // +CMGF:[20](0,1), 0 is PDU, 1 is Text
'OK', // No Response Expected, just OK
'+CSMS: ', // Response StartString expected
//'+CPMS: ', // Response StartString expected
'OK', // No Response Expected, just OK
'OK', // No Response Expected, just OK
//'+CSCS: ', // Show default character set like "PCCP437" {!!.06}
'OK'); // No Response Expected, just OK
//'+CSMP: '); // Response StartString expected {!!.06}
GSMSendMessageCommands : array[0..3] of string =
{ Some commands moved to GSMConfigAvail and look at QueryModem procedure}
(//'+CNMI?', // Default query for new message indication
//'+CNMI= 2,1,0,1,0', // New message indication =2,1,0,1,0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -