Bootstrak Skip to main content

6. MODBUS Interface – Transmission Modes and Function Codes

Streszczenie: Protokół MODBUS w NA-9473, część 1: tryby transmisji RTU i ASCII (budowa ramek), obsługiwane kody funkcji 1, 2, 3, 4, 5, 6, 8, 15, 16 i 23 z przykładami ramek zapytania i odpowiedzi, podfunkcje diagnostyczne (liczniki, reset, status FnBus) oraz odpowiedzi błędu i kody wyjątków.
EN: MODBUS interface, part 1: RTU/ASCII transmission modes, supported function codes with request/response examples, diagnostics sub-functions and exception codes.

Dotyczy: adapter sieciowy CREVIS NA-9473 (MODBUS RTU/ASCII, RS-485) systemu FnIO z modułami rozszerzeń serii ST
Źródło: dokumentacja producenta (CREVIS NA-9473 MODBUS Adapter (RS-485) User Manual); tabele przepisane z obrazów na tekst – opracowanie Multiprojekt

Rozdział 6 „MODBUS Interface” podzielono na 2 strony: Transmission Modes and Function Codes · Special Registers.

6.1 MODBUS Transmission Mode

Two different serial transmission modes are defined: The RTU mode and the ASCII mode. It defines the bit contents of message fields transmitted serially on the line. It determines how information is packed into the message fields and decoded.

6.1.1 RTU Transmission Mode

When devices communicate on a MODBUS serial line using the RTU (Remote Terminal Unit) mode, each 8–bit byte in a message contains two 4–bit hexadecimal characters. The main advantage of this mode is that its greater character density allows better data throughput than ASCII mode for the same baud rate. Each message must be transmitted in a continuous stream of characters.

RTU message frame

StartAddressFunctionDataCRC CheckEnd
≥ 3.5 chars1 char1 charUp to 252 chars2 chars≥ 3.5 chars

6.1.2 ASCII Transmission Mode

When devices are setup to communicate on a MODBUS serial line using ASCII (American Standard Code for Information Interchange) mode, each 8–bit byte in a message is sent as two ASCII characters. This mode is used when the physical communication link or the capabilities of the device does not allow the conformance with RTU mode requirement regarding timers management.

ASCII message frame

StartAddressFunctionDataCRC CheckEnd
1 char2 chars2 charsUp to 252 chars2 chars2 chars CR,LF

6.2 Supported MODBUS Function Codes

Supported MODBUS function codes

Function CodeFunctionDescriptionUnicast / Broadcast
1 (0x01)Read CoilsRead output bitUnicast
2 (0x02)Read Discrete InputsRead input bitUnicast
3 (0x03)Read Holding RegistersRead output wordUnicast
4 (0x04)Read Input RegistersRead input wordUnicast
5 (0x05)Write Single CoilWrite one bit outputUnicast / Broadcast
6 (0x06)Write Single RegisterWrite one word outputUnicast / Broadcast
8 (0x08)Diagnostics (Serial Line only)Read diagnostic registerUnicast
15 (0x0F)Write Multiple CoilsWrite a number of output bitsUnicast / Broadcast
16 (0x10)Write Multiple registersWrite a number of output wordsUnicast / Broadcast
23 (0x17)Read / Write Multiple registerRead a number of input words / write a number of output wordsUnicast

6.2.1 Read Coils (0x01)

This function code is used to read from 1 to 2000 contiguous status of coils in a remote device. The Request PDU specifies the starting address, i.e. the address of the first coil specified, and the number of coils. In the PDU Coils are addressed starting at zero. Therefore coils numbered 1-16 are addressed as 0-15. The coils in the response message are packed as one coil per bit of the data field. Status is indicated as 1= ON and 0= OFF.

Request

Read Coils (0x01) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x010x01
Starting Address Hi0x100x10
Starting Address Lo0x000x00
Quantity of Outputs Hi0x000x00
Quantity of Outputs Lo0x0A0x0A
Error Check (CRC/LRC)-0xB8, 0xAB
End of Frame-t1-t2-t3

Response

