ModBus Question for Arduino Uno

I am new to Modbus.

I am programing 3 Arduino Unos to communicate with one another using RS-485 and Modbus communication protocol. 2 of the Arduinos are programmed as slaves and the other one is the master.

One of the slaves has 5 switches connected to it and the other has 5 relays. When a switch is flipped on, a light bulb connected to the relay (on the other slave) will turn on.

I have the programs for both slaves working. Now, I'm programing the master.

I've been using example codes online to learn how to program a Modbus device. Some of the example codes for the ModbusMaster.h library, shows:

node.begin(1, Serial);

The comments say that this line on code is setting slave ID to 1. However, to my understanding, a modbus master does not need a slave ID.

Can someone please explain to me what this function is doing.

Thank you in advance.

That's correct but such an ID does no harm.

1 Like

It sets the slave ID of the slave you want to communicate with. Use one object per slave.

1 Like

Thank you for your message.

If i have 3 Arduinos communicating with one another using RS-485 and Modbus, should i use this function in the master's program in void setup() as followed.

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

Serial.begin(115200);             //Baud Rate as 115200

node.begin(1, Serial);            //Slave ID 1
node.begin(2, Serial);            //Slave ID 2

node.preTransmission(preTransmission);   //Callback for configuring RS-485 Transreceiver correctly
node.postTransmission(postTransmission);
}

For clarification, the master does not have a slave ID and the other two Arduinos slave IDs are 1 and 2.

Thanks again,

Have you checked the examples coming with the library?

not really.
You define two "server" (old: "slave") objects and assign the ID. like pylon explained already 3h ago.

So you might want something like:
global:

ModbusMaster node;
ModbusMaster nodeB;

and in setup

node.begin(1, Serial);            //Slave ID 1
nodeB.begin(2, Serial);            //Slave ID 2

node.preTransmission(preTransmission);   //Callback for configuring RS-485 Transreceiver correctly
node.postTransmission(postTransmission);
nodeB.preTransmission(preTransmission);   //Callback for configuring RS-485 Transreceiver correctly
nodeB.postTransmission(postTransmission);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.