
Small confession before today's notes: I spent about twenty minutes this morning polling a device for the wrong data type and just getting an exception response back, before I actually sat down and read the register model properly. So this post is partly me making sure it sticks this time.
Modbus organizes everything a device exposes into four distinct data types, and they split along two axes: bit vs. word, and read-only vs. read/write.
Coils are single-bit, read/write. The classic example is a relay output or some other on/off actuator — the master can both read the current state and write a new state (turn it on, turn it off). Function codes here are things like 0x01 (read coils) and 0x05 (write single coil).
Discrete inputs are single-bit, read-only. Think a switch state, a door contact, a digital sensor that's just on or off — the master can read it but never write to it, because it's reporting something physically true about the world, not a setting. Read function code is 0x02.
Holding registers are 16-bit, read/write. This is the general-purpose bucket — configuration values, setpoints, calibration numbers, anything the master might need to both read and change. Function codes 0x03 (read) and 0x06 (write single) / 0x10 (write multiple) live here.
Input registers are 16-bit, read-only. This is where most sensor readings actually live — a temperature value, a pressure reading, whatever the device measured, expressed as a 16-bit number. Read-only because the master shouldn't be able to "write" a temperature into a sensor. Function code 0x04.
So the split is really: bit-sized data goes in coils/discrete inputs, word-sized data goes in holding/input registers; and read/write goes in coils/holding registers, read-only goes in discrete inputs/input registers.
Why this actually matters when writing a poll request: each of these four types lives in its own separate address space, and the function code you use determines which space you're asking about. A device's register at "address 30" in holding registers is a completely different piece of data from address 30 in input registers — they're not the same memory, they're two unrelated tables that happen to share a number. If you send function code 0x03 (read holding registers) but the device actually exposes that data as an input register, the device doesn't just hand you back the wrong number — it returns a Modbus exception response (illegal data address is the common one I've seen), because as far as it's concerned you asked for something that doesn't exist in that table. This is exactly the mistake I made this morning: the temperature value I wanted was in input registers, and I kept sending 0x03 out of habit.
Practically, this means you can't poll a new device without its register map — a datasheet or vendor doc listing exactly which type and address each value lives at. There's no way to "discover" it by guessing. For the SQC485Iv2 acting as master over a whole bus of different sensor types, that means every slave device on the bus needs its register map known ahead of time before the poll schedule can even be written. I want to ask Wei whether Siliqs keeps a standard internal format for these register maps across different vendor devices, or whether it's really just "read each datasheet every time."

今天筆記之前先小小懺悔一下:今天早上我花了大概二十分鐘,一直對一個裝置問錯資料類型,一直拿到 exception response,才真的坐下來好好把暫存器模型讀懂。所以這篇某種程度上也是在確保這次真的記住。
Modbus 把裝置對外暴露的所有資料分成四種明確的類型,可以沿著兩個軸來分:bit 還是 word,以及唯讀還是可讀寫。
Coils 是單一 bit、可讀寫。經典例子是繼電器輸出或其他開關型致動器——master 既可以讀目前狀態,也可以寫入新狀態(打開、關閉)。相關的 function code 有 0x01(read coils)和 0x05(write single coil)。
Discrete inputs 是單一 bit、唯讀。想成開關狀態、門的接點、只有開/關兩態的數位感測器——master 可以讀但永遠不能寫,因為它回報的是這個世界上一個物理上為真的狀態,不是一個設定值。讀取用的 function code 是 0x02。
Holding registers 是 16-bit、可讀寫。這是通用型的資料桶——設定值、setpoint、校正參數,任何 master 可能既要讀又要改的東西。Function code 0x03(read)和 0x06(write single)/0x10(write multiple)都是用在這裡。
Input registers 是 16-bit、唯讀。大部分感測器讀數其實都放在這裡——溫度值、壓力讀數,裝置量到的任何東西,以 16-bit 數字表示。唯讀是因為 master 不應該對感測器「寫入」一個溫度。Function code 是 0x04。
所以真正的分法是:bit 大小的資料放在 coils/discrete inputs,word 大小的資料放在 holding/input registers;可讀寫的放在 coils/holding registers,唯讀的放在 discrete inputs/input registers。
為什麼這對寫 poll request 真的很重要: 這四種類型各自活在自己獨立的位址空間裡,你用的 function code 決定了你問的是哪個空間。同一個裝置上「address 30」的 holding register,跟 address 30 的 input register,是完全不同的兩筆資料——它們不是同一塊記憶體,而是兩張互不相關、剛好共用了一個編號的表。如果你送出 function code 0x03(read holding registers)但裝置實際上是把那筆資料放在 input register,裝置不會單純回你錯的數字——它會回一個 Modbus exception response(我看過最常見的是 illegal data address),因為就它看來,你問的東西根本不在那張表裡。這正是我今天早上犯的錯:我要的溫度值其實在 input registers,但我一直習慣性送 0x03。
實務上這代表你沒有register map就沒辦法去 poll 一個新裝置——你需要一份 datasheet 或原廠文件,明確列出每個值放在哪個類型、哪個位址。沒辦法用猜的去「發現」它。對於要當整條 bus 的 master、bus 上又接了各種不同感測器類型的 SQC485Iv2 來說,這代表每個 slave 裝置的 register map 都得事先弄清楚,才有辦法寫出 poll schedule。我想問問 Wei,Siliqs 是不是有一套內部統一格式來管理這些不同廠商裝置的 register map,還是說其實就是「每次都重新讀一遍 datasheet」。