Read Coils (0x01) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x010x01
Byte Count0x020x02
Output Status0x550x55
Output Status0x020x02
Error Check (CRC/LRC)-0x8F, 0x6D
End of Frame-t1-t2-t3

* In case of address 0x1015~0x1000 output bit value: 00000010_01010101.

6.2.2 Read Discrete Inputs (0x02)

This function code is used to read from 1 to 2000 contiguous status of discrete inputs in a remote device. The Request PDU specifies the starting address, i.e. the address of the first input specified, and the number of inputs. In the PDU Discrete Inputs are addressed starting at zero. Therefore Discrete inputs numbered 1-16 are addressed as 0-15. The discrete inputs in the response message are packed as one input per bit of the data field. Status is indicated as 1= ON; 0= OFF.

Request

Read Discrete Inputs (0x02) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x020x02
Starting Address Hi0x000x00
Starting Address Lo0x000x00
Quantity of Inputs Hi0x000x00
Quantity of Inputs Lo0x0A0x0A
Error Check (CRC/LRC)-0xF8, 0x6B
End of Frame-t1-t2-t3

Response

Read Discrete Inputs (0x02) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x020x02
Byte Count0x020x02
Input Status0x800x80
Input Status0x000x00
Error Check (CRC/LRC)-0x50, 0x78
End of Frame-t1-t2-t3

* In case of address 0x0015~0x0000 output bit value: 00000000_10000000.

6.2.3 Read Holding Registers (0x03)

This function code is used to read the contents of a contiguous block of holding registers in a remote device. The Request PDU specifies the starting register address and the number of registers.

The register data in the response message are packed as two bytes per register, with the binary contents right justified within each byte. For each register, the first byte contains the high order bits and the second contains the low order bits.

Request

Read Holding Registers (0x03) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x030x03
Starting Address Hi0x080x08
Starting Address Lo0x000x00
Quantity of Register Hi0x000x00
Quantity of Register Lo0x020x02
Error Check (CRC/LRC)-0xC6, 0x0D
End of Frame-t1-t2-t3

Response

Read Holding Registers (0x03) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x030x03
Byte Count0x040x04
Output Register #0 Hi0x110x11
Output Register #0 Lo0x220x22
Output Register #1 Hi0x330x33
Output Register #1 Lo0x440x44
Error Check (CRC/LRC)-0x2D, 0xC6
End of Frame-t1-t2-t3

* In case of address 0x0800, 0x0801 output register value: 0x1122, 0x3344.

6.2.4 Read Input Registers (0x04)

This function code is used to read from 1 to approx. 125 contiguous input registers in a remote device. The Request PDU specifies the starting register address and the number of registers. The register data in the response message are packed as two bytes per register, with the binary contents right justified within each byte. For each register, the first byte contains the high order bits and the second contains the low order bits.

Request

Read Input Registers (0x04) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x040x04
Starting Address Hi0x000x00
Starting Address Lo0x000x00
Quantity of Register Hi0x000x00
Quantity of Register Lo0x020x02
Error Check (CRC/LRC)-0x71, 0xAD
End of Frame-t1-t2-t3

Response

Read Input Registers (0x04) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x040x04
Byte Count0x040x04
Input Register #0 Hi0x000x00
Input Register #0 Lo0x800x80
Input Register #1 Hi0x000x00
Input Register #1 Lo0x000x00
Error Check (CRC/LRC)-0x9C, 0x6C
End of Frame-t1-t2-t3

* In case of address 0x0000, 0x0001 input register value: 0x0080, 0x0000.

6.2.5 Write Single Coil (0x05)

This function code is used to write a single output to either ON or OFF in a remote device. The requested ON/OFF state is specified by a constant in the request data field. A value of FF 00 hex requests the output to be ON. A value of 00 00 requests it to be OFF. All other values are illegal and will not affect the output.

Request

Write Single Coil (0x05) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x050x05
Starting Address Hi0x100x10
Starting Address Lo0x010x01
Quantity of Outputs Hi0xFF0xFF
Quantity of Outputs Lo0x000x00
Error Check (CRC/LRC)-0xD9, 0x5C
End of Frame-t1-t2-t3

