Go Down

Topic: Arduino R3 get Read Holding(FC03) & Write Register(FC04) from Slave Modbus RTU (Read 4204 times) previous topic - next topic

Marbun

Halo everybody there, I have tried using Arduino R3 will monitoring Read Holding(FC03) & Write Register(FC04) from Slave Modbus RTU , my trial is still error not succeed yet can't display according to actual value.  For everybody who give me solution do appreciated thanks.
 here is coding below:

#include <SimpleModbusMaster.h>

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  PACKET2,
  // leave this last entry
  TOTAL_NO_OF_PACKETS
};

// Create an array of Packets for modbus_update()
Packet packets[TOTAL_NO_OF_PACKETS];

// Create a packetPointer to access each packet
// individually. This is not required you can access
// the array explicitly. E.g. packets[PACKET1].id = 2;
// This does become tedious though...
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];

// The data from the PLC will be stored
// in the regs array
unsigned int regs[9];

void setup()
{
  // read 3 registers starting at address 0
  packet1->id = 1;
  packet1->function = READ_HOLDING_REGISTERS;
  packet1->address = 1001; // RTU Modbus slave of device is 31001
  packet1->no_of_registers = 3;
  packet1->register_array = regs;
 
  // write the 9 registers to the PLC starting at address 3
  packet2->id = 1;
  packet2->function = PRESET_MULTIPLE_REGISTERS;
  packet2->address = 1; // RTU Modbus slave of device is 4001
  packet2->no_of_registers = 3;
  packet2->register_array = regs;
 
  // P.S. the register_array entries above can be different arrays
 
  // Initialize communication settings etc...
  modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
 
  pinMode(connection_error_led, OUTPUT);
}

void loop()
{
  unsigned int connection_status = modbus_update(packets);
 
  if (connection_status != TOTAL_NO_OF_PACKETS)
  {
    digitalWrite(connection_error_led, HIGH);
    // You could re-enable the connection by:
    //packets[connection_status].connection = true;
  }
  else
    digitalWrite(connection_error_led, LOW);
 
  // update the array with the counter data
  regs[3] = packet1->requests;
  regs[4] = packet1->successful_requests;
  regs[5] = packet1->total_errors;
  regs[6] = packet2->requests;
  regs[7] = packet2->successful_requests;
  regs[8] = packet2->total_errors;
}

santhoshrajavels

I will use elmeasure EN8400 i will connect Elmeasure with arduino the will communicate and the will display trAns in the elmeasure but value are not come in the elmeasure

Go Up