ModbusServer (Modbus Server)

The ModbusServer instruction configures a datalogger as a Modbus ClosedRTU Remote Telemetry Units or Modbus ClosedTCP/IP Transmission Control Protocol / Internet Protocol. server device.

NOTE: ModbusServer() was formerly ModbusSlave(). Campbell Scientific, in conjunction with the Modbus Organization, is now using "client-server" to describe Modbus communications. The Modbus client(s) initiates communications and makes requests of server device(s). Server devices process requests and return an appropriate response (or error message). See https://modbus.org/ for more information.

Syntax

ModbusServer ( ComPort, BaudRate, ModbusAddr, ModbusVariable, BooleanVariable, [ModbusOption] )

Remarks

This instruction sets up a Modbus server device. As a Modbus server, the datalogger will respond to data request from a Modbus client over serial and TCP/IP. Supported Modbus functions are 01, 02, 03, 04, 05, 15, and 16. See the Modbus Client() help for details on these functions.

NOTE: The datalogger uses 8 data bits and no parity for Modbus communication. This can be changed by preceding the Modbus instruction with a SerialOpen instruction that sets the desired data bit and parity values.

The instruction only needs to be executed once, and therefore may appear outside the main program scan. It is commonly inserted directly after the BeginProg statement. One or more instance of ModbusServer() may be used in a single program. Multiple instances are most commonly used to configure multiple interfaces.

To route Modbus over a PakBus Network, in the router’s program, include a DataGram() instruction for every Modbus Server in the network. All server PakBus addresses must match their ModbusServer() addresses. If the Modbus Client is querying using TCP Modbus, then the DataGram() destination ID is 504 and source ID is 503. If the client is using RTU Modbus, then the DataGram() destination ID is 502 and the source ID is 501. At each of the remote servers, the ModbusServer() instructions comport is -2 for TCP Modbus and 0 for RTU Modbus, which signals the Modbus Server that they are looking for Modbus via PakBus datagrams carrying the Modbus request.

Parameters

ComPort (Communications Port)

The COM port that will be used by the instruction. Right-click to display a list.

Alphanumeric Description
0 Modbus packets routed via PakBus (using Datagram protocol; application ID = 502)
ComRS232 RS232 port of the datalogger
ComME Datalogger CS I/O port; modem enabled
Com310 Datalogger CS I/O port; COM310 modem
Com320 Datalogger CS I/O port, COM320 modem
ComSDC7 Datalogger CS I/O port; SDC7
ComSDC8 Datalogger CS I/O port; SDC8
ComSDC10 Datalogger CS I/O port; SDC10
ComSDC11 Datalogger CS I/O port; SDC11
ComC1 Datalogger control terminals 1 & 2
ComC3 Datalogger control terminals 3 & 4
ComC5 Datalogger control terminals 5 & 6
ComC7 Datalogger control terminals 7 & 8
502 - 65535 Modbus TCP/IP, for data requests via Modbus TCP/IP protocol

When communicating over TCP/IP, use the variable returned by the TCPOpen function for the ComPort.

Option 0, Modbus/PakBus, configures the datalogger to respond to Modbus RTU delivered via PakBus Datagrams with a destination Application ID of 502. A specific COM port is not specified as the Modbus request can be delivered over any active PakBus interface. This option is most commonly used in conjunction with an NL100 or with another logger configured with the DataGram() instruction. Additionally, this option is used when a user needs to route Modbus communications across a complex network or wants to use a single communications link for both Modbus and BMP5 communications via PakBus.

Setting the ComPort parameter to 502 or greater will specify the Modbus TCP/IP service port. The datalogger listens for Modbus TCP/IP communication on one IP port at a time; i.e., the logger will not listen on ports 502 and 503 at the same time. Avoid using port 6785 as that is the default PakBus TCP service port number.

A variable can be used in the ComPort parameter for use with functions that return a communication port.(for example, TCPOpen). If the ComPort parameter is set to 502, the datalogger will listen for a TCP connection on port number 502 and service TCP Modbus commands over TCP/IP instead of one one of the physical ComPorts.

Type: Constant

BaudRate (Data Transmit Rate)

The rate, in bps, at which data is transmitted. The options are 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, and 115200. Selecting one of these options fixes the baud rate at that rate of communication.If a negative baud rate is entered, the first communication attempt will be at the specified baud rate, but if communication fails at that rate, the datalogger will go into autobaud mode (where it will try different rates until successful or until the instruction times out).