Response

Write Single Coil (0x05) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x050x05
Output Address Hi0x100x10
Output Address Lo0x010x01
Output Value Hi0xFF0xFF
Output Value Lo0x000x00
Error Check (CRC/LRC)-0xD9, 0x5C
End of Frame-t1-t2-t3

* Output bit of address 0x1001 turns ON.

6.2.6 Write Single Register (0x06)

Request

Write Single Register (0x06) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x060x06
Starting Address Hi0x080x08
Starting Address Lo0x000x00
Quantity of Outputs Hi0x110x11
Quantity of Outputs Lo0x220x22
Error Check (CRC/LRC)-0x07, 0x85
End of Frame-t1-t2-t3

Response

Write Single Register (0x06) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x060x06
Output Address Hi0x080x08
Output Address Lo0x000x00
Output Value Hi0x110x11
Output Value Lo0x220x22
Error Check (CRC/LRC)-0x07, 0x85
End of Frame-t1-t2-t3

* In case of address 0x0800 output register value: 0x0000 changes to 0x1122.

6.2.7 Diagnostics (0x08)

MODBUS function code 08 provides a series of tests for checking the communication system between a client (Master) device and a server (Slave), or for checking various internal error conditions within a server. The function uses a two–byte sub-function code field in the query to define the type of test to be performed. The server echoes both the function code and sub-function code in a normal response. Some of the diagnostics cause data to be returned from the remote device in the data field of a normal response.

Request

Diagnostics (0x08), sub-function 0x0000 – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x080x08
Sub-Function Hi0x000x00
Sub-Function Lo0x000x00
Data Hi0x110x11
Data Lo0x220x22
Error Check (CRC/LRC)-0x6C, 0x24
End of Frame-t1-t2-t3

Response

Diagnostics (0x08), sub-function 0x0000 – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x080x08
Sub-Function Hi0x000x00
Sub-Function Lo0x000x00
Data Hi0x110x11
Data Lo0x220x22
Error Check (CRC/LRC)-0x6C, 0x24
End of Frame-t1-t2-t3
  • Sub-function 0x0000(0) Return Query Data

The data passed in the request data field is to be returned (looped back) in the response. The entire response message should be identical to the request.

Sub-functionData Field (Request)Data Field (Response)Description
0x0000 (0)AnyEcho Request Data
  • Sub-function 0x0001(1) Restart Communications Option

The remote device could be initialized and restarted, and all of its communications event counters are cleared. Especially, data field 0x55AA makes the remote device to restart with factory default setup of EEPROM

Sub-functionData Field (Request)Data Field (Response)Description
0x0001 (1)0x0000, 0xFF00Echo Request DataReset
0x0001 (1)0x55AAEcho Request DataReset with Factory Default*
  • Sub-function 0x000A(10) Clear Counters and Diagnostic Register

The goal is to clear all counters and the diagnostic register. Counters are also cleared upon power–up.

Sub-functionData Field (Request)Data Field (Response)Description
0x000A (10)0x0000Echo Request Data
  • Sub-function 0x000B(11) Return Bus Message Count

The response data field returns the quantity of messages that the remote device has detected on the communications system since its last restart, clear counters operation, or power–up

Sub-functionData Field (Request)Data Field (Response)Description
0x000B (11)0x0000Total Message Count
  • Sub-function 0x000C(12) Return Bus Communication Error Count

The response data field returns the quantity of CRC errors encountered by the remote device since its last restart, clear counters operation, or power–up.

Sub-functionData Field (Request)Data Field (Response)Description
0x000C (12)0x0000CRC Error Count
  • Sub-function 0x000D(13) Return Bus Exception Error Count

The response data field returns the quantity of MODBUS exception responses returned by the remote device since its last restart, clear counters operation, or power–up.

Exception responses are described and listed in section 6.2.11.

Sub-functionData Field (Request)Data Field (Response)Description
0x000D (13)0x0000Exception Error Count
  • Sub-function 0x000E(14) Return Slave Message Count

