Understanding Modbus
Understanding Modbus
Project:
Product
Synopsis:
Kongsberg Maritime AS
Kongsberg Maritime AS
Table of contents
1 ABOUT THIS DOCUMENT ..................................................................................5
1.1 Document history ..................................................................................................5
1.2 References .............................................................................................................5
/ / Page 2 of 29
Kongsberg Maritime AS
2 WHAT IS MODBUS?.............................................................................................. 6
9 WHAT IS A CRC?...................................................................................................8
/ / Page 3 of 29
Kongsberg Maritime AS
/ / Page 4 of 29
Kongsberg Maritime AS
A First Issue
1.2 References
No Doc No Description
/ / Page 5 of 29
Kongsberg Maritime AS
2 WHAT IS MODBUS?
Modbus is a communication protocol developed by Modicon systems. In simple terms,
it is a way of sending information between electronic devices. The device requesting the
information is called the Modbus Master and the devices supplying information are
Modbus Slaves. In a standard Modbus network, there is one Master and up to 247
Slaves, each with a unique Slave Address from 1 to 247. The Master can also write
information to the Slaves.
>
The data is sent as series of ones and zeroes called bits. Each bit is sent as a voltage.
Zeroes are sent as positive voltages and a ones as negative. The bits are sent very
quickly. A typical transmission speed is 9600 baud (bits per second).
5 WHAT IS HEXADECIMAL?
When troubleshooting problems, it can be helpful to see the actual raw data being
transmitted. Long strings of ones and zeroes are difficult to read, so the bits are
combined and shown in hexadecimal. Each block of 4 bits is represented by one of the
sixteen characters from 0 to F.
/ / Page 6 of 29
Kongsberg Maritime AS
Each block of 8 bits (called a byte) is represented by one of the 256 character pairs from
00 to FF.
Coil/Register Numbers can be thought of as location names since they do not appear in
the actual messages. The Data Addresses are used in the messages.
For example, the first Holding Register, number 40001, has the Data Address 0000.
The difference between these two values is the offset.
Each table has a different offset. 1, 10001, 30001 and 40001.
/ / Page 7 of 29
Kongsberg Maritime AS
9 WHAT IS A CRC?
CRC stands for Cyclic Redundancy check. It is two bytes added to the end of every
modbus message for error detection. Every byte in the message is used to calculate the
CRC. The receiving device also calculates the CRC and compares it to the CRC from
the sending device. If even one bit in the message is received incorrectly, the CRCs will
be different and an error will result.
/ / Page 8 of 29
Kongsberg Maritime AS
A discrete on/off value (this works the same as 16-bit integers with a value of 0 or
1.
The hex data would be 0000 or 0001)
Register 40108 could also be combined with 40109 to form any of these 32-bit data
types:
A 32-bit unsigned integer (a number between 0 and 4,294,967,295)
40108,40109 = AE41 5652 = 2,923,517,522
/ / Page 9 of 29
Kongsberg Maritime AS
More registers can be combined to form longer ASCII strings. Each register being used
to store two ASCII characters (two bytes).
Similarly, when registers are combined to represent 32-bit data types, Some devices
store the higher 16 bits (high word) in the first register and the remaining low word in
the second (AE41 before 5652) while others do the opposite (5652 before AE41)
It doesn't matter which order the bytes or words are sent in, as long as the receiving
device knows which way to expect it.
For example, if the number 29,235,175,522 was to be sent as a 32 bit unsigned integer,
it could be arranged any of these four ways.
/ / Page 10 of 29
Kongsberg Maritime AS
Some devices are built with a fixed map that is defined by the manufacturer. While
other devices allow the operator to configure or program a custom map to fit their
needs.
Many modbus software drivers (for Master PCs) were written with the 40001 to 49999
limits and cannot access extended registers in slave devices. And many slave devices do
not support maps using the extended registers. But on the other hand, some slave
devices do support these registers and some Master software can access it, especially if
custom software is written.
To get beyond this limit, a modification can be made to the protocol to use two bytes for
the address. The master and the slaves would all be required to support this
modification. Two byte addressing extends the limit on the number of slaves in a
network to 65535.
/ / Page 11 of 29
Kongsberg Maritime AS
By default, the Simply Modbus software uses 1 byte addressing. When an address
greater than 255 is entered, the software automatically switches to 2 byte addressing and
stays in this mode for all addresses until the 2 byte addressing is manually turned off.
18 FUNCTION CODES
Response
11 01 05 CD6BB20E1B 45E6
/ / Page 12 of 29
Kongsberg Maritime AS
The more significant bits contain the higher coil variables. This shows that coil 36 is off
(0) and 43 is on (1). Due to the number of coils requested, the last data field1B contains
the status of only 5 coils. The three most significant bits in this data field
This command is requesting the ON/OFF status of discrete inputs # 10197 to 10218
from the slave device with address 17.
Response
11 02 03 ACDB35 2018
The more significant bits contain the higher Discrete inputs. This shows that input
10197 is off (0) and 10204 is on (1). Due to the number of inputs requested, the last
data field35 contains the status of only 6 inputs. The two most significant bits in this
data field are filled in with zeroes.
/ / Page 13 of 29
Kongsberg Maritime AS
This command is requesting the content of analog output holding registers # 40108 to
40110 from the slave device with address 17.
Response
/ / Page 14 of 29
Kongsberg Maritime AS
Response
11 04 02 000A F8F4
/ / Page 15 of 29
Kongsberg Maritime AS
Response
The normal response is an echo of the query, returned after the coil has been written.
This command is writing the contents of analog output holding register # 40002
to the slave device with address 17.
Response
The normal response is an echo of the query, returned after the register contents have
been written.
/ / Page 16 of 29
Kongsberg Maritime AS
This command is writing the contents of a series of 10 discrete coils from #20 to #29
to the slave device with address 17.
The more significant bits contain the higher coil variables. This shows that coil 20 is on
(1) and 21 is off (0). Due to the number of coils requested, the last data field01
contains the status of only 2 coils. The unused bits in the last data byte are filled in with
zeroes.
Response
/ / Page 17 of 29
Kongsberg Maritime AS
This command is writing the contents of two analog output holding registers # 40002 &
40003 to the slave device with address 17.
Response
/ / Page 18 of 29
Kongsberg Maritime AS
To review, we can see that each byte of data can be represented in different ways.
Message Delimiting
In Modbus RTU, bytes are sent consecutively with no space in between them with a 3-
1/2 character space between messages for a delimiter. This allows the software to know
when a new message is starting.
Any delay between bytes will cause Modbus RTU to interpret it as the start of a new
message. This keeps Modbus RTU from working properly with modems.
Modbus ASCII marks the start of each message with a colon character " : " (hex 3A).
The end of each message is terminated with the carriage return and line feed characters
(hex 0D and 0A). This allows the space between bytes to be variable making it suitable
for transmission through some modems.
Byte Size
In Modbus RTU each byte is sent as a string of 8 binary characters framed with a start
bit, and a stop bit, making each byte 10 bits.
In Modbus ASCII, the number of data bits is reduced from 8 to 7. A parity bit is added
before the stop bit which keeps the actual byte size at 10 bits.
In Modbus ASCII, each data byte is split into the two bytes representing the two ASCII
characters in the Hexadecimal value. For example,
/ / Page 19 of 29
Kongsberg Maritime AS
The range of data bytes in Modbus RTU can be any characters from 00 to FF.
The range of data bytes in Modbus ASCII represent only the 16 hexadecimal characters.
Therefore, every data byte in Modbus ASCII must be one of these 16:
As mentioned earlier, each Modbus RTU message is terminate with two error checking
bytes called a CRC or Cyclic Redundancy Check. Similarly, Modbus ASCII is
terminated with an error checking byte called an LRC or Longitudinal Redundancy
Check.
The sum of the resulting byte stream with the LRC will then be 0 since adding the
negative subtotal will make the final total zero.
/ / Page 20 of 29
Kongsberg Maritime AS
For example, this command shows the data bytes required to request the content of
analog output holding registers 40108 to 40110 from the slave device with address 17.
11 03 00 6B 00 03
total:
0 00 0000 0000
The LRC byte is then split into two ASCII bytes with the rest of the data.
/ / Page 21 of 29
Kongsberg Maritime AS
The two ASCII bytes for the LRC are then 37 and 45
Using the same example shown above to request registers 40108 to 40110 from slave
address 17.
11 03 00 6B 00 03
The complete ASCII request is made by first adding the message delimiting
characters. A colon is added to the start of the message, the LRC, carriage return and
line feed are added to the end:
: 1 1 0 3 0 0 6 B 0 0 0 3 7 E CR LF
Each character is now treated as an ASCII character and replaced with it's hex value to
give the final message.
...............
11 03 00 6B 00 03 76 87
/ / Page 22 of 29
Kongsberg Maritime AS
20 MODBUS TCP/IP
20.1 TCP/IP
TCP is Transmission Control Protocol and IP is Internet Protocol. These protocols are
used together and are the transport protocol for the internet. When modbus information
is sent using these protocols, the data is passed to TCP where additional information is
attached and given to IP. IP then places the data in a packet (or datagram) and transmits
it.
Starting with the Modbus RTU message and removing the SlaveID from the beginning
and the CRC from the end results in the PDU, Protocol Data Unit.
Here is an example of a Modbus RTU request for the content of analog output holding
registers # 40108 to 40110 from the slave device with address 17.
/ / Page 23 of 29
Kongsberg Maritime AS
03 006B 0003
Transaction Identifier: 2 bytes set by the Client to uniquely identify each request. These
bytes are echoed by the Server since its responses may not be recieved in the same order
as the requests.
Unit Identifier: 1 byte set by the Client and echoed by the Server for identification of a
remote slave connected on a serial line or on other buses.
Summary
in ModbusTCP is:
/ / Page 24 of 29
Kongsberg Maritime AS
/ / Page 25 of 29
Kongsberg Maritime AS
21 TROUBLESHOOTING - EXCEPTION
RESPONSES
Following a request, there are 4 possible outcomes from the slave.
1. The request is successfully processed by the slave and a valid response is sent.
3. The request is received by the slave with a parity, CRC or LRC error.
The slave ignores the request and sends no response.
4. The request is received without an error, but cannot be processed by the slave for
another reason. The slave replies with an exception response.
In a normal response, the slave echoes the function code. The first sign of an exception
response is that the function code is shown in the echo with its highest bit set. All
function codes have 0 for their most significant bit. Therefore, setting this bit to 1 is
the signal that the slave cannot process the request.
Request
/ / Page 26 of 29
Kongsberg Maritime AS
Response
0A 81 02 B053
Following the Function Code is the Exception Code. The exception code gives an
indication of the nature of the problem. The possible codes are shown in the table
below.
The exception code shown above 02 is an indication that coil #1186 is an illegal
address in the slave. This coil has not been defined in the slave's modbus map.
Exception
Name Meaning
Code
The function code received in the query is not an
allowable action for the slave. This may be because
the function code is only applicable to newer devices,
and was not implemented in the unit selected. It could
01 Illegal also indicate that the slave is in the wrong state to
(01 hex) Function process a request of this type, for example because it is
unconfigured and is being asked to return register
values. If a Poll Program Complete command was
issued, this code indicates that no program function
preceded it.
The data address received in the query is not an
allowable address for the slave. More specifically, the
02 Illegal Data combination of reference number and transfer length is
(02 hex) Address invalid. For a controller with 100 registers, a request
with offset 96 and length 4 would succeed, a request
with offset 96 and length 5 will generate exception 02.
03 Illegal Data A value contained in the query data field is not an
/ / Page 27 of 29
Kongsberg Maritime AS
(03 hex) Value allowable value for the slave. This indicates a fault in
the structure of remainder of a complex request, such
as that the implied length is incorrect. It specifically
does NOT mean that a data item submitted for storage
in a register has a value outside the expectation of the
application program, since the MODBUS protocol is
unaware of the significance of any particular value of
any particular register.
04 Slave Device An unrecoverable error occurred while the slave was
(04 hex) Failure attempting to perform the requested action.
Specialized use in conjunction with programming
commands.
The slave has accepted the request and is processing it,
05 but a long duration of time will be required to do so.
Acknowledge
(05 hex) This response is returned to prevent a timeout error
from occurring in the master. The master can next issue
a Poll Program Complete message to determine if
processing is completed.
Specialized use in conjunction with programming
commands.
06 Slave Device
The slave is engaged in processing a long-duration
(06 hex) Busy
program command. The master should retransmit the
message later when the slave is free..
The slave cannot perform the program function
received in the query. This code is returned for an
07 Negative
unsuccessful programming request using function code
(07 hex) Acknowledge
13 or 14 decimal. The master should request diagnostic
or error information from the slave.
Specialized use in conjunction with function codes 20
and 21 and reference type 6, to indicate that the
extended file area failed to pass a consistency check.
08 Memory Parity
The slave attempted to read extended memory or
(08 hex) Error
record file, but detected a parity error in memory. The
master can retry the request, but service may be
required on the slave device.
Specialized use in conjunction with gateways, indicates
that the gateway was unable to allocate an internal
10 Gateway Path
communication path from the input port to the output
(0A hex) Unavailable
port for processing the request. Usually means the
gateway is misconfigured or overloaded.
Gateway Target Specialized use in conjunction with gateways, indicates
11
Device Failed to that no response was obtained from the target device.
(0B hex)
Respond Usually means that the device is not present on the
/ / Page 28 of 29
Kongsberg Maritime AS
network.
Caution Remark
Note Note
Warning Warning
/ / Page 29 of 29