Tampilkan postingan dengan label Modbus. Tampilkan semua postingan
Tampilkan postingan dengan label Modbus. Tampilkan semua postingan

Contoh program Modbus TCP Slave

  1. Contoh program Modbus TCP Slave
#include <SPI.h>
#include <Ethernet.h>
#include <ModbusIP.h>

IPAddress ip(192,168,1,177);
ModbusIP mb;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  mb.server();
  mb.addCoil(0, true);
  mb.addHreg(0, 1234);
}

void loop()
{
  mb.task();
}

Contoh program Modbus TCP Master

  1. Contoh program Modbus TCP Master

#include <SPI.h>
#include <Ethernet.h>
#include <ModbusIP.h>

IPAddress server(192, 168, 1, 177);

EthernetClient client;
ModbusIP mb;

void setup()
{
  Ethernet.begin(mac);
  Serial.begin(9600);
  mb.client();

  if (!client.connect(server, 502))
  {
    Serial.println("Connection failed.");
    return;
  }
}

void loop()
{
  uint16_t data[6];

  int result = mb.readInputRegisters(1, 0, 6, data);

  if (result == mb.ku8MBSuccess)
  {
    Serial.print("Humidity: ");
    Serial.print(data[0]);
    Serial.print(" %\t Temperature: ");
    Serial.print(data[1]);
    Serial.println(" °C");
  }
  else
  {
    Serial.print("Error: ");
    Serial.println(mb.lastError());
  }

  delay(1000);
}

Contoh program Modbus RTU Slave

  1. Contoh program Modbus RTU Slave
#include <ModbusSlave.h>

ModbusSlave node;

void setup()
{
  Serial.begin(9600);
  node.begin(1, Serial);
}

void loop()
{
  node.poll();

  uint8_t coilData[1] = {1};
  node.addCoil(0, true, 1);
  node.addDiscreteInput(0, false, 1);
  node.addHreg(0, 1234, 1);
  node.addIreg(0, -1234, 1);

  delay(100);
}

Contoh program Modbus RTU Master

  1. Contoh program Modbus RTU Master
#include <ModbusMaster.h>

#define MAX485_DE      3
#define MAX485_RE_NEG  2

ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600);

  node.begin(1, Serial);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}

void loop()
{
  uint8_t result;
  uint16_t data[6];

  result = node.readInputRegisters(0, 6);

  if (result == node.ku8MBSuccess)
  {
    Serial.print("Humidity: ");
    Serial.print(data[0]);
    Serial.print(" %\t Temperature: ");
    Serial.print(data[1]);
    Serial.println(" °C");
  }
  else
  {
    Serial.print("Error: ");
    Serial.println(node.strError(result));
  }

  delay(1000);
}

Arduino Modbus master slave (1 master - 2 slave)

Berikut contoh program Arduino Modbus 1 master 2 slave yang dapat Anda gunakan sebagai referensi:

Master
#include <ModbusMaster.h>

// instantiate ModbusMaster object
ModbusMaster node;

void setup()
{
  Serial.begin(9600);
  
  // initialize Modbus communication parameters
  node.begin(1, Serial);
  node.setSlaveId(1);
}

void loop()
{
  uint8_t result;
  uint16_t data;

  // read data from slave 1
  result = node.readHoldingRegisters(0, 1);
  if (result == node.ku8MBSuccess) {
    data = node.getResponseBuffer(0);
    Serial.println("Data from slave 1: " + String(data));
  }
  else {
    Serial.println("Error reading data from slave 1");
  }

  // read data from slave 2
  result = node.readHoldingRegisters(1, 1);
  if (result == node.ku8MBSuccess) {
    data = node.getResponseBuffer(0);
    Serial.println("Data from slave 2: " + String(data));
  }
  else {
    Serial.println("Error reading data from slave 2");
  }
  
  delay(1000);
}


Slave #1

#include <ModbusSlave.h>

// instantiate ModbusSlave object
ModbusSlave node;

void setup()
{
  Serial.begin(9600);
  
  // initialize Modbus communication parameters
  node.begin(1, Serial);
  
  // set slave address
  node.setSlaveId(1);
  
  // set holding register data
  node.setHreg(0, 123);
}

void loop()
{
  // handle Modbus communication
  node.poll();
}


Slave #2

#include <ModbusSlave.h>

// instantiate ModbusSlave object
ModbusSlave node;

void setup()
{
  Serial.begin(9600);
  
  // initialize Modbus communication parameters
  node.begin(1, Serial);
  
  // set slave address
  node.setSlaveId(2);
  
  // set holding register data
  node.setHreg(1, 456);
}

void loop()
{
  // handle Modbus communication
  node.poll();
}


Pastikan untuk menghubungkan modul Modbus ke Arduino dengan benar dan sesuai dengan skema yang digunakan. Selain itu, pastikan juga bahwa setiap slave memiliki alamat yang berbeda dan komunikasi Modbus berjalan dengan benar.

Pi Coin

Pi Coin adalah cryptocurrency yang dikembangkan oleh tim di Stanford University. Berikut adalah penjelasan spesifik tentang Pi Coin: ### 1. ...