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

📄 sd_pei_tool_main.pas

📁 Motorola 集群通信系统中SDTS车载台PEI端测试程序
💻 PAS
📖 第 1 页 / 共 4 页
字号:
//*******************************************************
//
//      Motorola Confidential Proprietary
//
//*******************************************************
//
// MODULE NAME : SD_PEI_tool_main
//
//
// MODULE DESCRIPTION :-
// This module contains the procedures required for Short Data
// demo PEI tool.
//
//
//*******************************************************
//
//       Copyright (c) 2004 MOTOROLA
//
// 2004 Jul 01 : Version 2.1 contains the following changes
// compared to version 2.0:
// Added support for returning received/consumed reports if requested by the sender.
// Fixed a bug that sometimes caused "access violation error" when receiving a report.
// Added flexibility such that it is not required to register an application before
//       allowing the send message function (send button is now active after successful connect
//       instead of successful registration).
// The register function can be used for more than one protocol identifier for added flexibility,
//       by keeping the register button active after the initial registration so it can be used for more protocol identifiers
// A de-register function has been added to support the de-registring of individual protocol identifiers.
// Information messages in the "Message window" have been enhanced to include the protocol identifier (PI) and
//       message reference number (mes. ref.) for the sent and received messages.
//*******************************************************


unit SD_PEI_tool_main;


interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Comms, Getdata, OnlyOne;

type
  TPEIState = (CONNECTING, CONNECTED, REGISTERAPP, REGISTERED, SEND, SENT, NOTUSED);
  TReportType = (NoReport, RecReport, ConReport, RecAndConReport);
  TForm1 = class(TForm)
    Panel1: TPanel;
    ComPortLabel: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ConnectButton: TButton;
    RegisterButton: TButton;
    SendButton: TButton;
    ComPortComboBox: TComboBox;
    ComPort: TComPort;
    CTS: TTimer;
    DataTimer: TTimer;
    sd: TSaveDialog;
    Panel6: TPanel;
    Panel2: TPanel;
    MessagesPad_Memo: TMemo;
    Panel3: TPanel;
    Label4: TLabel;
    Button1: TButton;
    Panel4: TPanel;
    CommandLogger_Memo: TMemo;
    Panel5: TPanel;
    Label5: TLabel;
    Button2: TButton;
    Splitter1: TSplitter;
    Label6: TLabel;
    Label7: TLabel;
    CheckBoxRec: TCheckBox;
    CheckBoxCon: TCheckBox;
    ProtocolIdentifierEdit: TGetLong;
    DestISSI_Edit: TGetLong;
    Message_Edit: THistComboBox;
    FormEnh1: TFormEnh;
    DelayTimer: TTimer;
    RepititionsEdit: TGetLong;
    DelayEdit: TGetLong;
    DisconnectButton: TButton;
    Deregister: TButton;
    procedure ConnectButtonClick(Sender: TObject);
    procedure DisconnectButtonClick(Sender: TObject);
    procedure CTSTimer(Sender: TObject);
    procedure DataTimerTimer(Sender: TObject);
    procedure RegisterButtonClick(Sender: TObject);
    procedure SendButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CheckBoxRecClick(Sender: TObject);
    procedure CheckBoxConClick(Sender: TObject);
    procedure DelayTimerTimer(Sender: TObject);
    procedure DeregisterClick(Sender: TObject);
  private
    { Private declarations }
    function  Send_PEI_Msg(msg_str:String):Bool;
    procedure Start_CTS_Timer(State: TPEIState);
    procedure Set_buttons_after_successful_connect();
    procedure Set_buttons_after_successful_registration();
    procedure Decode_CMT_message(var msg_str: string);
    procedure Decode_CMGS_response(var msg_str: string);
    procedure Decode_OK_response(var msg_str: string);
    procedure Decode_CME_ERROR_response(var msg_str: string);
    function  Get_Time : String;
    function  HexStringToByteString(StringToConvert: string): string;
    function  Conv_Msg_To_Hex(var msg_str:String):String;
    procedure SendData();
    procedure Delete_Str_Until_2nd_CR_LF(var msg_str: string);
  public
    { Public declarations }
  end;

const
   ATConnectCommand = 'AT'#13;
   AT_OK_Response = #13#10'OK'#13#10;
   ATRegisterCommandFirstPart = 'AT+CMGS=0,1,0,8'#13;
   ATDeRegisterCommandFirstPart = 'AT+CMGS=0,0,0,8'#13;
   ATRegisterCommandLastPart = #26;
   CommandSuccessfulResponse = #13#10'+CMGS: 0'#13#10#13#10'OK'#13#10;
   ATRegisterOK_Response = #13#10'+CMGS: 0'#13#10#13#10'OK'#13#10;
   ATSendOK_Response = ATRegisterOK_Response;
   ATReceivedMessage = #13#10'+CMT: ';
   ATCME_Error = #13#10'+CME ERROR: '#13#10;
   ATTerminateStr = #13#10;
   CTSMonitorTime = 100;
   MaxCTSMonitorDuration = 5000 / CTSMonitorTime; // 5000 msecs max. wait time

var
  Form1: TForm1;
  CTS_Timeout: Bool;
  CTSTimerCounter, RemainingNumOfRepitions: Integer;
  CTSPendingOperation: TPEIState;
  Buffer, Dumm_Buff, Unsent_PEI_msg: String;
  RefNum, DeliveryByte : Byte;

implementation

{$R *.DFM}


//*******************************************************
//
// PROCEDURE NAME : TForm1.ConnectButtonClick
//
//
// PROCEDURE DESCRIPTION :-
//   This procedure is called when the user clicks on the
//   Connect button. It will determine which COM port has
//   been selected by the user, and then try and use that COM
//   port to send an AT command to the radio to see if the
//   connection to the radio is working.
//*******************************************************

