Difference between revisions of "Modbus"
Line 178: | Line 178: | ||
The first line is a compact form for reading multiple data points. | The first line is a compact form for reading multiple data points. | ||
− | |||
=== Reading binary data === | === Reading binary data === |
Revision as of 16:40, 17 March 2014
The Modbus Protocol is a messaging structure. It is used to establish master-slave/client-server communication between intelligent devices. It is a de facto standard. The Modbus RTU specification defines the default communication protocol over serial lines. The Modbus TCP/IP specification takes the Modbus instruction set and wraps TCP/IP around it.
HSYCO supports both Modbus TCP and Modbus RTU, acting as a Modbus master in both cases.
This support is implemented as a set of Java APIs that allow read/write functions as described in the HSYCO Programming Handbook, and an I/O Server with a data point representation that you can use in the EVENTS language to poll Modbus data and generate events.
Contents
General Configuration
Modbus TCP
If a Modbus TCP/IP device is used to interface HSYCO to a Modbus RTU or ASCII bus over RS485, the gateway should be configured as Modbus TCP/IP slave and Modbus RTU/ASCII master.
HSYCO can also directly access Modbus TCP slave devices.
Modbus RTU
HSYCO supports Modbus RTU over RS485, using the FTDI’s USB-RS485 cable, a USB to RS485 levels serial UART converter cable incorporating FTDI’s FT232RQ USB to serial UART interface IC device which handles all the USB signaling and protocols. The cable provides a fast, simple way to connect devices with a RS485 interface to USB.
The USB-RS485 cable connects to any USB port of the HSYCO server, and doesn’t require external power supply. It offers an internal 120 Ohm resistor for line termination, and +5Vcc and ground level references that could become handy to connect pull-up and pull-down resistors for lines’ polarization (See the MODBUS over Serial Line Specification & Implementation guide for additional information about line polarization and termination).
The USB-RS485 cable doesn’t provide opto-isolated RS485 lines. Transient currents and voltage levels exceeding the cable’s specification could damage the USB-RS485 cable, HSYCO server and any Modbus device attached to the RS485 line.
HSYCO Configuration
Options
ID | Default | Values | Description |
---|---|---|---|
eventslog | false | true | if the general eventsLog option is also true in hsyco.ini, Modbus events for this gateway are written in the log files |
false | Modbus events for this gateway are not written in the log files, even if the eventsLog option is true in hsyco.ini | ||
extendednames | false | true | when this option is set, data point names used to return values will have the postfix .FF, where FF is the 2-digit decimal function code |
false | data points’ default naming convention is used |
Modbus Integration
Because the Modbus protocol is not asynchronous, but is based on polling, you always have to request a read to tell the driver to actually read the data and eventually generate an event on the associated data point if the data has changed.
Datapoints
The naming convention HSYCO uses for the events is closely related to the Modbus addressing standards and data types.
The data point names always begin with the same format: ID.unitid.address, where ID is the I/O server id, unitid is the Modbus slave device address, from 1 to 247, and address is the initial register address you are reading.
The supported public function codes are:
- 01: Read Coils
- 02: Read Discrete Inputs
- 03: Read Holding Registers
- 04: Read Input Registers
- 05: Write Single Coil
- 06: Write Single Register
- 15: Write Multiple Coils
- 16: Write Multiple Registers
Read operations with functions 03 and 04 support the following data types:
Type code | Description | Return range | Number of registers |
---|---|---|---|
ushort | unsigned short integer | 0 ... 65535 | 1 |
uint | unsigned integer | 0 ... (232 - 1) | 2 |
ulong | unsigned long integer | 0 ... (264 - 1) | 4 |
short signed | short integer | -32768 ... +32767 | 1 |
int | signed integer | -231 ... (231 - 1) | 2 |
long | signed long integer | -263 ... (263 - 1) | 4 |
float | signed IEEE 754 floating point | +/- 2-149 ... (2-2-23)x2127 | 2 |
Read Coils
Reads from 1 to 2000 contiguous coils, using function code 01. Each coil returns a binary value, 0 or 1, in distinct data points, one for each coil.
ID | Value | R/W | Description |
---|---|---|---|
unitid.address.di | bit:0 bit:false bit:off |
W | sets a single bit in the discrete inputs data table to off |
bit:1 bit:true bit:on |
W | sets a single bit in the discrete inputs data table to on | |
bits:<0|1|-...> '-' characters in the sequence are skipped |
W | sets multiple bits in the discrete inputs data table. The left-most bit in the sequence is the discrete input at <address> |
Coils Data Table
The internal coils data table can be read or written by a Modbus client, and written locally to set the coils’ values.
Examples
Reading an integer value from two registers
TIME: IO modbus.27.400 = readholdingregisters:uint IO modbus.27.400 : UISET mydata.text = IO modbus.27.400
The first line executes the read holding registers function on a Modbus device with address 27, reading two contiguous registers starting at address 400.
The second line sets a text object of the Web interface with the value read from the Modbus device.
Reading several values at once
TIME: IO modbus.27.220 Modbus.27.222 Modbus.27.224 = readinputregisters:float IO modbus.27.220 : UISET mydata1.text = IO modbus.27.220 IO modbus.27.224 : UISET mydata1.text = IO modbus.27.224
The first line is a compact form for reading multiple data points.
Reading binary data
TIME: IO modbus.27.1000 = readdiscreteinputs:24 IO modbus.27.1000.1 = 0 : UISET mylabel1.color = g IO modbus.27.1000.1 = 1 : UISET mylabel1.color = r ... IO modbus.27.220.24 = 0 : UISET mylabel24.color = g IO modbus.27.220.24 = 1 : UISET mylabel24.color = r
The first line executes the read discrete inputs function on a Modbus device with address 27, reading two contiguous registers starting at address 1000 and interpreting the first 24 bits as binary values.
The following lines change the color of some objects of the Web interface to green if the bit status is 0 or red if the status is 1.
Reading in extended mode
If the option extendednames was specified in System Settings:
TIME: IO modbus.27.1000 = readholdingregisters:ushort IO modbus.27.1000.03 : UISET mydata.text = IO modbus.27.1000.03
The first line executes the read holding register function for an ushort value on a Modbus device with address 27, reading the registers 1000.
The following line changes the attribute text of a text object setting it to the value just read.
Writing a single coil
TIME : IO modbus.27.1220 = bit:1
Set coil 1220 to on.
Writing multiple coils
TIME : IO modbus.27.1220 = bits:100
Set three coils starting from 1220 to 1, 0, 0.
Writing registers
TIME : IO modbus.27.210 = ushort:2048 TIME : IO modbus.27.220 = float:-123.22 TIME : IO modbus.27.230 modbus.27.240 = long:200000000
The first line executes the write single register on a Modbus device with address 27, writing the register at address 210 with an unsigned short number.
The second line executes the write multiple registers on a Modbus device with address 27, writing two consecutive registers starting at address 220 with an IEEE-754 floating-point number.
The third line executes the write multiple registers on a Modbus device with address 27, writing four consecutive registers starting at address 230 and four at 240 with a signed long number.
Release Notes
3.3.0
- added support for Write Multiple Coils function:
- writeMultipleCoils(String name, int unit, int address, int coils, byte[] bytes) Java method
- ID.unitid.address = bits:<0|1>... in EVENTS
3.2.1
- EVENTS: minor fixes to the readcoils and readdiscreteinputs actions
3.2.0
- reading multiple consecutive values with a single Modbus read is now supported for readholdingregisters and readinputregisters
3.1.1
- using format options bitshort, bitint and bitlong for readholdingregisters and readinputregisters, it is now possible to read 1, 2 or 4 registers and return the individual bit values
3.1.0
- new error events on Modbus read or write errors
- new option extendedNames. When this option is set, data point names used to return values will have the postfix .FF, where FF is the 2-digit decimal function code
3.0.3
- fixes to the Modbus write functions
- variables can now be used in value strings
3.0.0
- initial release
Modbus is a registered trademark of Modbus Organization, Inc.
ID | Value | R/W | Description |
---|