"Day 19/30: The dots finally connect — why the gateway caches instead of relaying live"

a Modbus data icon connected wirelessly to a LoRa mesh node

This is the post I've been building toward all week without quite realizing it. I finally sat down with Wei and had him walk through what the SQC485Iv2 actually does end to end, and it's the RS485/Modbus fundamentals from the last four days plus the mesh fundamentals from two weeks ago, snapped together.

Each SQC485Iv2 node is a Modbus master for its own local RS485 bus. This is exactly what I described on day 16 — the node sits at the head of a wired RS485 run with real sensors and meters hanging off it, each with its own slave ID, and the node polls them on a schedule: send request, wait for response, move to the next slave ID, repeat. Nothing new here, it's textbook Modbus master behavior, just running on Siliqs firmware.

Here's the part that took me a minute to get: the node doesn't relay those readings live over the mesh. My first assumption was something like "poll a sensor, immediately forward that reading over LoRa mesh to wherever it's needed." But that doesn't actually work, for two reasons I hadn't considered until Wei pointed them out. First, the mesh has real propagation delay — multi-hop routing, retries, contention — nothing like the near-instant turnaround a wired RS485 bus gives you. Second, the mesh already has its own periodic reporting schedule carrying traffic for potentially many nodes; you can't just also treat it as a live wire that answers on demand without messing with that existing schedule. So instead, each node reports its RS485 readings over the mesh on its own periodic cadence — a report, not a live relay.

A gateway node listens to the mesh and keeps a cache. It's subscribed to reports from every node in the mesh and keeps the latest known reading for every device, indexed by device/register. So at any moment the gateway has a reasonably fresh snapshot of the whole distributed sensor network's state sitting locally, even though none of that data arrived "live" in the request/response sense.

This is where it clicked for me: the gateway is itself a Modbus slave, from the perspective of whatever's downstream of it. A customer's SCADA system or PLC — something that already speaks Modbus RTU, the exact protocol I've spent all week learning — polls the gateway over its own RS485 connection, asking for a register the same way it would ask any normal Modbus device. The gateway answers instantly, because it's not going out to the mesh to fetch a live value at request time — it's just handing back whatever's already sitting in its cache from the last mesh report. From the SCADA system's point of view, it's talking to an ordinary, fast-responding Modbus slave. It has no idea the actual sensor data crossed a multi-hop wireless mesh to get there.

That's the "instant Modbus gateway" feature, and now that I've spent a week on RS485 fundamentals it actually makes sense why it's built this way rather than as a live pass-through — the mesh's timing characteristics make live pass-through impractical, so caching plus a locally-answered Modbus interface is the only design that actually fits both worlds' constraints. Genuinely satisfying to have this click after four days of what felt like fairly dry protocol details. Next up, I think I'm moving into embedded firmware topics — still need to confirm the exact schedule with Wei.

Modbus 資料圖示透過無線訊號連接到 LoRa mesh 節點

這篇是我這一整週在不知不覺中一直朝著它走的文章。我今天終於坐下來讓 Wei 從頭到尾講一次 SQC485Iv2 實際上在做什麼,結果發現就是把過去四天的 RS485/Modbus 基礎,跟兩週前學的 mesh 基礎,兜在一起而已。

每個 SQC485Iv2 節點,對它自己那條本地 RS485 bus 來說,就是一個 Modbus master。 這正是我 day 16 寫過的東西——節點接在一條有線 RS485 線路的最前端,線上掛著真正的感測器和電表,各自有自己的 slave ID,節點按照排程去 poll 它們:發 request、等 response、換下一個 slave ID、重複。這部分沒有新東西,就是教科書等級的 Modbus master 行為,只是跑在 Siliqs 的韌體上。

這裡有個地方我花了點時間才轉過來:節點不會把這些讀數即時轉發過 mesh。 我一開始的假設是類似「poll 到一個感測器讀數,就立刻透過 LoRa mesh 把它轉發到需要的地方」。但這其實行不通,有兩個我自己一開始沒想到、Wei 點出來之後才意識到的原因。第一,mesh 有實際的傳播延遲——多跳路由、重傳、爭用——完全不像有線 RS485 bus 那種近乎即時的往返速度。第二,mesh 本身已經有自己的週期性回報排程,可能同時承載著很多節點的流量;你不能把它也當成一條隨叫隨到的即時線,而不去打亂那個既有的排程。所以做法是:每個節點按照自己的週期節奏,把 RS485 讀數透過 mesh 回報出去——是回報(report),不是即時轉發(live relay)。

Gateway 節點會監聽整個 mesh,並維護一份快取。 它訂閱 mesh 上每個節點的回報,並且針對每個裝置/暫存器,保存最新已知的讀數。所以在任何一個時間點,gateway 本地都有一份整個分散式感測器網路狀態的、相當新鮮的快照,即使這些資料沒有一筆是以「即時 request/response」的方式抵達的。

這裡是我真正搞懂的地方:對下游來說,gateway 本身就是一個 Modbus slave。 客戶的 SCADA 系統或 PLC——已經在講 Modbus RTU 的東西,正好就是我這一週一直在學的協定——透過自己那條 RS485 連線去 poll gateway,問某個暫存器的方式,就跟問任何一台正常的 Modbus 裝置一模一樣。Gateway 會立刻回答,因為它不是在收到 request 的當下才跑去 mesh 上抓一個即時值——它只是把快取裡、上一次 mesh 回報留下來的資料直接回傳。從 SCADA 系統的角度看,它只是在跟一台普通、反應很快的 Modbus slave 講話,完全不知道實際的感測器資料是繞過一個多跳的無線 mesh 才到那裡的。

這就是「instant Modbus gateway」這個功能,而在我花了一週搞懂 RS485 基礎之後,終於理解為什麼它是被設計成這樣,而不是即時直通——mesh 的時間特性讓即時直通根本不實際,所以「快取加上一個本地就能回答的 Modbus 介面」才是唯一真正同時符合兩個世界限制的設計。花了四天感覺相當枯燥的協定細節之後,能有這種豁然開朗的感覺真的很爽。接下來我想應該要開始進到 embedded firmware 的主題了——確切排程還要再跟 Wei 確認一次。