1

I am trying to use following code but its not working. Need a help in this regard.

#include <SimpleModbusMaster.h>
#define baudrate 115200
#define timeout 2000
#define polling 500
#define retry_count 5
#define Txenablepins 2
#define Led1 12
#define totalregs 9
enum{
  PACKET1,
  PACKET2,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];

unsigned int regs[9];
unsigned int write_regs[1];

void setup() {

    packet1->id = 1;
    packet1->function = PRESET_MULTIPLE_REGISTERS;
    packet1->address = 0;
    packet1->no_of_registers = 1;
    packet1->register_array = write_regs;
    
    packet2->id = 1;
    packet2->function = READ_HOLDING_REGISTERS;
    packet2->address = 0;
    packet2->no_of_registers = 1;
    packet2->register_array = regs;

modbus_configure(baudrate, timeout, polling, retry_count, Txenablepins, packets, TOTAL_NO_OF_PACKETS);
pinMode(Led1, OUTPUT);

}

void loop() {

unsigned int connection_status1 = modbus_update(&packets[PACKET1]);
unsigned int connection_status2 = modbus_update(&packets[PACKET2]);
if(connection_status1==1 and connection_status2==1){
digitalWrite(Led1, HIGH);
}
else{
digitalWrite(Led1, LOW);}
checkResponse();


}

This is my arduino slave code

#include <SimpleModbusSlave.h>

#define  ledPin  11 


enum {
    ADC0,
    ADC1,
    ADC2,
    ADC3,
    ADC4,
    ADC5,
    LED_STATE,
    BUTTON_STATE,
    TOTAL_ERRORS,
    TOTAL_REGS_SIZE
};

unsigned int holdingRegs[TOTAL_REGS_SIZE];

void setup() {
 // put your setup code here, to run once:
   modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE, 0);
   
   pinMode(ledPin, OUTPUT);
   Serial.flush(); 
}

void loop() {
  // put your main code here, to run repeatedly:


  holdingRegs[TOTAL_ERRORS] = modbus_update(holdingRegs);
  
  int val = analogRead(ADC0);

  holdingRegs[0] = val;

  delayMicroseconds(50);


if(holdingRegs[0] == 1){
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(ledPin, LOW);
}




}

Using this library for modbus master and slave

4
  • what does not working mean? ... what actually happens? ... please update your post with a clear description
    – jsotola
    Mar 23 at 14:30
  • add some debugging code ... right now, you have no way of knowing what the programs are doing
    – jsotola
    Mar 23 at 15:39
  • "control two slaves with one master but its not working" You were able to get it to control one?
    – timemage
    Mar 23 at 19:40
  • The library you are using uses Serial port for modbus communication, that means that you can't open the Serial Monitor and the only way to know whether it is working or not is by observing the LED. You should either take a look at the library SoftwareSerail examples or find a better Modbus library.
    – hcheung
    Mar 24 at 6:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.