diff --git a/examples/RTU-master/RTU-Master.ino b/examples/RTU-master/RTU-Master.ino index 25ae0cb..fb4be75 100644 --- a/examples/RTU-master/RTU-Master.ino +++ b/examples/RTU-master/RTU-Master.ino @@ -1,32 +1,51 @@ /* ModbusRTU ESP8266/ESP32 Read multiple coils from slave device example - + (c)2019 Alexander Emelianov (a.m.emelianov@gmail.com) https://github.com/emelianov/modbus-esp8266 + + modified 13 May 2020 + by brainelectronics + This code is licensed under the BSD New License. See LICENSE.txt for more info. */ #include -#ifdef ESP8266 +#if defined(ESP8266) #include - SoftwareSerial S(D1, D2, false, 256); + // SoftwareSerial S(D1, D2, false, 256); + + // receivePin, transmitPin, inverse_logic, bufSize, isrBufSize + // connect RX to D2 (GPIO4, Arduino pin 4), TX to D1 (GPIO5, Arduino pin 4) + SoftwareSerial S(4, 5); #endif ModbusRTU mb; bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) { +#ifdef ESP8266 + Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap()); +#elif ESP32 Serial.printf_P("Request result: 0x%02X, Mem: %d\n", event, ESP.getFreeHeap()); +#else + Serial.print("Request result: 0x"); + Serial.print(event, HEX); +#endif return true; } void setup() { Serial.begin(115200); - #ifdef ESP8266 + #if defined(ESP8266) S.begin(9600, SWSERIAL_8N1); mb.begin(&S); + #elif defined(ESP32) + Serial1.begin(9600, SERIAL_8N1); + mb.begin(&Serial1); #else - Serial1.begin(9600, SERIAL_8N1, 17, 18); + Serial1.begin(9600, SERIAL_8N1); + mb.setBaudrate(9600); mb.begin(&Serial1); #endif mb.master(); diff --git a/examples/RTU-slave/RTU-slave.ino b/examples/RTU-slave/RTU-slave.ino index ad1a326..73e7794 100644 --- a/examples/RTU-slave/RTU-slave.ino +++ b/examples/RTU-slave/RTU-slave.ino @@ -1,9 +1,14 @@ /* ModbusRTU ESP8266/ESP32 Simple slave example - + (c)2019 Alexander Emelianov (a.m.emelianov@gmail.com) https://github.com/emelianov/modbus-esp8266 + + modified 13 May 2020 + by brainelectronics + + This code is licensed under the BSD New License. See LICENSE.txt for more info. */ #include @@ -14,8 +19,13 @@ ModbusRTU mb; void setup() { - Serial.begin(9600, SERIAL_8N1) + Serial.begin(9600, SERIAL_8N1); +#if defined(ESP32) || defined(ESP8266) + mb.begin(&Serial); +#else + mb.setBaudrate(9600); mb.begin(&Serial); +#endif mb.slave(SLAVE_ID); mb.addHreg(REGN); mb.Hreg(REGN, 100); diff --git a/src/ModbusIP_ESP8266.cpp b/src/ModbusIP_ESP8266.cpp index fbd2a85..c1962a7 100644 --- a/src/ModbusIP_ESP8266.cpp +++ b/src/ModbusIP_ESP8266.cpp @@ -3,6 +3,8 @@ Copyright (C) 2014 Andr� Sarmento Barbosa 2017-2019 Alexander Emelianov (a.m.emelianov@gmail.com) */ +#if defined(ESP32) || defined(ESP8266) + #include "ModbusIP_ESP8266.h" ModbusIP::ModbusIP() { @@ -423,4 +425,6 @@ ModbusIP::~ModbusIP() { delete client[i]; client[i] = nullptr; } -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/src/ModbusIP_ESP8266.h b/src/ModbusIP_ESP8266.h index fe4618f..0ed2f76 100644 --- a/src/ModbusIP_ESP8266.h +++ b/src/ModbusIP_ESP8266.h @@ -5,6 +5,8 @@ */ #pragma once +#if defined(ESP32) || defined(ESP8266) + #include #ifdef ESP8266 #include @@ -123,4 +125,6 @@ class ModbusIP : public Modbus { uint16_t writeOffset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT); */ -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/src/ModbusRTU.cpp b/src/ModbusRTU.cpp index 18e4c96..a0a4602 100644 --- a/src/ModbusRTU.cpp +++ b/src/ModbusRTU.cpp @@ -43,6 +43,10 @@ uint16_t ModbusRTU::crc16(uint8_t address, uint8_t* frame, uint8_t pduLen) { return (CRCHi << 8) | CRCLo; } +void ModbusRTU::setBaudrate(uint32_t baud) { + _baudrate = baud; +} + bool ModbusRTU::begin(Stream* port) { _port = port; _t = 2; @@ -50,10 +54,22 @@ bool ModbusRTU::begin(Stream* port) { } bool ModbusRTU::begin(HardwareSerial* port, int16_t txPin) { - uint32_t baud = port->baudRate(); - #if defined(ESP8266) - maxRegs = port->setRxBufferSize(MODBUS_MAX_FRAME) / 2 - 3; - #endif + uint32_t baud = 0; + if (_baudrate > 0) { + baud = _baudrate; + } + else { + #if defined(ESP32) || defined(ESP8266) + // baudRate() only available with ESP32+ESP8266 + baud = port->baudRate(); + #else + #warning "Using default 9600 baud as not specified with setBaudrate()" + baud = 9600; + #endif + } + #if defined(ESP8266) + maxRegs = port->setRxBufferSize(MODBUS_MAX_FRAME) / 2 - 3; + #endif _port = port; _txPin = txPin; if (_txPin >= 0) { @@ -96,7 +112,7 @@ bool ModbusRTU::rawSend(uint8_t slaveId, uint8_t* frame, uint8_t len) { #endif _port->write(slaveId); //Send slaveId _port->write(frame, len); // Send PDU - _port->write(newCrc >> 8); //Send CRC + _port->write(newCrc >> 8); //Send CRC _port->write(newCrc & 0xFF);//Send CRC #ifdef ESP32 portEXIT_CRITICAL(&mux); diff --git a/src/ModbusRTU.h b/src/ModbusRTU.h index 19c0caa..5b323df 100644 --- a/src/ModbusRTU.h +++ b/src/ModbusRTU.h @@ -23,6 +23,7 @@ class ModbusRTU : public Modbus { protected: Stream* _port; int16_t _txPin = -1; + uint32_t _baudrate = -1; unsigned int _t; // inter-frame delay in mS uint32_t t = 0; bool isMaster = false; @@ -43,10 +44,11 @@ class ModbusRTU : public Modbus { bool cleanup(); // Free clients if not connected and remove timedout transactions and transaction with forced events uint16_t crc16(uint8_t address, uint8_t* frame, uint8_t pdulen); public: + void setBaudrate(uint32_t baud = -1); #if defined(ESP8266) bool begin(SoftwareSerial* port, int16_t txPin=-1); #endif - bool begin(HardwareSerial* port, int16_t txPin=-1); + bool begin(HardwareSerial* port, int16_t txPin=-1); bool begin(Stream* port); void task(); void master() { isMaster = true; };