NOTE: 300 baud is not applicable for the RS232 port. Autobaud is not available on control ports used as com ports. Baud rate for SDC ports must be 9600 or greater. If a serial port is opened, it must be closed before changing the port baud rate.

NOTE: If you are using SerialOpen to control a SDM-SIOx (SDM-SI01A, SDM-SIO1A, SDM-SIO2R) automatic baud rate detection is not supported. Rather, setting the baud rate to a negative value enables automatic flow control (RTS/CTS). Click here for additional information.

Right-click this parameter to display a list.

Type: Constant. In SerialOpen, BaudRate can be a variable.

This setting does not apply to Modbus TCP/IP or Modbus/Pakbus (ComPort = 0). A negative baud rate configures the port for automatic baud rate recognition (auto-baud). If set to auto-baud, the first communication attempt will be at the specified baud rate and failed communications will cause the datalogger to try different baud rates until successful or until the instruction times out.

The datalogger uses 8 data bits and no parity for Modbus communication. This can be changed by preceding the Modbus instruction with a SerialOpen instruction that sets the desired data bit and parity values.

ModbusAddr (Modbus Address)

The Modbus address of the server. Valid range is 1-255 (excluding 189, which is the PakBus sync byte). Address 0 is a broadcast address. Multiple addresses can be assigned to a single interface by using multiple ModbusClient() instructions

The ModbusAddr can be used to specify an offset for the starting register. Enter Starting Register * 1000 + the address. (i.e., the most significant digits specify the offset and the last three digits specify the Modbus address). For instance, for a starting register of 1500 and an address of 3, enter 1500003.

NOTE: If ModbusAddr is set to a negative value, the datalogger will discard any bytes that are echoed back to it.

Type: Variable or Integer

Multiple addresses can be assigned to a single interface by using multiple ModbusServer() instructions

ModbusVariable (Modbus Variable)

Used to specify the variable array that is used as the source of data to send, or the variable array that is used as the destination for data received. This variable can be formatted for Coil, Floating Point, or Integer Modbus registers and can be formatted as either Floating Point or Integer. If Variable is declared as Long or Boolean, then this parameter is set as a Modbus integer; otherwise, floating point Modbus will be used. Longs are assumed to be signed integers (numbers will be in the range -32768..+32767). The datalogger does not differentiate between holding and input registers. The only difference is the address offset. The specified array will be used for requests of input (address offset of 30000) or holding (address offset of 40000) registers.

If option 1 is used for ModbusOption, Variable should be declared as a long. If option 05, 06, 15, or 16 is used for Function, this parameter can be a constant.

Floating point variables take two Modbus registers. The Modbus input registers are offset by 30000; Modbus holding registers are offset by 40000. Therefore, the first register corresponding to any array location X is holding register 40000+2X-1. For example, to retrieve array value number 3, you would ask for two registers starting with 40005.

If the ModbusServer is queried by the client using an odd number of registers, then the ModbusServer datalogger assumes the request is for a 16 bit integer.

Type: Variable

BooleanVariable (Boolean Variable)

A variable or variable array that is used to hold the result if the client sends one of the discrete on/off commands to the server (i.e., 01 Read Coil/Port Status, 02 Read Input Status, 05 Force Single Coil/Port, 15 Force Multiple Coils/Ports). This parameter must be dimensioned as a ClosedBoolean Data type used to represent conditions or hardware that have only two states (true or false) such as flags and control ports. or a compiler error is returned.

If a 0 is entered for this parameter, then the discrete commands are mapped to control ports 1 through 8 instead. This will cause a compile warning, "A set coil function could conflict with a control port already in use." This is a warning, not an error, and the program can run successfully.

Type: Variable or Constant

Optional Parameter

ModbusOption (Modbus Option)

An optional parameter that is used to specify the data type of the Modbus variables.

Code Description
0 or absent Default; 32-bit float or Long, the standard ordering of the 2 byte registers are reversed (CDAB).
1 If the Modbus variable array is defined as a Long, variables are treated as 16-bit signed integers.
2 If the Modbus variable array is defined as a 32-bit float or a Long, with no reversal of the byte order (ABCD).
3 If the Modbus variable array is defined as a Long, variables are treated as 16-bit unsigned integers.
10 Modbus ASCII, 4 bytes, CDAB.
11 Modbus ASCII, 2 bytes, signed.
12 Modbus ASCII, 4 bytes, ABCD.
13 Modbus ASCII, 2 bytes, unsigned.

Type: Variable

Notes:

See the MoveBytes example for a program that converts an inverse floating point number to a floating point number.