The following example sersimple.cpp shows how to configure a serial Modbus protocol and read values:
(** * @file sersimple.dpr * * A simple console based example using FieldTalk in Modbus RTU master mode * * @if NOTICE * * Copyright (c) proconX Pty Ltd. All rights reserved. * * The following source file constitutes example program code and is * intended merely to illustrate useful programming techniques. The user * is responsible for applying the code correctly. * * THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @endif *) program SerSimple_Example; {$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: TMbusAsciiMasterProtocol; { Use this declaration for ASCII } mbusProtocol: TMbusRtuMasterProtocol; { Use this declaration for RTU } (***************************************************************************** * Function implementation *****************************************************************************) (** * Opens protocol *) procedure openProtocol; begin try mbusProtocol := TMbusRtuMasterProtocol.Create(nil); mbusProtocol.portName := portName; mbusProtocol.baudRate := 19200; mbusProtocol.dataBits := 8; mbusProtocol.stopBits := 1; mbusProtocol.parity := 2; 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:
(** * @file tcpsimple.dpr * * A simple console based example using FieldTalk in Modbus/TCP master mode * * @if NOTICE * * Copyright (c) proconX Pty Ltd. All rights reserved. * * The following source file constitutes example program code and is * intended merely to illustrate useful programming techniques. The user * is responsible for applying the code correctly. * * THIS SOFTWARE IS PROVIDED BY PROCONX AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PROCONX OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @endif *) program TcpSimple_Example; {$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.