Professional Documents
Culture Documents
Giao tiếp Board PLC FX3U (FK3U) Với Arduino Uno Bằng RS485
Giao tiếp Board PLC FX3U (FK3U) Với Arduino Uno Bằng RS485
1) Cấu hình
D8120
No. Name Content
0(OFF) 1(ON)
B0 Data length 7 8
B1 Parity bit b2 b1
B2 0 0 None
0 1 Odd
1 1 Event
B3 Stop bit 1 2
B4 Transfer rate bps b7 b6 b5 b4
B5 0 0 1 1: 300
B6 0 1 0 0: 600
B7 0 1 0 1: 1200
0 1 1 1: 4800
1 0 0 0: 9600
1 0 0 1: 19200
B8 Start symbol Nothing Yes (D8124)
B9 Terminator Nothing Yes (D8125)
B10 Do not use
B11
B12 Communication b15 b14 b13 b12
B13 protocol 0 0 0 0 MITSUBISHI FX2N
B14 0 1 0 0 MODBUS RTU
B15 1 0 0 0 RTU MODBUS (IVRD, IVWR)
1 1 0 0 Free communication (RS,CCD)
Lệnh IVRD được sử dụng đọc giá trị từ trạm Slave, trong đó
D0: 8byte cao là địa chỉ trạm tớ, 8byte thấp là mã đọc (03,04) ví dụ D0= H103,
trong đó địa chỉ trạm tớ là 01 và mã đọc là 03:Holding register
D1: địa chỉ đầu tiên muốn đọc ở trạm tớ (D1=k0 tương ứng với địa chỉ là 30001 ở
trạm tớ)
K1: Số lượng thanh ghi muốn đọc (ở đây đọc 1 thanh ghi)
Lệnh IVWR được sử dụng để ghi giá trị từ trạm PLC xuống trạm tớ, trong đó
D10: 8byte cao là địa chỉ trạm tớ, 8byte thấp là mã ghi (06,16)
D11: Thanh ghi dữ liệu đầu tiên trên PLC cần ghi xuống trạm tớ
Dưới đây là đoạn chương trình đọc ghi dữ liệu cơ bản từ PLC đến arduino Uno
Tốc độ truyền 9600
8E1, RTU Modbus
1) Cấu hình
Thư viện cho Arduino: (Giải nén rồi copy cho vào đường dẫn C:\Program Files
(x86)\Arduino\libraries)
https://drive.google.com/file/d/1MPSntFqn4u3CBD6PLb_K3gMGlqVmhumc/view
?usp=sharing
https://program-plc.blogspot.com/2015/11/plc-modbus-master-arduino-modbus-
slave.html
2) Chương trình
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
modbusDevice regBank;
modbusSlave slave;
#define LED1 11
#define LED2 12
void setup()
/*
10001-19999 Digital Inputs, A master device can only read the values from these
registers
30001-39999 Analog Inputs, A master device can only read the values from these
registers
40001-49999 Analog Outputs, A master device can read and write to these registers
Analog values are 16 bit unsigned words stored with a range of 0-32767
Digital values are stored as bytes, a zero value is OFF and any nonzer value is ON
*/
regBank.add(30001);
regBank.add(30002);
regBank.add(40001);
regBank.add(40002);
slave._device = ®Bank;
slave.setBaud(&Serial,RS485Baud,RS485Format,RS485TxEnablePin);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
void loop()
digitalWrite(LED1, regBank.get(40001));
digitalWrite(LED2, regBank.get(40002));
regBank.set(30001, (word) 111); // ghi giá trị 111 vào thanh ghi MODBUS 30001
regBank.set(40001, (word) 112); // ghi giá trị 112 vào thanh ghi MODBUS 40001
slave.run();