"Day 16/30: Modbus RTU — one conversation at a time"

a data frame diagram labeled Modbus RTU

Yesterday I got as far as "RS485 gets bits reliably onto a shared bus, but doesn't define who talks when." Today's topic is the thing that actually answers that: Modbus RTU.

Modbus is a request/response protocol, and it's strictly master/slave. There's exactly one master on the bus (in the SQC485Iv2's case, that's the node itself), and everything else on the bus is a slave. The master initiates every single exchange — a slave is never allowed to just start talking on its own. It only replies when the master sends it a message addressed specifically to it, and it stays silent otherwise. This seemed almost too simple when I first read it, but it's actually the whole trick that makes a shared two-wire bus work without collisions: if only one device is ever allowed to speak first, and everyone else waits to be asked, you never get two transmitters fighting over the same wire pair at once.

Every slave has a numeric address (slave ID), usually 1–247. When the master wants to talk to a specific device, it puts that device's ID at the front of the message. Every slave on the bus sees every message (it's a shared bus, after all — everyone's electrically connected to the same A/B pair), but only the slave whose ID matches actually processes it and replies; the rest just discard it. There's also a broadcast address (0) for messages meant for everyone, which slaves act on but don't reply to.

After the address comes a function code — a single byte that says what kind of operation this is. Things like "read holding registers" (function code 0x03) or "write single register" (0x06) are common ones I've seen in Wei's example traffic captures. The function code plus whatever data follows (register address, quantity, or value to write) forms the request. I'm covering the actual register types tomorrow, so I'm deliberately not going deep on that today.

The frame ends with a CRC — a 16-bit cyclic redundancy check computed over the whole message. The receiving device recomputes the CRC on what it received and compares it to what was sent; if they don't match, the frame gets silently dropped as corrupted. This matters a lot on RS485 runs that might be tens or hundreds of meters through a noisy factory floor — you want a cheap, fast way to catch a garbled byte before you act on a bogus register value.

Putting the picture together: master sends [slave ID][function code][data][CRC], the addressed slave sends back [slave ID][function code][response data][CRC], and then the bus goes quiet until the master initiates the next exchange. One conversation at a time, always initiated by the master — which is exactly the discipline you need to make multiple devices coexist peacefully on a wire pair that has no arbitration mechanism of its own.

This finally clicked for me why the SQC485Iv2 is described as a "Modbus master for the whole bus" — it's not optional or a config choice, it's structurally the only role that can initiate anything. Still on my list to check with Wei: what actually happens on the bus if two slaves somehow get assigned the same address by mistake — I assume it's a mess, but haven't seen it happen or asked what the failure looks like in practice.

資料訊框示意圖,標示 Modbus RTU

昨天講到「RS485 負責把位元可靠地送上共用 bus,但沒定義誰什麼時候可以講話」。今天要看的東西正好回答了這個問題:Modbus RTU。

Modbus 是一個 request/response 協定,而且嚴格是 master/slave 架構。 Bus 上永遠只有一個 master(在 SQC485Iv2 的情境裡,就是節點本身),其他所有東西都是 slave。每一次通訊都是 master 發起的——slave 絕對不能自己主動開口講話。它只有在收到 master 專門發給它的訊息時才會回應,其他時候就保持沉默。第一次讀到這點時覺得未免太簡單,但這其實就是整套機制的關鍵:如果永遠只有一個裝置可以先講話,其他人都在等被點名,那就永遠不會有兩個發送端同時搶同一對線。

每個 slave 都有一個數字位址(slave ID),通常是 1–247。 Master 要跟特定裝置講話時,會把那個裝置的 ID 放在訊息最前面。Bus 上每個 slave 都會「聽到」每一則訊息(畢竟是共用 bus,大家都接在同一對 A/B 線上),但只有 ID 對得上的那個 slave 才會真的處理並回應,其他的直接丟棄。另外還有一個廣播位址(0),是給所有裝置的訊息,slave 會執行但不會回應。

位址之後接的是 function code——一個位元組,說明這是什麼類型的操作。像「read holding registers」(function code 0x03) 或「write single register」(0x06) 是我在 Wei 給的範例封包截圖裡常看到的。Function code 加上後面的資料(暫存器位址、數量,或要寫入的值)組成了整個 request。實際的暫存器類型我明天才會講,今天先刻意不深入。

訊框最後是 CRC——對整則訊息算出來的 16-bit cyclic redundancy check。接收端會對收到的內容重新算一次 CRC,跟送過來的比對,對不上就直接把這個訊框當作損毀而默默丟棄。這在可能拉了幾十甚至幾百公尺、穿過吵雜工廠現場的 RS485 線路上很重要——你需要一個便宜又快的方法,在根據一個亂掉的暫存器值採取動作之前先攔下損毀的 byte。

把整個流程串起來:master 送出 [slave ID][function code][data][CRC],被點名的 slave 回送 [slave ID][function code][response data][CRC],然後 bus 就安靜下來,直到 master 發起下一次通訊。永遠一次一句話,永遠由 master 發起——這正是讓多個裝置能在一對本身沒有任何仲裁機制的線上和平共存所需要的規矩。

這下我終於搞懂為什麼 SQC485Iv2 會被說成是「整條 bus 的 Modbus master」——這不是可有可無的選項或設定值,而是架構上唯一能發起通訊的角色。還有一件事想找 Wei 確認:如果不小心讓兩個 slave 被設成同一個位址,bus 上實際會發生什麼事——我猜會亂掉,但沒實際看過,也還沒問過實務上失效的樣子長怎樣。