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

📄 adgsm.pas

📁 Async Professional 4.04
💻 PAS
📖 第 1 页 / 共 4 页
字号:
{*********************************************************}
{*                    ADGSM.PAS 4.04                     *}
{*         Copyright (C) TurboPower Software 2002        *}
{*                 All rights reserved.                  *}
{*********************************************************}

{Global defines potentially affecting this unit}
{$I AWDEFINE.INC}

unit adgsm;
interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms,
  OoMisc, AdPort, AdPacket, AdExcept;

{$IFDEF AproBCB}
{*$HPPEMIT '' }
{*$HPPEMIT '#undef GetMessage' }
{*$HPPEMIT '#undef SendMessage' }
{*$HPPEMIT '' }
{$ENDIF}
{$IFDEF VER110}
  { BCB3 needs this included here }
  {$I AdExcept.inc}
{$ENDIF}
const
  ApdGSMResponse = WM_USER + 100;

type
  TGSMStates = (gsNone, gsConfig, gsSendAll, gsListAll, gsSend, gsSendFStore,
                gsWrite, gsDelete, gsNofify);

  TApdSMSStatus = (srUnread, srRead, ssUnsent, ssSent, ssAll, ssUnknown);{!!.01}

  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 }
  public
    { public declarations }

    property Address : string
      read FAddress write FAddress;
    property Message : string
      read FMessage write FMessage;
    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;
    {$IFDEF AproBCB}
    function GetSMSMessage(Index: Integer): TApdSMSMessage;
    {$ELSE}
    function GetMessage(Index: Integer): TApdSMSMessage;
    {$ENDIF}
    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;
    {$IFDEF AproBCB}
    property Messages[Index: Integer]: TApdSMSMessage
             read GetSMSMessage write SetMessage; default;
    {$ELSE}      
    property Messages[Index: Integer]: TApdSMSMessage
             read GetMessage write SetMessage; default;
    {$ENDIF}
    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}
    FConnected : Boolean;                // Flag for connection status
    FErrorCode: Integer;
    FGSMState: TGSMStates;
    FHandle: THandle;
    FMessage : string;                   // Defines Message to be sent
    FMessageStore: TApdMessageStore;
    FNotifyOnNewMessage: Boolean;

    // 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

    ResponsePacket : TApdDataPacket;
    ErrorPacket : TApdDataPacket;
    NotifyPacket : TApdDataPacket;                                       {!!.02}
    TempSMSMessage : TApdSMSMessage;

    procedure SetMessage(const Value: string);
    procedure SetCenter(const Value: string);
    procedure SetNotifyOnNewMessage(const Value: Boolean);

  protected
    { Protected declarations }
    CmdIndex : Integer;
    ResponseStr : string;
    NotifyStr : string;                                                  {!!.02}
    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 ErrorStringPacket(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;
    {$IFDEF AproBCB}
    procedure SendSMSMessage;
    {$ELSE}
    procedure SendMessage;
    {$ENDIF}
    procedure SendAllMessages;
    procedure ListAllMessages;
    procedure Connect;
    procedure SendFromMemory(TheIndex : Integer);
    procedure WriteToMemory(const Dest, Msg: string);
    procedure ProcessResponse;
    procedure Synchronize;

    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;
    { 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)

  published
    property ComPort;
    property QuickConnect;
    property SMSAddress;
    property SMSMessage;
    property SMSCenter;
    property NotifyOnNewMessage;

    {published events}
    property OnNewMessage;
    property OnNextMessage;
    property OnMessageList;
    property OnGSMComplete;
    
  end;

implementation
{$IFDEF TRIALRUN}
  {$I TRIAL07.INC}
  {$I TRIAL03.INC}
  {$I TRIAL01.INC}
{$ENDIF}


const
  { +CMS ERROR:  =  Message Service Failure Result Code }
  GSMConfigAvail : array[0..6] of string =
              ('E0',
              '+CSMS=0',           // Select Message Service
               '+CPMS=?',           // Preferred Message Storage
               '+CMGF=1',           // Text Mode
               '+CSCS?',            // Set Character Set
               '+CSDH=1',           // Show Text Code Parameters
               '+CSMP?');           // Get Default values for SMS-SUBMIT
  GSMConfigResponse : array[0..6] of string =
              ('OK',
              '+CSMS: ',           // Response StartString expected
               '+CPMS: ',           // Response StartString expected
               'OK',                // No Response Expected, just OK
               '+CSCS: ',           // Show default character set like "PCCP437"
               'OK',                // No Response Expected, just OK
               '+CSMP: ');          // Response StartString expected

  GSMSendMessageCommands : array[0..6] of string =
    { leaving the first command commented out, will use this for querying later } 
               (//'+CNMI?',           // Default query for new message indication
                '+CNMI= 2,1,0,1,0',    // New message indication =2,1,0,1,0 or = 2,,2,,0
                '+CSCB?',           // Types of CBMs to be received by the ME
                '+CSMP=32,167,0,0', // status report wanted; otherwise default
                '+CMGF=1',          // Force Text Mode
                '+CSCA=',           // Service Center if FSMSCenter not empty
                '+CMGS=',           // Send Destination address
                '');                // Sending message text

  GSMSendMessageResponse : array[0..6] of string =
               (//'+CNMI: ',          // Response StartString expected
                'OK',
                '+CSCB: ',          // Response StartString expected
                'OK',               // No Response Expected, just OK
                'OK',               // No Response Expected, just OK
                'OK',               // No Response Expected, just OK
                #13#10,             // Response from phone to Send Message
                '+CMGS: ');         // Response StartString expected

  GSMListAllMessagesCommands : array[0..0] of string =
               ('+CMGL');           // List Messages on Phone
  GSMListAllMessagesResponse : array[0..0] of string =
               ('+CMGL: ');         // Response StartString expected

  GSMSendMessFromStorage : array[0..0] of string =
               ('+CMSS=');          // Send Message from Storage
  GSMSendMessFromStorageResp : array[0..0] of string =
               (#13#10'OK');        // Response StartString expected

  GSMWriteToMemoryCommands : array[0..1] of string =
               ('+CMGW=',           // Write Message to Memory
                '');                // Writing message text

⌨️ 快捷键说明

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