RTU Modbus TCP from arduino
Bahan :
- Arduino uno
-
Ethernet shield R3
-
ethernet shield
Kabel data arduino
ethernet shield
Kabel data arduino
- Kabel UTP
-
Board Analog Input, Digital Input dan Digital
Output
Tutorial merakit RTU Modbus TCP Arduino
1. Masukkan
library Mudbus ke folder C:\Program Files\Arduino\libraries
2. Run
Software Arduino, bisa download disini https://www.arduino.cc/en/Main/Software
3. Masukin
sketch
/*
Author : Fuad Hasan
*/
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write
coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R
MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT
void setup()
{
uint8_t mac[] = { 0x91, 0xA1, 0xDA, 0x00, 0x52, 0x05 };
uint8_t ip[] = { 192, 168, 1, 10 };
uint8_t gateway[] = {
192, 168, 1, 1 };
uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac,
ip, gateway, subnet);
//Avoid pins
4,10,11,12,13 when using ethernet shield
delay(3000);
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Mb.R[21] = 1987;
}
void loop()
{
Mb.Run();
//Analog inputs 0-1023
Mb.R[0] =
(analogRead(A2)*1); // tegangan
Mb.R[1] =
(analogRead(A4)*1); // arus
Mb.R[2] =
(analogRead(A3)*1); // temperature
//Digital inputs
Mb.C[0] =
digitalRead(2);
Mb.C[1] = digitalRead(5);
Mb.C[2] =
digitalRead(6);
//Digital outputs
digitalWrite(7,
Mb.C[4]);
digitalWrite(8,
Mb.C[5]);
digitalWrite(9,
Mb.C[6]);
digitalWrite(A1,
Mb.C[7]);
digitalWrite(A0,
Mb.C[8]);
}
4. Tancapkan kabel
data ke arduino dan port usb laptop/PC
5. Klik Verify untuk Compile sketch
yang dicopy tadi,
6. Setting board
dan port arduino
- Pilih Tool – board – arduino uno
-
-
Pilih Tool – Port- com (sesuai arduino, cek di device
manager)
7. Upload Sketch ke arduino
8. Settiing IP
laptop/PC 192.168.1.87(87 bisa di ganti angka lain selain 10(ip RTU) dan 1(ip
gateway))
9. Tancapkan Ethernet
shield to arduino
10. Tancapkan kabel
UTP to port RJ45 ethernet shield dan port RJ45 laptop
11. Cek komunikasi
RTU menggunakan Command Promt (CMD)
-
Ketik perintah : Ping 192.168.1.10 untuk cek sesaat
-
Ketik perintah : ping 192.168.1.10 –t untuk
cek seterusnya
12. Test RTU
menggunakan modscan32
13. Setelah
koneksi menggunakan modscan berhasil, itu menandakan RTU Modbus TCP arduino
sudah sukses.
klik mudbus untuk tahap selanjutnya