The greenhouse network is up and the register map is drawn. Ms. Wang's most basic need right now is: "What's the temperature in there? What's the humidity? Is the exhaust fan on?"—in other words, "read only, don't write". In Modbus, "reading" is handled by a dedicated group of function codes; they only ask the slave for data and never change anything.
Analogy: read function codes are like "calling on a student in class"—the teacher asks "student number 3, what's the temperature?" and the student reports a number; no matter how many times you ask, the state of the classroom is unchanged.
"Reading" is 90% of the work of a monitoring system: SCADA video walls, phone apps, and data dashboards are all essentially polling for data on a cycle. Master the 4 read function codes and you have half the ability to "map the physical world onto a screen". The other half—"writing" (controlling devices)—is saved for Chapter 5.
| Code | Hex | Action | What it reads | Corresponding data type |
|---|---|---|---|---|
| 01 | 0x01 | Read Coils | Switch states (read/write area) | Coils |
| 02 | 0x02 | Read Discrete Inputs | Read-only switch states | Discrete inputs |
| 03 | 0x03 | Read Holding Registers | Read/write 16-bit values | Holding registers |
| 04 | 0x04 | Read Input Registers | Read-only 16-bit measured values | Input registers |
Memory aid: 01/02 read "switches" (1 bit), 03/04 read "values" (16 bits); the ones ending in 1 read the writable area (coils / holding registers), the ones ending in 2 read the read-only area (discrete inputs / input registers). The four codes cover exactly the four data types from Chapter 3.
Read request: function code + starting address (2B) + quantity (2B).
Read response: function code echoed + byte count (1B) + data (N bytes). Here byte count = number of data bytes: when reading N registers (16-bit) the byte count = 2×N; when reading N coils the byte count = ⌈N/8⌉.
Request—read 2 holding registers from slave 1 (starting at 0x0000):
01
03
00 00 00 02
C4 0B
| Byte | Value | Meaning |
|---|---|---|
| 0 | 01 | Slave 1 |
| 1 | 03 | Function code: Read Holding Registers |
| 2–3 | 00 00 | Starting address |
| 4–5 | 00 02 | Quantity |
| 6–7 | C4 0B | CRC-16 |
Response—the slave returns 4 data bytes 00 64 00 C8 (100 and 200); note that the byte count = 04 (2 registers × 2 bytes):
01
03
04
00 64 00 C8
BA 7A
| Byte | Value | Meaning |
|---|---|---|
| 0 | 01 | Slave 1 |
| 1 | 03 | Function code echoed |
| 2 | 04 | Byte count = 4 |
| 3–6 | 00 64 00 C8 | Data (100, 200) |
| 7–8 | BA 7A | CRC-16 |
Request—read 1 input register from slave 1 (starting at 0x0000): 01 04 00 00 00 01 31 CA. The response structure is identical to FC03, except the byte count = 02 and the data is the 16-bit measured value being read (for example the raw reading of the humidity sensor).
Request—read 10 coils from slave 1 (starting at 0x0000): 01 01 00 00 00 0A BC 0D. The response has a byte count = 02 (⌈10/8⌉=2), and the data is packed by bit: bit 0 = coil 0, bit 1 = coil 1, and so on—one byte holds at most 8 switches.
| What Ms. Wang wants to know | Function code | Request (complete RTU frame) |
|---|---|---|
| Temperature of sensor 1 | FC 03 | 01 03 00 00 00 01 84 0A |
| Humidity | FC 04 | 01 04 00 00 00 01 31 CA |
| Whether the fan / pump / shade curtain is on | FC 01 | 01 01 00 00 00 0A BC 0D |
Note: FC01 reads coils (switch states in the writable area), while FC02 reads discrete inputs (read-only switches, such as a door sensor). In a greenhouse, "is the device on" usually maps to coils, so FC01 is used.
A factory host application uses FC 03 to read holding register 0x0000 of a VFD and obtain the present output frequency; comparing it with the target speed that was written in tells you whether the motor is "obeying". Read the setpoint, read the actual value—this is the pair of operations industrial monitoring performs most often.
The Lab wizard currently supports FC 01 / 03 (read operations); practise them one at a time:
01 03 00 00 00 02 C4 0B, and the response byte count should be 04;01 01 00 00 00 0A BC 0D, and the response byte count should be 02;In this chapter you mastered the 4 read function codes: 01/02 read switches, 03/04 read values; you learned that a read request contains only "address + quantity", while a read response adds a "byte count" followed by the data. You can also read every command in the greenhouse "look, don't control" operation table.
In the next chapter we turn the page—looking is not enough; Ms. Wang also wants to turn on the exhaust fan and set the humidity target. That means the write function codes.