|
|
 |
Serial Example
The following example sersimple.cpp shows how to configure a serial Modbus protocol and read values:
(**
* @internal
* @file sersimple.cpp
*
* @if NOTICE
*
* $Id: sersimple.dpr,v 1.4 2004/05/25 11:46:33 henrik Exp $
*
* Copyright (c) 2003-2004 FOCUS Software Engineering Pty Ltd, Australia.
* All rights reserved. <www.focus-sw.com>
*
* This file is for demonstration purposes only.
*
* No part of this material may be reproduced or transmitted in any
* form or by any means or used to make any derivative work without
* express written consent from the copyright holders.
*
* This material is provided "AS IS", WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. Any use is at your own risk.
*
* @endif
*)
program SerSimple;
{$APPTYPE CONSOLE}
uses
SysUtils, { sleep lives here in Delphi 7 }
Windows, { sleep lives here in Delphi 3 }
MbusMasterFunctions in '..\src\MbusMasterFunctions.pas',
MbusSerialMasterProtocol in '..\src\MbusSerialMasterProtocol.pas',
MbusAsciiMasterProtocol in '..\src\MbusAsciiMasterProtocol.pas',
MbusRtuMasterProtocol in '..\src\MbusRtuMasterProtocol.pas',
BusProtocolExceptions in '..\src\BusProtocolExceptions.pas';
(*****************************************************************************
* Gobal data
*****************************************************************************)
const
portName = 'COM1';
var
mbusProtocol: TMbusRtuMasterProtocol; { Use this declaration for RTU }
(*****************************************************************************
* Function implementation
*****************************************************************************)
(**
* Opens protocol
*)
procedure openProtocol;
begin
try
mbusProtocol := TMbusRtuMasterProtocol.Create(nil);
mbusProtocol.portName := portName;
mbusProtocol.baudRate := 9600;
mbusProtocol.dataBits := 8;
mbusProtocol.stopBits := 1;
mbusProtocol.parity := 0;
mbusProtocol.openProtocol;
except
on e: Exception do
begin
writeln('Error opening protocol: ', e.message, '!');
halt(1);
end;
end;
end;
(**
* Closes protocol
*)
procedure closeProtocol;
begin
mbusProtocol.closeProtocol;
mbusProtocol.Destroy;
end;
(**
* Cyclic loop which polls every one second 10 registers starting at
* reference 100 from slave # 1
*)
procedure runPollLoop;
var
i: integer;
dataArr: array[0..9] of word;
begin
while true do
begin
try
mbusProtocol.readMultipleRegisters(1, 100, dataArr);
for i := low(dataArr) to high(dataArr) do
writeln('[', 100 + i, ']: ', dataArr[i]);
writeln;
except
on e: EBusProtocolException do
writeln(e.message, '!');
on e: Exception do
begin
writeln('Fatal error: ', e.message, '!');
halt(1);
end;
end;
sleep(1000);
end;
end;
(**
* Main function.
*)
begin
openProtocol;
runPollLoop;
closeProtocol;
end.
MODBUS/TCP Example
The following example tcpsimple.cpp shows how to configure a MODBUS/TCP protocol and read values:
(**
* @internal
* @file tcpsimple.cpp
*
* @if NOTICE
*
* $Id: tcpsimple.dpr,v 1.4 2004/05/25 11:46:33 henrik Exp $
*
* Copyright (c) 2003-2004 FOCUS Software Engineering Pty Ltd, Australia.
* All rights reserved. <www.focus-sw.com>
*
* This file is for demonstration purposes only.
*
* No part of this material may be reproduced or transmitted in any
* form or by any means or used to make any derivative work without
* express written consent from the copyright holders.
*
* This material is provided "AS IS", WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. Any use is at your own risk.
*
* @endif
*)
program TcpSimple;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
MbusTcpMasterProtocol in '..\src\MbusTcpMasterProtocol.pas',
BusProtocolExceptions in '..\src\BusProtocolExceptions.pas',
MbusMasterFunctions in '..\src\MbusMasterFunctions.pas';
(*****************************************************************************
* Gobal data
*****************************************************************************)
const
hostName = '127.0.0.1';
var
mbusProtocol: TMbusTcpMasterProtocol;
(*****************************************************************************
* Function implementation
*****************************************************************************)
(**
* Opens protocol
*)
procedure openProtocol;
begin
try
mbusProtocol := TMbusTcpMasterProtocol.Create(nil);
mbusProtocol.hostName := hostName;
mbusProtocol.openProtocol;
except
on e: Exception do
begin
writeln('Error opening protocol: ', e.message, '!');
halt(1);
end;
end;
end;
(**
* Closes protocol
*)
procedure closeProtocol;
begin
mbusProtocol.closeProtocol;
mbusProtocol.Destroy;
end;
(**
* Cyclic loop which polls every one second 10 registers starting at
* reference 100 from slave # 1
*)
procedure runPollLoop;
var
i: integer;
dataArr: array[0..9] of word;
begin
while true do
begin
try
mbusProtocol.readMultipleRegisters(1, 100, dataArr);
for i := low(dataArr) to high(dataArr) do
writeln('[', 100 + i, ']: ', dataArr[i]);
writeln;
except
on e: EBusProtocolException do
writeln(e.message, '!');
on e: Exception do
begin
writeln('Fatal error: ', e.message, '!');
halt(1);
end;
end;
sleep(1000);
end;
end;
(**
* Main function.
*)
begin
openProtocol;
runPollLoop;
closeProtocol;
end.
|
 |