The following example sersimple.cpp shows how to configure a serial Modbus protocol and read values:
(**
* @internal
* @file sersimple_example.dpr
*
* @if NOTICE
*
* $Id: sersimple_example.dpr,v 1.1 2005/07/14 05:25:08 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_Example;
{$APPTYPE CONSOLE}
uses
SysUtils, { sleep lives here in Delphi 7 }
Windows, { sleep lives here in Delphi 3 }
MbusMasterFunctions in 'MbusMasterFunctions.pas',
MbusSerialMasterProtocol in 'MbusSerialMasterProtocol.pas',
MbusAsciiMasterProtocol in 'MbusAsciiMasterProtocol.pas',
MbusRtuMasterProtocol in 'MbusRtuMasterProtocol.pas',
BusProtocolExceptions in '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.
The following example tcpsimple.cpp shows how to configure a MODBUS/TCP protocol and read values:
(**
* @internal
* @file tcpsimple_example.dpr
*
* @if NOTICE
*
* $Id: tcpsimple_example.dpr,v 1.1 2005/07/14 05:25:08 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_Example;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
MbusTcpMasterProtocol in 'MbusTcpMasterProtocol.pas',
BusProtocolExceptions in 'BusProtocolExceptions.pas',
MbusMasterFunctions in '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.