/* Modbus Library for Arduino Example - Modbus RTU Client Read Holding Registers from Modbus RTU Server in blocking way ESP8266 Example (c)2020 Alexander Emelianov (a.m.emelianov@gmail.com) https://github.com/emelianov/modbus-esp8266 */ #include #include #define SLAVE_ID 1 #define FIRST_REG 0 #define REG_COUNT 25 SoftwareSerial S(D5, D6); // RX = D5, TX = D6 ModbusRTU mb; uint16_t res[REG_COUNT]; bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Callback to monitor errors if (event != Modbus::EX_SUCCESS) { Serial.print("Request result: 0x"); Serial.print(event, HEX); } return true; } void setup() { Serial.begin(115200); S.begin(9600, SWSERIAL_8N1); mb.begin(&S); // bool begin(SoftwareSerial* port, int16_t txPin=-1, bool direct=true); // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.master(); } void loop() { // 1 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 0, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 2 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 25, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 3 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 50, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 4 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 75, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 5 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 100, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 6 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 125, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } delay(1000); // 7 if (!mb.slave()) { // Check if no transaction in progress // Array vullen met nullen. Als je dat niet doet staat er rommel in. for (int n = 0; n < REG_COUNT; n++) { res[n] = 0; } mb.readIreg(SLAVE_ID, 150, res, 25, cb); // Send Read Ireg from Modbus Server (SLAVE_ID, FIRST_REG, res, REG_COUNT, cb) while (mb.slave()) { // Check if transaction is active mb.task(); yield(); } Serial.println(); for (int n = 0; n < REG_COUNT; n++) { Serial.print(res[n], HEX); Serial.print(" "); } } Serial.println("klaar"); delay(2000); }