The response data field returns the quantity of messages addressed to the remote device, or broadcast, that the remote device has processed since its last restart, clear counters operation, or power–up.

Sub-functionData Field (Request)Data Field (Response)Description
0x000E (14)0x0000Slave Message Count
  • Sub-function 0x000F(15) Return Slave No Response Count

The response data field returns the quantity of messages addressed to the remote device for which it has returned no response (neither a normal response nor an exception response), since its last restart, clear counters operation, or power–up.

Sub-functionData Field (Request)Data Field (Response)Description
0x000F (15)0x0000Slave No Response Count
  • Sub-function 0x0064(100) Return Slave MODBUS, FnBus Status

The response data field returns the status of MODBUS and FnBus addressed to the remote device. This status values are identical with status 1word of input process image. Refer to 5.3.1.

Sub-functionData Field (Request)Data Field (Response)Description
0x0064 (100)0x0000MODBUS, FnBus StatusSame as status 1 word
  • Sub-function 0x0065(101) Return Slave MODBUS, Error Count

The response data field returns the quantity of watchdog error addressed to the remote device since its last restart, clear counters operation, or power–up.

Sub-functionData Field (Request)Data Field (Response)Description
0x0065 (101)0x0000Watchdog Error Count
  • Sub-function 0x0066(102) Change Slave IO Output Status

The sub-function with data fields is to clear watchdog counter and change IO output status. This may be used to simulate clear output and fault output.

Sub-functionData Field (Request)Data Field (Response)Description
0x0066 (102)0x0000Echo Request DataReady output, automatically turns normal output
0x0066 (102)0x0001, 0x0002, 0x0003Echo Request DataClear output
0x0066 (102)0x0004Echo Request DataNormal output
0x0066 (102)0x0005, 0x0006, 0x0007Echo Request DataFault output

6.2.8 Write Multiple Coils (0x0F)

This function code is used to force each coil in a sequence of coils to either ON or OFF in a remote device. The Request PDU specifies the coil references to be forced. Coils are addressed starting at zero. A logical '1' in a bit position of the field requests the corresponding output to be ON. A logical '0' requests it to be OFF.

The normal response returns the function code, starting address, and quantity of coils forced.

Request

Write Multiple Coils (0x0F) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x0F0x0F
Starting Address Hi0x100x10
Starting Address Lo0x000x00
Quantity of Outputs Hi0x000x00
Quantity of Outputs Lo0x0A0x0A
Byte Count0x020x02
Output Value #00x550x55
Output Value #10x010x01
Error Check (CRC/LRC)-0x21, 0xC9
End of Frame-t1-t2-t3

Response

Write Multiple Coils (0x0F) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x0F0x0F
Starting Address Hi0x100x10
Starting Address Lo0x000x00
Quantity of Outputs Hi0x000x00
Quantity of Outputs Lo0x0A0x0A
Error Check (CRC/LRC)-0xD1, 0x6A
End of Frame-t1-t2-t3

* In case of address 0x1015~0x1000 output bit value: 00000000_00000000 changes to 00000001_01010101.

6.2.9 Write Multiple Registers (0x10)

This function code is used to write a block of contiguous registers (1 to approx. 120 registers) in a remote device. The requested written values are specified in the request data field. Data is packed as two bytes per register. The normal response returns the function code, starting address, and quantity of registers written.

Request

Write Multiple Registers (0x10) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x100x10
Starting Address Hi0x080x08
Starting Address Lo0x000x00
Quantity of Registers Hi0x000x00
Quantity of Registers Lo0x020x02
Byte Count0x040x04
Register Value #0 Hi0x110x11
Register Value #0 Lo0x220x22
Register Value #1 Hi0x330x33
Register Value #1 Lo0x440x44
Error Check (CRC/LRC)-0x3B, 0x12
End of Frame-t1-t2-t3

Response

Write Multiple Registers (0x10) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x100x10
Starting Address Hi0x080x08
Starting Address Lo0x000x00
Quantity of Outputs Hi0x000x00
Quantity of Outputs Lo0x020x02
Error Check (CRC/LRC)-0x43, 0xCE
End of Frame-t1-t2-t3

