Modbus RTU Master — อ่านค่าจากเซนเซอร์ Slave
PLC เป็น Modbus RTU Master อ่านค่าจาก slave ผ่าน COM1 — อ่าน 4 holding register + 1 coil พร้อม flag สถานะ (healthy / error / timeout) และนับจำนวนครั้งที่ error
หลักการ
ใช้ คำสั่ง MBUS ของ Samkoon PLC เป็น Modbus RTU Master โพลล์ slave devices ที่ต่ออยู่บน RS485 bus (เช่น energy meter, temperature sensor, inverter)
ตัวอย่างนี้แสดง polling pattern ที่:
- อ่านค่าเซนเซอร์อัตโนมัติ — จังหวะการ poll ตั้งใน Modbus Table (Loop mode)
- ตรวจจับ error → set flag M90 สำหรับแจ้ง HMI
- นับจำนวนครั้งที่ error สะสมไว้ที่ D710 (resettable จาก HMI)
Modbus Table Setup
ก่อน compile/download โปรแกรมนี้ ต้องสร้าง Modbus Table ใน SamSoar2022 IDE ก่อน:
- คลิกขวาที่ Modbus Table → Add → Add Modbus Table
- ตั้งชื่อ table =
sensor - เพิ่มรายการดังนี้:
| # | Station | Function Code | Slave Register | Count | Master Register | Trigger Mode | Purpose |
|---|---|---|---|---|---|---|---|
| 1 | K1 | 03 (Read Holding) | 40001 | 4 | D100 | Loop | อ่าน D0–D3 จาก slave ไป D100–D103 |
| 2 | K1 | 01 (Read Coils) | 00001 | 1 | M100 | Loop | อ่าน coil 1 ของ slave ไป M100 |
- Save + Compile
Trigger Mode ตั้งใน Table เอง — ไม่ต้องเขียน timer ใน ladder เพื่อ poll Loop mode จะโพลล์ต่อเนื่องในขณะที่ MBUS ถูกเรียก. Trigger mode จะโพลล์เฉพาะตอนที่ bit ที่ระบุไว้ใน table ติดเท่านั้น — เหมาะสำหรับ write-on-change patterns
Communication Flow
M8151 (Always ON) → MBUS K1 sensor D700 D701
↓
Modbus Table "sensor" (Loop mode) runs continuously:
PLC (Master) → Slave: Read Holding D0-D3
Slave → PLC: D100-D103 ← D0-D3
↓
PLC (Master) → Slave: Read Coil 1
Slave → PLC: M100 ← Coil 1
↓
D700 = Error code (0 = success, 16 = timeout, 24 = frame error)
D701 = Current command index (0, 1, 0, 1, ...)
Error Handling
- Error 0 (0x00) — Communication ปกติ → M91 (healthy flag) ติด
- Error 16 (0x10) — Timeout — slave ไม่ตอบ → M92 (timeout) ติด + M90 (error) ติด
- Error 24 (0x18) — Frame error — ตรวจ address หรือ function code ผิด → M90 ติด
HMI สามารถ monitor M90 / M91 / M92 / D700 / D710 เพื่อแสดงสถานะการเชื่อมต่อ
Configuration
ต้องตั้งค่า Communication ใน Project Settings ของ SamSoar2022:
- COM1 — Modbus Master, 9600 bps, 8-N-1 (ตรงกับ slave)
- Station Number — 0 (master)
- Frame Interval — 30 ms (กัน frame ซ้อนกัน)
- Re-Transmission — 3 ครั้ง (retry ถ้า slave ไม่ตอบ)
โปรแกรม PLC
I/O Assignment
| Address | Role | Note |
|---|---|---|
| COM1 | Port RS485 Master | ตั้งเป็น Modbus Master ใน Project Settings → Communication |
| D100 – D103 | ค่า sensor ที่อ่านมา (จาก slave D0–D3) | อ่านทุก 500 ms |
| M100 | สถานะ coil ที่อ่านมา (จาก slave coil 1) | |
| D700 | รหัส Error ของ Modbus | 0=OK, 16=timeout, 24=frame error |
| D701 | Index คำสั่งปัจจุบัน | index ใน Modbus table ที่กำลัง poll |
| M90 | Error flag (มี error) | ใช้แจ้ง alert บน HMI |
| M91 | Healthy flag (ไม่มี error) | ใช้โชว์ indicator สีเขียวบน HMI |
| M92 | Flag Timeout | ติดเฉพาะเวลา slave ไม่ตอบ (Error 16) |
| D710 | เคาน์เตอร์ error สะสม | เพิ่ม 1 ทุกครั้งที่ error — รีเซ็ตจาก HMI ได้ |
Parameters ที่ปรับได้
Modbus Table 'sensor'default: ตั้งใน IDEStation 1, FC 03 (Read Holding) + FC 01 (Read Coils), Trigger Mode = Loop — แก้ได้ตามอุปกรณ์จริง: polling cadence, station ID, function code, register offset ปรับได้ใน table configuration
Frame Intervaldefault: 30 msตั้งใน Project Settings → Communication → COM1 — gap ระหว่าง Modbus frame เพื่อกัน traffic ซ้อนกัน
Re-Transmissiondefault: 3จำนวนครั้งที่ retry ถ้า slave ไม่ตอบ ก่อนตั้ง error flag
อุปกรณ์ที่แนะนำ
คำถามที่พบบ่อย
ทำไม MBUS ใช้ LD M8151 ไม่ต้อง timer?
Modbus Table ที่สร้างใน IDE มี **Trigger Mode** ของตัวเอง (Loop / Trigger-on-bit) — ระบุไว้ใน table แล้วว่าจะโพลล์ยังไง. ไม่ต้องใช้ timer มา gate MBUS. ใช้ LD M8151 (Always ON) ให้ MBUS instruction ถูกเรียกตลอดเวลาที่ PLC อยู่ใน RUN mode, แล้วให้ table คุมจังหวะเอง. ถ้าต้องการเปลี่ยน polling interval ให้ไปแก้ใน table ไม่ใช่ใน ladder
Modbus Table ที่ชื่อ 'sensor' ต้องสร้างเอง?
ใช่ — MBUS instruction อ้างอิงชื่อ table ที่สร้างใน IDE. ตาราง metadata (station/FC/register mapping) ไม่ได้ embed ใน CSV import — ต้อง setup ในโปรเจกต์ SamSoar2022 ก่อน compile. ถ้าชื่อไม่ตรงจะ compile error
ถ้าต้องการ poll slave หลายตัวทำยังไง?
เพิ่ม rows ใน Modbus Table พร้อมเปลี่ยน Station แต่ละ row. MBUS จะ poll ทุก rows ใน table ตามลำดับ. หรือสร้างหลาย Table แล้ว MBUS สลับกัน (ใช้ index D701 เช็ค)
Slave offline แล้ว M90 (error flag) ติดค้างเลย — Reset ยังไง?
M90 คือ comb output ของ D700 ≠ 0. พอ slave กลับมา online, poll ถัดไป → D700=0 → M90 ดับอัตโนมัติ. ไม่ต้อง reset manual. ถ้าต้องการ latch error (ต้อง ack ก่อนดับ) เพิ่ม SET/RST pattern
เอาค่า 32-bit float จาก inverter (Modbus register มี 2 words) ทำยังไง?
ตั้ง Modbus Table: FC 03, Slave Register = address ของ high word, Count = 2. อ่านมาเก็บใน 2 registers ของ Master (เช่น D100, D101). บางยี่ห้อ inverter ใช้ endianness ต่างกัน — ใช้ ROLD D100 K16 D110 เพื่อ swap words ก่อนแปลงเป็น float. ดู docs/plc/modbus/plc-modbus-endianness
เอกสารที่เกี่ยวข้อง
วิธีนำเข้าไฟล์ใน SamSoar2022
- ดาวน์โหลดไฟล์
modbus-rtu-master.csvจากแท็บ CSV ด้านบน - เปิด SamSoar2022 → สร้างโปรเจกต์ใหม่ (หรือเปิดโปรเจกต์ที่มีอยู่)
- เมนู File → Import → เลือกไฟล์
.csvที่เพิ่งดาวน์โหลด - กด F4 เพื่อ Compile — ตรวจสอบว่าไม่มี error
- ตรวจการต่อสายตามตาราง I/O ด้านบน แล้ว Download โปรแกรมเข้า PLC

