dianartanto

Modbus Slave dengan Software Serial

Sep 6th, 2026
36
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <ModbusRTU.h>
  2. #include <SoftwareSerial.h>
  3. SoftwareSerial S(2, 3);
  4. #define SLAVE_ID 4 //SILAHKAN DIGANTI ANGKANYA, SLAVE_ID 1 UNTUK HARDWARE SLAVE 1 dst.nya
  5. ModbusRTU mb;
  6. void setup() {
  7.   Serial.begin(9600);
  8.   S.begin(9600);
  9.   mb.begin(&S);
  10.   //mb.begin(&Serial, RXTX_PIN);  //or use RX/TX direction control pin (if required)
  11.   mb.setBaudrate(9600);
  12.   mb.slave(SLAVE_ID);
  13.   pinMode(4, INPUT_PULLUP);
  14.   pinMode(5, OUTPUT);
  15.   pinMode(6, OUTPUT);
  16.   mb.addCoil(0);  //tombol D4
  17.   mb.addCoil(1);  //LED D5
  18.   mb.addCoil(2);  //Buzzer D6
  19. }
  20. void loop() {
  21.   mb.task();
  22.   mb.Coil(0, digitalRead(4));
  23.   digitalWrite(5, mb.Coil(1));
  24.   digitalWrite(6, mb.Coil(2));
  25.   delay(100);
  26. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment