Intermediate
6 rungs
9 I/O

Modbus RTU Master — อ่านค่าจากเซนเซอร์ Slave

Modbus RTU Master — Read Slave Sensors

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 ก่อน:

  1. คลิกขวาที่ Modbus Table → Add → Add Modbus Table
  2. ตั้งชื่อ table = sensor
  3. เพิ่มรายการดังนี้:
#StationFunction CodeSlave RegisterCountMaster RegisterTrigger ModePurpose
1K103 (Read Holding)400014D100Loopอ่าน D0–D3 จาก slave ไป D100–D103
2K101 (Read Coils)000011M100Loopอ่าน coil 1 ของ slave ไป M100
  1. 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

Modbus RTU Master — อ่านค่าจากเซนเซอร์ Slave — Modbus RTU Master — Read Slave Sensors
PLC เป็น Modbus RTU Master อ่านค่าจาก slave ผ่าน COM1 — อ่าน 4 holding register + 1 coil พร้อม flag สถานะ (healthy / error / timeout) และนับจำนวนครั้งที่ error
N0Modbus Polling
MBUS แบบ always-on — cadence ของการ poll ตั้งใน Modbus Table (Loop/Trigger mode) ไม่ใช่ตรงนี้
M8151MBUSK1, sensor, D700, D701
N1Flag สถานะการสื่อสาร
Healthy flag: ถ้า Error code D700 = 0 → M91 (ok)
D700 = K0M91
Error flag: ถ้า Error code D700 ≠ 0 → M90 (error)
D700 <> K0M90
Timeout flag: Error 16 (0x10) = communication timeout → M92
D700 = K16M92
N2เคาน์เตอร์ Error
First scan — เคลียร์เคาน์เตอร์ error (D710)
M8150MOVK0, D710
นับ error: เพิ่ม D710 ทุกครั้งที่ M90 ขอบขาขึ้น (INC ต้องการ source + dest — increment in-place)
M90INCD710, D710
Network count: 3, gap: 12
นำเข้าไฟล์ใน SamSoar2022: File → Import → เลือกไฟล์ .csv

I/O Assignment

AddressRoleNote
COM1Port RS485 Masterตั้งเป็น Modbus Master ใน Project Settings → Communication
D100 – D103ค่า sensor ที่อ่านมา (จาก slave D0–D3)อ่านทุก 500 ms
M100สถานะ coil ที่อ่านมา (จาก slave coil 1)
D700รหัส Error ของ Modbus0=OK, 16=timeout, 24=frame error
D701Index คำสั่งปัจจุบันindex ใน Modbus table ที่กำลัง poll
M90Error flag (มี error)ใช้แจ้ง alert บน HMI
M91Healthy flag (ไม่มี error)ใช้โชว์ indicator สีเขียวบน HMI
M92Flag Timeoutติดเฉพาะเวลา slave ไม่ตอบ (Error 16)
D710เคาน์เตอร์ error สะสมเพิ่ม 1 ทุกครั้งที่ error — รีเซ็ตจาก HMI ได้

Parameters ที่ปรับได้

Modbus Table 'sensor'default: ตั้งใน IDE

Station 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

  1. ดาวน์โหลดไฟล์ modbus-rtu-master.csv จากแท็บ CSV ด้านบน
  2. เปิด SamSoar2022 → สร้างโปรเจกต์ใหม่ (หรือเปิดโปรเจกต์ที่มีอยู่)
  3. เมนู File → Import → เลือกไฟล์ .csv ที่เพิ่งดาวน์โหลด
  4. กด F4 เพื่อ Compile — ตรวจสอบว่าไม่มี error
  5. ตรวจการต่อสายตามตาราง I/O ด้านบน แล้ว Download โปรแกรมเข้า PLC