ARTICLE

Modbus คืออะไร: RTU, TCP, Register และการเชื่อมระบบ Industrial IoT.

อธิบาย Modbus แบบแยก Application Protocol ออกจาก Serial RTU และ Modbus TCP ตั้งแต่ register, function code, address, CRC/MBAP ไปจนถึง Modbus Security และแนวทางต่อเข้าระบบ monitoring

IoTAutomationโดย Thospaakเผยแพร่ 12 ก.พ. 2568อัปเดต 15 ต.ค. 2569
ภาพ Modbus RTU สำหรับระบบ IoT และ Automation แสดง Master/Controller, RS485 multi-drop bus, field devices และโครงสร้าง frame เพื่อแยก protocol ออกจาก physical bus

Modbus เป็นโปรโตคอลสื่อสารที่ใช้มายาวนานในระบบอุตสาหกรรม พลังงาน มิเตอร์ อินเวอร์เตอร์ PLC และอุปกรณ์ภาคสนาม จุดแข็งของมันไม่ใช่ความทันสมัย แต่คือความเรียบง่ายและ ecosystem ที่กว้าง

เวลาพูดว่า “อุปกรณ์รองรับ Modbus” ต้องถามต่อว่า Modbus แบบไหน, transport อะไร, register map revision ไหน และ data type/scale อย่างไร เพราะ Modbus RTU กับ Modbus TCP ใช้แนวคิด application protocol ร่วมกัน แต่ framing และ transport ไม่เหมือนกัน

ก่อนเอาค่าเข้า automation ให้สร้าง data contract ให้ครบ:

Register + function
→ data type / byte order
→ scale + unit
→ valid / sentinel / stale state
→ read permission / write permission
→ normalized application field

อ่านค่าได้ ยังไม่เท่ากับ เขียนควบคุมได้อย่างปลอดภัย และ register ที่ไม่มี quality/freshness boundary ไม่ควรถูกใช้เป็น control trigger โดยไม่ตรวจสถานะการสื่อสาร

แหล่งหลัก:

ชั้น Application กับ Transport ต้องแยกกัน

Modbus Application Protocol กำหนดแนวคิดอย่าง function code และ data model ส่วนวิธีขนส่ง message มีหลายแบบ

ที่พบมากคือ:

  • Modbus RTU over serial — มักใช้ RS-485
  • Modbus TCP/IP — ใช้ TCP/IP

จึงไม่ควรอธิบายว่า frame ของ Modbus ทุกแบบมี Address + Function + Data + CRC เหมือนกันทั้งหมด

Modbus RTU

RTU frame บน serial มี address ของ slave/server, PDU และ CRC สำหรับตรวจความผิดพลาดของ frame

Address | Function | Data | CRC

Modbus TCP

Modbus TCP ใช้ TCP และมี MBAP Header (Modbus Application Protocol header) แทน serial address/CRC framing แบบ RTU

MBAP Header | Function | Data

TCP มี checksum และ sequencing ของ transport ของตัวเอง จึงไม่มี RTU CRC ต่อท้าย Modbus TCP ADU แบบเดียวกับ serial RTU

Register คืออะไร

อุปกรณ์จำนวนมาก expose ข้อมูลผ่าน address/register map เช่น:

  • voltage
  • current
  • active power
  • energy
  • temperature
  • alarm status
  • setpoint

คำที่เห็นบ่อย:

Data model ขนาดเชิงตรรกะ การใช้งานทั่วไป
Coil 1 bit read/write boolean
Discrete Input 1 bit read-only boolean
Input Register 16 bit read-only value
Holding Register 16 bit read/write value

แต่ค่าจริงอาจกินหลาย register เช่น float32, uint32, int32 หรือ string และผู้ผลิตอาจกำหนด byte/word order ต่างกัน

ดังนั้น register 30001 จากอุปกรณ์หนึ่ง ไม่ได้มีความหมายเดียวกับ register 30001 ของอีกยี่ห้อ

Function Code ที่พบบ่อย

ตัวอย่างเช่น:

  • 01 Read Coils
  • 02 Read Discrete Inputs
  • 03 Read Holding Registers
  • 04 Read Input Registers
  • 05/06 Write Single Coil/Register
  • 15/16 Write Multiple Coils/Registers

การที่อุปกรณ์รองรับ Modbus ไม่ได้แปลว่าอนุญาต function code ทั้งหมด ต้องดู manual ของอุปกรณ์นั้น

ปัญหา Address Offset ที่เจอบ่อย

เอกสารบางชุดเขียน register แบบ human-readable เช่น 40001 แต่ library บางตัวใช้ zero-based offset จึงอาจต้องอ่าน address 0

อย่าเดา offset จากเลขหน้าเอกสาร ให้ตรวจว่า vendor map ระบุ:

  • logical register number
  • protocol address/offset
  • base 0 หรือ base 1
  • function code ที่ต้องใช้

ความผิดพลาดแบบ off-by-one เป็นสาเหตุคลาสสิกของการอ่านค่าผิด register

Scaling และ Signed Value

ค่าที่อ่านได้อาจเป็น integer ที่ต้องคูณ scale เช่น:

raw = 2305
scale = 0.1 V
value = 230.5 V

หรือค่า power อาจเป็น signed integer เพื่อบอกทิศทาง import/export

ก่อนใช้ข้อมูลจึงต้องยืนยัน:

  1. data type
  2. signed/unsigned
  3. scale factor
  4. endian/word order
  5. unit
  6. invalid/sentinel values