* In case of address 0x0800, 0x0801 output register value: 0x0000, 0x0000 changes to 0x1122, 0x3344.

Uwaga Multiprojekt: w oryginale w polu Function Code omyłkowo podano 0x0F; poprawiono na 0x10 – podane sumy CRC (0x3B, 0x12 i 0x43, 0xCE) zgadzają się tylko z kodem 0x10.

6.2.10 Read/Write Multiple Registers (0x17)

This function code performs a combination of one read operation and one write operation in a single MODBUS transaction. The write operation is performed before the read. The request specifies the starting address and number of holding registers to be read as well as the starting address, number of holding registers, and the data to be written. The byte count specifies the number of bytes to follow in the write data field. The normal response contains the data from the group of registers that were read. The byte count field specifies the quantity of bytes to follow in the read data field.

Request

Read/Write Multiple Registers (0x17) – request example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x170x17
Read Starting Address Hi0x080x08
Read Starting Address Lo0x000x00
Quantity of Read Hi0x000x00
Quantity of Read Lo0x020x02
Write Starting Address Hi0x080x08
Write Starting Address Lo0x000x00
Quantity of Write Hi0x000x00
Quantity of Write Lo0x020x02
Byte Count0x040x04
Write Reg. Value #0 Hi0x110x11
Write Reg. Value #0 Lo0x220x22
Write Reg. Value #1 Hi0x330x33
Write Reg. Value #1 Lo0x440x44
Error Check (CRC/LRC)-0x88, 0x3F
End of Frame-t1-t2-t3

Response

Read/Write Multiple Registers (0x17) – response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x170x17
Byte Count0x040x04
Write Reg. Value #0 Hi0x110x11
Write Reg. Value #0 Lo0x220x22
Write Reg. Value #1 Hi0x330x33
Write Reg. Value #1 Lo0x440x44
Error Check (CRC/LRC)-0x2E, 0xD2
End of Frame-t1-t2-t3

* In case of address 0x0800, 0x0801 output register value: 0x0000, 0x0000 changes to 0x1122, 0x3344.

6.2.11 Error Response

In an exception response, the server sets the MSB of the function code to 1. This makes the function code value in an exception response exactly 80 hexadecimal higher than the value would be for a normal response.

Exception Response Example

Exception response example

Field nameExampleRTU
Start of Frame-t1-t2-t3
Slave Address0x070x07
Function Code0x810x81
Exception Code0x020x02
Error Check (CRC/LRC)-0x22, 0xC0
End of Frame-t1-t2-t3

Exception Codes

MODBUS exception codes

Exception CodeNameDescription
01Illegal FunctionThe function code received in the query is not an allowable action for the server (or slave).
02Illegal Data AddressThe data address received in the query is not an allowable address for the server (or slave).
03Illegal Data ValueA value contained in the query data field is not an allowable value for server (or slave).
04Slave Device FailureAn unrecoverable error occurred while the server (or slave) was attempting to perform the requested action.
05AcknowledgeThe server (or slave) has accepted the request and is processing it, but a long duration of time will be required to do so.
06Slave Device BusySpecialized use in conjunction with programming commands. The server (or slave) is engaged in processing a long-duration program command. The client (or master) should retransmit the message later when the server (or slave) is free.
08Memory Parity ErrorThe server (or slave) attempted to read record file, but detected a parity error in the memory. The client (or master) can retry the request, but service may be required on the server (or slave) device.
0AGateway Path UnavailableSpecialized use in conjunction with gateways, indicates that the gateway was unable to allocate an internal communication path from the input port to the output port for processing the request.

* NA-9473 responds with exception codes 01, 02, 03, 04 and 06.

Słowa kluczowe

MODBUS RTU, MODBUS ASCII, ramka, kody funkcji, Read Coils, Read Discrete Inputs, Read Holding Registers, Read Input Registers, Write Single Coil, Write Multiple Registers, diagnostyka, kody wyjątków, CRC