ModbusMaster on a Mega: first meter answers, second one always times out
Startup guy, learning as I go. Arduino Mega + MAX485 on Serial1, two energy meters on the same RS485 pair, ids 1 and 2. Using the ModbusMaster library. In setup I do node.begin(1, Serial1) and reading meter 1 works great.
For meter 2 I made a second object:
ModbusMaster node;
ModbusMaster node2;
void setup() {
Serial1.begin(9600);
pinMode(DE_RE, OUTPUT);
node.begin(1, Serial1);
node.preTransmission(preTx);
node.postTransmission(postTx);
node2.begin(2, Serial1);
}
void loop() {
uint8_t r1 = node.readInputRegisters(0x0000, 2); // 0x00, fine
uint8_t r2 = node2.readInputRegisters(0x0000, 2); // 0xE2 every time
...
}
What’s the right way to change node? Should I call begin() again before each read instead?
Following, same question for our two chillers.
Are you sure meter 2 is set to id 2? Out of the box they’re often all 1.
Yes, set it to 2 on the front panel. If I swap and read only meter 2 with node (id 1 changed to 2) it works.
0xE2 is a response timeout. Same baud and parity on both meters?
two objects is the right idea, begin() just remembers the id and the serial port
Look at your setup(): node2 never gets the pre/post transmission callbacks. So when node2 sends, nobody switches the MAX485 into transmit, the request never leaves the Arduino, and meter 2 never answers. Hence 0xE2.
node2.begin(2, Serial1);
node2.preTransmission(preTx);
node2.postTransmission(postTx);
Calling node.begin(2, Serial1) before each read with one object works too, the callbacks stay set. I’d still keep two objects, it reads better.
Also add a short pause between the two reads, 20 to 50 ms. Some meters need a moment after answering before the bus is quiet again.
When you send to Hermetry, if the Mega (or the ESP next to it) is the gateway, put both meters in one message as two children:
v1/gw/telemetry
{"Meter 1": {"values": {"kw": 3.41, "kwh": 1203.4}},
"Meter 2": {"values": {"kw": 0.87, "kwh": 455.1}}}
Facepalm. Missing callbacks on node2, that was all. Both read every second now, and both meters showed up as their own devices in Hermetry. Thanks Ciaran!
Same mistake in my sketch, glad I followed this.
- Vous devez être connecté pour répondre à ce sujet.
Rejoignez la discussion
Connectez-vous pour poser et répondre à des questions, aimer les messages utiles et marquer la réponse qui a résolu votre problème. Chaque contribution rapporte des points et des niveaux.

