meta data for this page
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| automation:s7_modbus [2026/03/24 14:40] – [Modbus RTU and TCP, addressing] vamsan | automation:s7_modbus [2026/03/25 08:46] (current) – [Modbus/TCP] vamsan | ||
|---|---|---|---|
| Line 55: | Line 55: | ||
| **Modbus Server (formerly Slave)** | **Modbus Server (formerly Slave)** | ||
| - | The server is a passive device that responds to requests | + | A server is a passive device that responds to client |
| * **Action:** It waits for an incoming message, processes the request (for example, looks up a sensor value), and sends back a //" | * **Action:** It waits for an incoming message, processes the request (for example, looks up a sensor value), and sends back a //" | ||
| Line 63: | Line 63: | ||
| {{ : | {{ : | ||
| - | ==== Modbus RTU and TCP, addressing | + | ==== Modbus RTU and TCP, and Hybrid |
| - | The core difference is the transport layer: Modbus RTU is designed for a physical wire (Serial), while Modbus TCP is designed for a network (Ethernet). Before Ethernet' | + | The core difference is the transport layer: Modbus RTU is designed for a physical wire (Serial), while Modbus TCP is designed for a network (Ethernet). Before Ethernet' |
| + | |||
| + | Although quite different, they can sometimes be integrated, such as in multimaster | ||
| + | |||
| + | |< 100% >| | ||
| + | |{{ : | ||
| |< 100% >| | |< 100% >| | ||
| Line 77: | Line 82: | ||
| ^Addressing|Master ID: 0, slave ID: 1 to 247|IP Address (like: // | ^Addressing|Master ID: 0, slave ID: 1 to 247|IP Address (like: // | ||
| ^Wiring|3-core cable with shielding or 4-core cable|Ethernet cables (RJ45) and network switches| | ^Wiring|3-core cable with shielding or 4-core cable|Ethernet cables (RJ45) and network switches| | ||
| - | ==== Monomaster and Multimaster ==== | ||
| + | ==== Modbus/RTU ==== | ||
| + | {{ : | ||
| + | Modbus/RTU was the first Modbus communication method and remained the main standard until Ethernet became popular. It remains widely used today, partly because its hardware integration is simpler and more affordable than Modbus/TCP. As a result, it is likely to stay available for quite some time. | ||
| + | === Comparison of RS-232 and RS-485 === | ||
| + | The Modbus/RTU transmission options depend on the physical layer: [[com: | ||
| + | |< 100% >| | ||
| + | ^ ^[[com: | ||
| + | ^Operating mode|asynchronous transmission|synchronous transmission| | ||
| + | ^Number of drives and receivers\\ per line|1 drive\\ 1 receiver (point-to-point)|32 stations per segment| | ||
| + | ^Data transfer method|half-duplex, | ||
| + | ^Data transmission|p2p|multipoint| | ||
| + | ^Max. cable length|15 m|1200 m| | ||
| + | ^Max. data transfer\\ 12 m\\ 1200 m|\\ 20 kbps\\ (1 kbps)|\\ 35 Mbps\\ 100 kbps| | ||
| + | ^Receiver input\\ resistance|3..7 kΩ|≧ 12 kΩ| | ||
| + | ^Drive Load-\\ Impedance|3..7 kΩ|54 Ω| | ||
| + | ^Receiver "dead band" | ||
| + | ^Receiver voltage level|±15 V|-7..+12 V| | ||
| + | ^Drive output\\ voltage max.|±25 V|--9..+14 V| | ||
| + | ^Drive output\\ voltage min. (with load)|±5 V|±1.5 V| | ||
| + | ^Drive output\\ short circuit current limit|500 mA\\ to Vdc or Ground|150 mA to Ground\\ 250 mA to Vdc| | ||
| + | ^Receiver Hysteresis|1.15 V|50 mV| | ||
| + | === Modbus/RTU wiring === | ||
| + | For Modbus/RTU communication, | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | In a Modbus RTU network over RS-485, it is vital to terminate the bus to prevent signal reflections that can cause communication errors and CRC failures. | ||
| + | |||
| + | **Guidelines on Proper Terminator Placement** | ||
| + | |||
| + | * **Both Ends Only:** Install a termination resistor at the first and last device of the trunk cable. | ||
| + | * **Do Not Terminate Middle Devices:** Adding resistors at intermediate nodes can overload drivers and cause signal loss. | ||
| + | * **Daisy Chain Topology:** RS-485 should be wired in a continuous line. Avoid star or tree configurations with long branches or stubs, as these cannot be correctly terminated. | ||
| + | |||
| + | **Resistor Specifications** | ||
| + | |||
| + | * **Value:** Usually **120 Ω** to match the characteristic impedance of standard twisted-pair cable. | ||
| + | * **Power Rating:** Select a resistor with a power rating of at least 0.25W to 0.5W. | ||
| + | * **Connection: | ||
| + | |||
| + | === Modbus/RTU RS-485 Signaling === | ||
| + | {{ : | ||
| + | |||
| + | In a Modbus/RTU network (which uses the RS-485 physical layer), **A** and **B** represent the two wires of a differential pair used to transmit data. | ||
| + | |||
| + | * **Differential Signaling: | ||
| + | * **Half-Duplex: | ||
| + | |||
| + | **The Naming Confusion (A vs. B)** | ||
| + | |||
| + | Since there is no universal naming standard, wiring errors are common. Terminal labels vary by manufacturer as follows: | ||
| + | |||
| + | |< 100% >| | ||
| + | ^Labeling Convention^Non-Inverting Signal^Inverting Signal| | ||
| + | ^Common/ | ||
| + | ^TIA/ | ||
| + | ^Alternate Labels|D+, Data+, Tx+|D-, Data-, Tx-| | ||
| + | |||
| + | * **Standard Rule:** In the official TIA-485 standard, **A is negative (A-)** and **B is positive (B+)**. | ||
| + | * **Industry Practice:** Many Modbus device manufacturers (such as those following Modbus Organization guidelines) use A for negative (-) and B for positive (+). | ||
| + | |||
| + | **Voltage Levels** | ||
| + | |||
| + | * **Logic 1 (Idle/ | ||
| + | * **Logic 0 (Active/ | ||
| + | * **Idle State:** When no device is transmitting, | ||
| + | |||
| + | <WRAP center round tip 100%> | ||
| + | If your devices have the correct baud rate and address but still can't communicate, | ||
| + | </ | ||
| + | |||
| + | === Modbus/RTU Speed (Baud Rate) === | ||
| + | This is the transmission speed in bits per second (bps). | ||
| + | |||
| + | * **Standard speeds** are typically 9600 and 19200 bps. For quicker data updates over short distances, higher speeds such as 38400, 57600, or 115200 bps are often used. | ||
| + | * **Lower baud rates**, such as 9600, provide greater stability over lengthy cable runs—up to 1200 meters—because they are less affected by signal reflection and noise. | ||
| + | * **Timing Requirement: | ||
| + | * **Manufacturer specification: | ||
| + | |||
| + | === Modbus/RTU Data Format (" | ||
| + | **Data Format 8N1:** | ||
| + | |||
| + | This describes the structure of a single byte (character) sent over the wire. Totaling 10 bits per character: | ||
| + | |||
| + | * **8 (Data Bits):** Modbus RTU consistently employs 8 bits per byte, enabling direct transmission of binary data, whereas Modbus ASCII converts data to text. | ||
| + | * **N (Parity):** No parity bit is employed for error detection at the byte level. Note: While the Modbus standard technically specifies Even Parity as the default, the industry predominantly uses No Parity (8N1). | ||
| + | * **1 (Stop Bit):** A single stop bit indicates the conclusion of the byte. | ||
| + | * **Constraint: | ||
| + | |||
| + | === Modbus/RTU Error Checking (CRC) === | ||
| + | Even though //"No Parity"// | ||
| + | |||
| + | The **CRC** (//Cyclic Redundancy Check//) is an error-detection method that ensures data integrity in Modbus RTU. It is a 16-bit (2-byte) value appended to each message. | ||
| + | |||
| + | **Key Characteristics** | ||
| + | |||
| + | * **Algorithm: | ||
| + | * **Verification: | ||
| + | * **Efficiency: | ||
| + | |||
| + | ==== Modbus/TCP ==== | ||
| + | {{ : | ||
| + | Modbus/TCP (also known as Modbus TCP/IP) is a version of the Modbus protocol designed for Ethernet network communication. It wraps standard Modbus messages into TCP/IP packets, enabling reliable, high-speed data exchange. | ||
| + | |||
| + | **Key Characteristics** | ||
| + | |||
| + | * **Architecture: | ||
| + | * **Reliability: | ||
| + | * **Addressing: | ||
| + | |||
| + | |||
| + | ==== Modbus/TCP and Modbus/RTU Hybrid ==== | ||
| + | {{ : | ||
| ==== Modbus Registers and Coins ==== | ==== Modbus Registers and Coins ==== | ||
| + | Modbus data is structured into four main //" | ||
| - | ==== Modbus Register types ==== | + | **Coils** and **Discrete Inputs** are 1-bit variables. While a Coil can be read and written, a Discrete Input is only a read-only indicator of status. |
| + | |||
| + | Registers are 16-bit storage units: **Holding registers** are both writable and readable, and **Input registers** are read-only. | ||
| + | |||
| + | Although the Modbus standard allows Holding registers to be both read and written, many manufacturers restrict them to read-only. Always check the manufacturer’s specifications for accurate details. | ||
| + | |||
| + | **Key Characteristics** | ||
| + | |||
| + | * **16-Bit Architecture: | ||
| + | * **Big-Endian Format:** Modbus typically transmits the most significant byte (MSB) first. | ||
| + | * **Zero-Based Addressing: | ||
| + | * **Manufacturer Flexibility: | ||
| + | * **Read/ | ||
| + | |||
| + | === Modbus | ||
| + | To index Modbus address ranges, a 5-digit address (e.g., 40001) was initially used. Over time, this was insufficient, | ||
| + | |||
| + | Addressing within each range begins at 1, so the first holding register is 40001 or 400001. Users should note that many manufacturers use hexadecimal addresses, while Modbus/RTU uses decimal addresses. Larger data types (>16 bit, such as REAL, LREAL, DT, or STRING, WSTRING) span multiple registers for a single variable; thus, both the start address (offset, e.g., 400012) and the area length are specified. | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | ==== Modbus Telegram Structure, ADU/PDU ==== | ||
| + | In Modbus, the Application Data Unit (ADU) represents the complete message frame sent over a physical network. It functions as an //" | ||
| + | |||
| + | The ADU's structure changes based on whether you're using Modbus RTU (serial) or Modbus TCP (Ethernet): | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | |< 100% 50% 50% >| | ||
| + | |**Modbus/ | ||
| + | |**MBAP** Header (7 Bytes): \\ - Transaction ID (2 Bytes): Matches requests with responses. \\ - Protocol ID (2 Bytes): Always 0 for Modbus. \\ - Length (2 Bytes): Number of remaining bytes. \\ - Unit ID (1 Byte): Used for routing to serial devices through a gateway. \\ **PDU** (Function Code + Data): The core command. \\ \\ Max Size: 260 bytes.|Slave **Address** (1 Byte): Identifies the target device (1–247). \\ **PDU** (Function Code + Data): The core command. \\ **CRC** (2 Bytes): A Cyclic Redundancy Check used to detect transmission errors. \\ \\ Max Size: 256 bytes.| | ||
| + | |||
| + | Error checking by Modbus/TCP is managed by the TCP layer and is not included in the ADU. | ||
| + | |||
| + | === Modbus Protocol Data Unit (PDU) === | ||
| + | The //Modbus Protocol Data Unit// (**PDU**) is the core message structure common to all Modbus variants (RTU, ASCII, and TCP). It defines the actual command and data being exchanged between a client and a server, independent of the network medium. The PDU consists of two primary fields with a maximum combined size of 253 bytes. | ||
| + | |||
| + | ^Field^Size^Description| | ||
| + | ^Function Code|1 Byte|Tells the server which action to perform, such as Read, Write, Diagnostic.| | ||
| + | ^Data Field|0–252 Bytes|Contains request details such as register addresses, quantities, or the actual data values being transmitted or returned.| | ||
| + | |||
| + | **Function Code Types** | ||
| + | |||
| + | The function code ranges from 1 to 255 and is categorized by its purpose: | ||
| + | * **Public Codes (1–64, 73–99, 111–127): | ||
| + | * **User-Defined Codes (65–72, 100–110): | ||
| + | * **Exception Responses (128–255): | ||
| + | |||
| + | **Data Field Structure** | ||
| + | |||
| + | The structure of the data field changes depending on whether the PDU is part of a Request or a Response: | ||
| + | |||
| + | * In a **Request**, | ||
| + | * In a **Response**, | ||
| + | * **Addressing: | ||
| + | |||
| + | **Data Encoding (Endianness)** | ||
| + | |||
| + | Modbus uses Big-Endian representation for all 16-bit values within the PDU. This means the //Most Significant Byte// (**MSB**) is transmitted before the //Least Significant Byte// (**LSB**). // | ||
| + | |||
| + | === Modbus Function Codes === | ||
| + | The table below details the standard Modbus function codes, which cover data access, diagnostics, | ||
| + | |||
| + | |< 100% >| | ||
| + | ^Code (Hex)^Code (Dec)^Function Name^Data Type^Access| | ||
| + | ^0x01|01|Read Coils|Bit (0x)|Read| | ||
| + | ^0x02|02|Read Discrete Inputs|Bit (1x)|Read| | ||
| + | ^0x03|03|Read Holding Registers|16-bit (4x)|Read| | ||
| + | ^0x04|04|Read Input Registers|16-bit (3x)|Read| | ||
| + | ^0x05|05|Write Single Coil|Bit (0x)|Write| | ||
| + | ^0x06|06|Write Single Register|16-bit (4x)|Write| | ||
| + | ^0x07|07|Read Exception Status|Serial Only|Read| | ||
| + | ^0x08|08|Diagnostics|Internal|Read| | ||
| + | ^0x0B|11|Get Comm Event Counter|Serial Only|Read| | ||
| + | ^0x0C|12|Get Comm Event Log|Serial Only|Read| | ||
| + | ^0x0F|15|Write Multiple Coils|Bit (0x)|Write| | ||
| + | ^0x10|16|Write Multiple Registers|16-bit (4x)|Write| | ||
| + | ^0x11|17|Report Server ID|Serial Only|Read| | ||
| + | ^0x14|20|Read File Record|File|Read| | ||
| + | ^0x15|21|Write File Record|File|Write| | ||
| + | ^0x16|22|Mask Write Register|16-bit (4x)|Write| | ||
| + | ^0x17|23|Read/ | ||
| + | ^0x18|24|Read FIFO Queue|16-bit|Read| | ||
| + | ^0x2B|43|Read Device Identification|Internal|Read| | ||
| - | ==== Modbus Register-adressing ==== | + | **Function Code Categories** |
| - | ==== Modbus | + | * **Public Function Codes (1–64, 73–99, 111–127): |
| + | * **User-Defined Codes (65–72, 100–110): | ||
| + | * **Exception Codes (128–255): | ||
| ==== Modbus test programs, test methods ==== | ==== Modbus test programs, test methods ==== | ||