procedure TForm1.ConnectButtonClick(Sender: TObject);
var
   ComPort_Good, Sent_OK : Bool;
begin
   RefNum:= 0;

  // Initialise the DeliveryByte depending on the saved settings for user requested reports
   DeliveryByte:= 0;
   if CheckBoxRec.Checked then
   begin
     // Set the message received report requested bit in the SDS-TL-TRANSFER
      DeliveryByte := DeliveryByte or $04;
   end;
   if CheckBoxCon.Checked then
   begin
     // Set the message consumed report requested bit in the SDS-TL-TRANSFER
      DeliveryByte := DeliveryByte or $08;
   end;


   ComPort_Good := True;

// The user has pressed the Connect button
// Find out which COM port has been selected
   If ComPortComboBox.Text = 'COM1' then
   begin
      ComPort.Port := TPortType(COM1);
   end;
   If ComPortComboBox.Text = 'COM2' then
   begin
     ComPort.Port := TPortType(COM2);
   end;
   If ComPortComboBox.Text = 'COM3' then
   begin
     ComPort.Port := TPortType(COM3);
   end;
   If ComPortComboBox.Text = 'COM4' then
   begin
     ComPort.Port := TPortType(COM4);
   end;

  // Try to open the selected COM port for communication
   try
     ComPort.Open;
   except
    // Tell User : Not a Chance Buddy !!!!!
     MessageDlg('An Error Has Occurred whilst opening the Serial Port'#13#10+'Another application may be utilising that COM port !!!!!'+#39+'', mtError, [mbAbort], 0);
     ComPort_Good := False;
   end;

  // Valid COM port selected
   if ComPort_Good then
   begin
     // Set the DTR signal active before sending anything to the radio
      MessagesPad_Memo.Lines.Append(Get_Time() + ': Trying to connect to radio.');
      ComPort.SetDTR(True);
      ComPort.SetRTS(True);

     // Enable the Data timer that monitors whether data has been received
      DataTimer.Enabled := True;
      Sent_OK := Send_PEI_Msg(ATConnectCommand);
      CTSPendingOperation := CONNECTING;
      if  not(Sent_OK) then
      begin
         Unsent_PEI_msg := ATConnectCommand;
        // Start CTS timer waiting to check if the radio becomes ready to receive data
         Start_CTS_Timer(CONNECTING);
      end;
   end; // if ComPort_Good
end; // End of procedure TForm1.ConnectButtonClick


//*******************************************************
//
// PROCEDURE NAME : TForm1.DisconnectButtonClick
//
//
// PROCEDURE DESCRIPTION :
//   This procedure is called when the user clicks on the
// Disconnect button. The procedure will deassert the DTR line
// to disconnect this application from the radio.
//*******************************************************

procedure TForm1.DisconnectButtonClick(Sender: TObject);
begin
   ComPort.SetDTR(False);
   ComPort.SetRTS(False);
   RegisterButton.Enabled := False;
   DeRegister.Enabled := False;
   DisconnectButton.Enabled := False;
   ConnectButton.Enabled := True;
   SendButton.Enabled := False;
   CheckBoxRec.Enabled := False;
   CheckBoxCon.Enabled := False;
   ProtocolIdentifierEdit.Enabled := False;
   DestISSI_Edit.Enabled := False;
   Message_Edit.Enabled := False;
   DelayEdit.Enabled := False;
   RepititionsEdit.Enabled := False;
   ComPortComboBox.Enabled := True;

end; // End of procedure TForm1.DisconnectButtonClick


//*******************************************************
//
// FUNCTION NAME : TForm1.Send_PEI_Msg
//
//
// FUNCTION DESCRIPTION :-
//   This function is used to send a message string on the PEI
//
//*******************************************************

function TForm1.Send_PEI_Msg(msg_str: String): Bool;

begin
  // Check to see if radio is ready to receive data by checking the CTS signal from radio
   if (ComPort.HighCTS()) then
   begin
  // Radio is ready to receive data, so go ahead and send the AT command
      ComPort.WriteString(msg_str,True);
      CommandLogger_Memo.Lines.Append(Get_Time() + ' pc: ' + msg_str);
      Send_PEI_Msg := True;
   end
   else
   begin
     // PEI message has not been sent because radio is not ready to receive data
      Send_PEI_Msg := False;
   end;
end; // End of function TForm1.Send_PEI_Msg


//*******************************************************
//
// PROCEDURE NAME : TForm1.Start_CTS_Timer
//
//
// PROCEDURE DESCRIPTION :-
//   This procedure is used to start the "CTS" timer. The CTS timer is needed when
// it has not been possible to send a message on the PEI because
// the radio was not ready to receive data, so the CTS timer is started and when
// the CTS timer it is time to try and send original message again.
//*******************************************************

procedure TForm1.Start_CTS_Timer(State: TPEIState);
begin
    CTS.Interval := CTSMonitorTime;
    CTS.Enabled := True;
    CTSTimerCounter := 0;
    CTSPendingOperation := State;
end;

procedure TForm1.CTSTimer(Sender: TObject);
var
   Sent_OK: Bool;
begin
   CTSTimerCounter := CTSTimerCounter + 1;

   Sent_OK := Send_PEI_Msg(Unsent_PEI_msg);
   if (Sent_OK) then
   begin
     // Message has been sent so disable the timer
      CTS.Enabled := False;
   end;

  // Check to see if it is time to give up.
   if (CTSTimerCounter = MaxCTSMonitorDuration) then
   begin
     // We have waited 5 seconds and CTS is still not active so it is time to give up

⌨️ 快捷键说明

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