RS-485 ไม่ใช่ Modbus

RS-485 เป็นมาตรฐาน physical/electrical layer สำหรับ serial bus ส่วน Modbus RTU เป็น protocol ที่สามารถวิ่งบน RS-485 ได้

ดังนั้นคำว่า “มี RS-485” ไม่ได้แปลว่าอุปกรณ์รองรับ Modbus โดยอัตโนมัติ และอุปกรณ์ Modbus RTU ก็ยังต้องตั้ง baud rate, parity, stop bit และ slave/server address ให้ตรงกัน

ถ้าต้องการปู physical layer ก่อน อ่าน RS485 คืออะไร และต่างจาก UART อย่างไร แล้วค่อยกลับมาที่ register/function code ของ Modbus

Modbus TCP ไม่ได้แปลว่า Internet-ready

Modbus TCP ใช้ TCP/IP จึงต่อผ่าน Ethernet network ได้สะดวก แต่ไม่ได้หมายความว่าควรเปิด port ออก Internet ตรง ๆ

Modbus Organization มี Modbus Security Protocol ซึ่งใช้ TLS และ X.509 certificate และกำหนด port 802 สำหรับ secure profile

สำหรับระบบจริงควรแยกสองคำถาม:

  • protocol/device รองรับ Modbus อะไร
  • network architecture จะป้องกัน access อย่างไร

แนวทางมักรวม network segmentation, firewall/VPN, access control และ gateway ที่จำกัด write operation

Read กับ Write มีความเสี่ยงไม่เท่ากัน

การอ่าน telemetry เช่นกำลังไฟหรืออุณหภูมิเป็นคนละระดับกับการเขียน:

  • setpoint
  • operating mode
  • export limit
  • start/stop
  • protection parameter

ถ้า integration ไม่ได้ผ่าน validation ของผู้ผลิต อย่าทดลอง write register ในระบบ production เพียงเพราะรู้ address จากโพสต์ออนไลน์

register map ยังเปลี่ยนตาม model, firmware และ market variant ได้ด้วย

Modbus กับ MQTT ใช้ร่วมกันอย่างไร

สอง protocol นี้ไม่จำเป็นต้องแข่งกัน

Architecture ที่ใช้ได้ดีคือ:

Meters / Inverters / PLC
        │
   Modbus RTU/TCP
        │
        ▼
   Edge Gateway
        │
     MQTT/TLS
        │
        ▼
 Broker / Historian / Dashboard

Modbus ทำหน้าที่ field/device integration ส่วน MQTT เหมาะกับการกระจาย telemetry ไปหลาย consumer

การวาง gateway ตรงกลางยังช่วย normalize unit, timestamp, register map และ security policy ก่อนส่งข้อมูลขึ้นระบบส่วนกลาง

อ่านต่อ: MQTT คืออะไร และ IoT Gateway / Edge Controller คืออะไร หากต้องการแยก field protocol ออกจาก application layer ให้ชัด

Checklist ก่อนเชื่อมอุปกรณ์ Modbus

  • model และ firmware ตรงกับ register map หรือไม่
  • RTU หรือ TCP
  • RS-485 wiring/topology ถูกต้องหรือไม่
  • baud/parity/stop bits/address ถูกหรือไม่
  • TCP unit identifier ต้องใช้หรือไม่
  • register offset เป็น base 0 หรือ 1
  • data type/word order/scale/unit คืออะไร
  • polling rate ที่ vendor อนุญาตเท่าไร
  • read/write permission แยกกันหรือไม่
  • communication loss ต้อง fail อย่างไร

ทำไมโรงงานยังใช้ Modbus ในยุค IoT

เหตุผลหลักไม่ใช่เพราะ Modbus “ดีที่สุด” แต่เพราะมันมี installed base ใหญ่, implementation เรียบง่าย และอุปกรณ์ภาคสนามจำนวนมากยังรองรับอยู่

โรงงานจึงมักไม่รื้อ field layer ที่ทำงานดีเพียงเพื่อเปลี่ยน protocol แต่ใช้ gateway/edge layer เชื่อม Modbus เข้ากับ MQTT, historian, API หรือ cloud แทน

ข้อดีของแนวทางนี้คือ:

  • รักษา PLC/meter/inverter เดิมที่ยังใช้งานได้
  • แยก OT field communication ออกจาก IT/cloud transport
  • normalize register map และ unit ที่ gateway
  • เพิ่ม access control / segmentation ที่ชั้น network/edge
  • migrate ระบบทีละส่วนแทน big-bang replacement

แต่ legacy longevity ไม่ควรถูกใช้เป็นเหตุผลให้ expose Modbus TCP ออก Internet หรือเขียน setpoint โดยไม่มี control boundary ที่ชัด

สรุป

Modbus ยังมีประโยชน์มากใน Industrial IoT เพราะเรียบง่ายและอุปกรณ์รองรับจำนวนมาก แต่การใช้งานที่ถูกต้องต้องแยก Application Protocol, Serial/TCP transport และ vendor register map ออกจากกัน

อย่าจำ Modbus เป็น frame เดียวหรือ register ชุดเดียว และอย่าถือว่า Modbus TCP ไม่มีทางทำ security เพราะปัจจุบันมี Modbus Security profile แล้ว สิ่งที่สำคัญที่สุดในงาน integration คือเอกสาร revision ของอุปกรณ์จริงและ boundary ระหว่าง read-only monitoring กับ control/write operation