You're right on that one thank you. I've found a new piece of code and I'm just gonna do the same thing where I talk through my thoughts.
Ok so the code i've found is from https://www.industrialshields.com/blog/arduino-industrial-1/modbus-tcp-master-with-industrial-arduino-esp32-plcs-103:
/*
Copyright (c) 2018 Boot&Work Corp., S.L. All rights reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Ethernet.h> // This is the client;
#include <ModbusTCPMaster.h> // This is the master;
// Ethernet configuration values
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
uint8_t ip[] = { 10, 10, 10, 3 };
uint8_t slaveIp[] = { 10, 10, 10, 4 };
uint16_t slavePort = 502;
// Define the ModbusTCPMaster object
ModbusTCPMaster master;
// Ethernet client object used to connect to the slave
EthernetClient slave;
uint32_t lastSentTime = 0UL;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600UL);
// Begin Ethernet
Ethernet.begin(mac, ip);
Serial.println(Ethernet.localIP());
// NOTE: it is not necessary to start the modbus master object
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// Connect to slave if not connected
// The ethernet connection is managed by the application, not by the library
// In this case the connection is opened once
if (!slave.connected()) {
slave.stop();
slave.connect(slaveIp, slavePort);
}
// Send a request every 1000ms if connected to slave
if (slave.connected()) {
if (millis() - lastSentTime > 1000) {
// Send a Read Input Registers request to the slave with address 31
// It requests for 6 registers starting at address 0
// IMPORTANT: all read and write functions start a Modbus transmission, but they are not
// blocking, so you can continue the program while the Modbus functions work. To check for
// available responses, call master.available() function often.
if (!master.readInputRegisters(slave, 31, 0, 6)) {
// Failure treatment
}
lastSentTime = millis();
}
// Check available responses often
if (master.isWaitingResponse()) {
ModbusResponse response = master.available();
if (response) {
if (response.hasError()) {
// Response failure treatment. You can use response.getErrorCode()
// to get the error code.
} else {
// Get the input registers values from the response
Serial.print("Input registers values: ");
for (int i = 0; i < 6; ++i) {
Serial.print(response.getRegister(i));
Serial.print(',');
}
Serial.println();
}
}
}
}
}
Ok, few questions about the initialisation: Can't find the documentation for "ModbusTCPMaster.h" does anyone know where to find that?
I am also a bit unsure as to what the name "mac" means (I've seen this in other examples and it isn't clear to me), more for my own understanding this one, also a bit unclear what is being referenced in this and what these values mean. I understand the rest of the initialisations.
I think I mostly understand the loop also, but line 64 which is:
if (!master.readInputRegisters(slave, 31, 0, 6))
I am a bit confused on this one, I understand slave as it's the device being requested, but then 31 I am unsure on, is this the master or slaves address or something else entirely? Then 0 for me I am assuming is where I set 30845 which will be the data I want, and then 6 I think I need 2?
I think I understand the rest, does anyone see why this would not work for my intended purpose? Reading in a value on one of the slaves registers.