Delphi – using SAIA communication library

Continued from “SAIA PLC communication library with Delphi and VB IDE

Create a new Delphi application. Copy necessary DLLs of the communication library to the same directory.

Startup of the application


program SkidSim;
uses
Forms,
Connect in 'Connect.pas' {ConnectForm},
Main in 'Main.pas' {MainForm},

{$R *.res}

begin
Application.Initialize;
// Application.CreateForm(TConnectForm, ConnectForm);
Application.Title := 'Skid Hydroform DB';
Application.CreateForm(TMainForm, MainForm);

Application.Run;
end.

//Add the start function of the driver in the "Create" function
//of the "MainForm" unit and make connection with PLC.

procedure TMainForm.FormCreate(Sender: TObject);
ConnectForm.Start;
OpenConnection;
end;

 

Open connections with PLC and exchange data:

 program SkidSim;
uses
Forms,
Connect in 'Connect.pas' {ConnectForm},
Main in 'Main.pas' {MainForm},

{$R *.res}

begin
Application.Initialize;
// Application.CreateForm(TConnectForm, ConnectForm);
Application.Title := 'Skid Hydroform DB';
Application.CreateForm(TMainForm, MainForm);

Application.Run;
end.

program SkidSim;

uses
Forms,
Connect in 'Connect.pas' {ConnectForm},
Main in 'Main.pas' {MainForm},

{$R *.res}

begin
Application.Initialize;
// Application.CreateForm(TConnectForm, ConnectForm);
Application.Title := 'Skid Hydroform DB';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.

procedure TConnectForm.Start;
begin
  PcdMessage(PcdRdChanSetupFromIni('SkidSim.ini', 'GoOnline', @mOpenData));
end;

procedure TMainForm.Exit1Click(Sender: TObject);
begin
  if ConnectForm.GetChannelConnected = true then
  begin
    ConnectForm.Disconnect(self.Handle,RetMessage);
  end;
  PcdComUnloadDrv(TRUE);
  PcdExitInterface;
  application.Terminate;
end;

procedure TMainForm.ConnectionSettingsClick(Sender: TObject);
begin
  ConnectForm.SetupConnection(self.handle,RetMessage);
  StatusBar1.Panels[0].Text:=RetMessage;
end;

procedure TConnectForm.SetupConnection(Handle:HWND; var Text:string);
begin
  if (GetChannelConnected) then
  begin
    MessageDlg('Disconnect before changing communication options!',mtWarning,[mbOk],0);
    if (MessageDlg('Disconnect?',mtConfirmation,[mbYes, mbNo],0)=mrYes) then
    begin
      Text:=ShowStatus(PcdDisconnectChannel(PcdConn , 0,Handle), 'Disconnect : ');
      if (respons = 0)  then
      begin
        Text := 'Disconnected';
      end;
    end;
  end
  else
  begin
    respons := PcdRdChanSetupFromIni('SkidSim.ini', 'GoOnline',@mOpenData);
    if(PcdConnectionDialog(handle, @mOpenData)= IDOK) then
    begin
      respons := PcdWrChanSetupToIni('SkidSim.ini', 'GoOnline',@mOpenData);
    end;
    Text:=ShowStatus(respons, 'Read - write setup to ini.');
  end;
end;

procedure TMainForm.OpenConnection;
begin
  if (ConnectForm.GetChannelConnected = false) then
  ConnectForm.Connection(self.handle,RetMessage);
  if (ConnectForm.GetChannelConnected = true) then
  begin
    //Exchange data ...
  end;
  StatusBar1.Panels[0].Text:=RetMessage;
end;

function TConnectForm.Connection(Handle:HWND; var Text:string): boolean;
var
  str1       : string;
  str2       : string;
  TempString : array[0..32] of Char;
  Buffer     : PChar;
  begin
    Result := false;
    if ((GetChannelConnected = false) {and (mOpenData.Protocol = 0)}) then
    begin
      Buffer := @TempString;
      respons := PcdConnectChannel(Addr(PcdConn),@mOpenData,0,Handle);
      Text:=ShowStatus(respons ,'Connection : ');
      if (respons = 0) then
      begin
        PcdGetPortName( @mOpenData,Buffer,32);
        str1 := string(Buffer);
        PcdGetBaudrateName( @mOpenData,Buffer,32);
        str2 := string(Buffer);
        Text:=Text+'   '+ str1+ ', '+ str2 +' bps';
        Result := mOpenData.bConnected
      end
      else Text := 'Disconnected';
    end
    else MessageDlg('Second connection!',mtWarning,[mbOk],0);
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
var
  Inp  : array[0..8] of Char;
  respons  : integer;
begin
  if ConnectForm.GetChannelConnected = true then
  begin
    //read 22 integer values from PLC, begin with address 100
    respons := PcdRdRTC(PcdConn, 'R', 100, 22, lpData);
    if (respons=0) then
    begin
    Elem0.Caption :=FloatToStr(lpData[0]/10.0);
    //water temperature in reaction tank (0.1 C)

    Elem1.Caption :=FloatToStr(lpData[4]/100);
    //pressure H2O2 in storage tank [0 ... 1000]
    Elem21.Caption :=FloatToStr(lpData[21]/10.0);
    //H2O2 temperature in storage tank (0.1 C)
  end;

  //read 8 flags from PLC, begin with address 620
  respons := PcdRdIOF(PcdConn, 'F', 620, 8, Inp);
  if (respons=0) then
  begin
    if(Inp[0] = '0') then Rtank.Color :=clRed
    else Rtank.Color :=clGreen;  //reactor
    if(Inp[1] = '0') then Valve.Color :=clRed
    else Valve.Color :=clGreen;  //valve
    if(Inp[7] = '0') then Pump.Color :=clRed
    else Pump.Color :=clGreen;  //pump
  end
  else
  //without connection
  begin
    Elem0.Caption := "**";
    Elem1.Caption  := "**";
    //...
    Elem21.Caption := "**";

    Rtank.Color :=clGray;
    Valve.Color  := clGray;
    //...
    Pump.Color :=clGray;
    end;
  end;
end;

 

  Delphi_source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: