Skip to content

Commit

Permalink
Refactored RTUMaster
Browse files Browse the repository at this point in the history
  • Loading branch information
graham22 committed May 6, 2020
1 parent 5134d48 commit e57053a
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 239 deletions.
7 changes: 0 additions & 7 deletions Code/ESP32/include/Enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,4 @@ enum ResultCode {
EX_CANCEL = 0xE6 // Custom. Transaction/request canceled
};

enum RtuReplyState {
RTU_IDLE = 0x01,
RTU_PENDING = 0x02,
RTU_COMPLETE = 0x03,
RTU_FAILED = 0x04
};

}
21 changes: 7 additions & 14 deletions Code/ESP32/include/RtuMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@
#include "Enumerations.h"

#define InterFrameDelay 2
#define MODBUSRTU_TIMEOUT 3000
#define MODBUSRTU_TIMEOUT 2000

namespace ModbusAdapter
{
class RtuMaster
{

byte _slaveId; // unit ID sent to slave, cleared after
byte _unitId; // configured unit ID from setup
Stream* _port;
int16_t _rtsPin = -1;
uint32_t _timestamp = 0;
uint8_t _sentFunctionCode = 0;
void* _callBackFunction = nullptr;
unsigned int _interFrameDelay; // inter-frame delay in mS
uint32_t _lastTimeStamp = 0;
uint16_t _len = 0;

uint8_t _requestFrame[16];
uint8_t _requestFrameLen;
public:
RtuMaster();
void Init(long baudRate, uint8_t mosbussAddress);
bool Transfer(byte* frame, uint16_t len, void* cb);
void run();
void reset();
void Transfer(byte* frame, uint16_t len, void* cb);
void Run();

private:
bool rawSend(uint8_t slaveId, uint8_t* frame, uint8_t len);
bool cleanup();
uint16_t crc16(uint8_t address, uint8_t* frame, uint8_t pdulen);
};


}
4 changes: 2 additions & 2 deletions Code/ESP32/include/TcpSlave.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class TcpSlave
uint8_t* _frame = nullptr;
uint16_t _len = 0;
RtuMaster _rtuMaster;
ResultCode _rtuResultCode;
int8_t _clientIndex = -1;
uint16_t _slavePort = 0;
WiFiServer* _pServer;
WiFiClient* _pClients[MODBUSIP_MAX_CLIENTS];
RtuReplyState _rtuState;
void cleanup();
int8_t getFreeClient();
void exceptionResponse(FunctionCode fn, ResultCode excode);
Expand All @@ -52,7 +52,7 @@ class TcpSlave

public:
TcpSlave();
void cbResponse(ResultCode res, void *data, uint16_t len);
void cbResponse(ResultCode res, uint8_t* requestFrame, uint8_t requestFrameLen, uint8_t *data, uint16_t len);
void init(long baudRate, long tcpPort, uint8_t mosbusAddress);
void close();
void run();
Expand Down
2 changes: 1 addition & 1 deletion Code/ESP32/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lib_deps =
esp32ModbusRTU

build_flags =
-D 'CONFIG_VERSION="V2.0.1"' ; major.minor.build (major or minor will invalidate the configuration)
-D 'CONFIG_VERSION="V2.0.2"' ; major.minor.build (major or minor will invalidate the configuration)

-D WIFI_STATUS_PIN=2 ;LED Pin on the Dev board
-D WIFI_AP_PIN=15 ; force AP mode
Expand Down
Loading

0 comments on commit e57053a

Please sign in to comment.