22#include " RtuMaster.h"
33#include " Enumerations.h"
44#include " Log.h"
5+ #include " TcpSlave.h"
56
67namespace ModbusAdapter
78{
@@ -31,7 +32,7 @@ RtuMaster::RtuMaster()
3132{
3233}
3334
34- void RtuMaster::Init (long baudRate, uint8_t modbusAddress, int16_t rtsPin )
35+ void RtuMaster::Init (long baudRate, uint8_t modbusAddress)
3536{
3637 Serial1.begin (baudRate, SERIAL_8N1, GPIO_NUM_16, GPIO_NUM_17, false ); // Modbus connection
3738 while (!Serial1)
@@ -40,7 +41,7 @@ void RtuMaster::Init(long baudRate, uint8_t modbusAddress, int16_t rtsPin)
4041 }
4142 _port = &Serial1;
4243 _unitId = modbusAddress;
43- _rtsPin = rtsPin ;
44+ _rtsPin = GPIO_NUM_5 ;
4445 if (_rtsPin >= 0 ) {
4546 pinMode (_rtsPin, OUTPUT);
4647 digitalWrite (_rtsPin, LOW);
@@ -52,15 +53,17 @@ void RtuMaster::Init(long baudRate, uint8_t modbusAddress, int16_t rtsPin)
5253 }
5354}
5455
55- bool RtuMaster::Transfer (byte* frame, cbTransaction cb)
56+ bool RtuMaster::Transfer (byte* frame, uint16_t len, void * cb)
5657{
5758 if (_slaveId) return false ; // Break if waiting for previous request result
5859 _slaveId = _unitId;
5960 _sentFunctionCode = frame[0 ];
60- rawSend (_slaveId, frame, 5 );
61+ rawSend (_slaveId, frame, len );
6162 _timestamp = millis ();
6263 _callBackFunction = cb;
6364 _len = 0 ;
65+ // logd("RTU Transfer id: %d, len: %d", _slaveId, len);
66+ // printHexString(frame, len);
6467 return true ;
6568}
6669
@@ -76,24 +79,27 @@ void RtuMaster::run()
7679 return ;
7780 }
7881 if (millis () - _lastTimeStamp < _interFrameDelay) return ; // Wait data whitespace
79- uint8_t address = 0 ;
82+ uint8_t address = _port->read (); // first byte of frame = address
83+ _len--; // Decrease by slaveId byte
84+ if (_len == 0 ) {
85+ return ;
86+ }
8087 if (_slaveId == 0 ) { // Check if slaveId is set
8188 for (uint8_t i=0 ; i < _len ; i++) _port->read (); // Skip packet if is not expected
8289 _len = 0 ;
83- logw (" slaveId not set" );
8490 return ;
8591 }
86- else
87- {
88- uint8_t address = _port->read (); // first byte of frame = address
89- _len--; // Decrease by slaveId byte
90- }
91-
92- if (address != MODBUSRTU_BROADCAST && address != _slaveId) { // SlaveId Check
93- for ( uint8_t i= 0 ; i < _len ; i++) _port-> read (); // Skip packet if SlaveId doesn't mach
94- _len = 0 ;
95- logw ( " Invalid slaveId " ) ;
96- return ;
92+ if (address != _slaveId) { // SlaveId Check
93+ while (_len != 0 ) {
94+ address = _port->read (); // filter out non-modbus crap
95+ _len--;
96+ if (address == _slaveId) {
97+ break ;
98+ }
99+ }
100+ if ( _len == 0 ) {
101+ return ;
102+ }
97103 }
98104 uint8_t * frame = (uint8_t *) malloc (_len);
99105 if (!frame) { // Fail to allocate buffer
@@ -108,8 +114,8 @@ void RtuMaster::run()
108114 _len = _len - 2 ; // Decrease by CRC 2 bytes
109115 uint16_t crc = crc16 (address, frame, _len);
110116 if (frameCrc != crc) { // CRC Check
111- logw (" wrong crc for %d: [0x%x, 0x%x] len %d" , address, frameCrc, crc, _len);
112- printHexString (( char *) frame, _len);
117+ // logw("wrong crc for %d: [0x%x, 0x%x] len %d", address, frameCrc, crc, _len);
118+ // printHexString(frame, _len);
113119 free (frame);
114120 frame = nullptr ;
115121 _len = 0 ; // Cleanup if wrong crc
@@ -118,7 +124,8 @@ void RtuMaster::run()
118124 }
119125 if ((frame[0 ] & 0x7F ) == _sentFunctionCode) { // Check if function code the same as requested
120126 if (_callBackFunction) {
121- _callBackFunction (EX_SUCCESS, frame, _len);
127+ TcpSlave* p =(TcpSlave*) _callBackFunction;
128+ p->cbResponse (EX_SUCCESS, frame, _len);
122129 }
123130 _slaveId = 0 ;
124131 }
@@ -160,11 +167,18 @@ bool RtuMaster::rawSend(uint8_t slaveId, uint8_t* frame, uint8_t len) {
160167
161168bool RtuMaster::cleanup () {
162169 if (_slaveId && ((millis () - _timestamp) > MODBUSRTU_TIMEOUT)) {
163- if (_callBackFunction)
164- _callBackFunction (EX_TIMEOUT, nullptr , 0 );
170+ if (_callBackFunction) {
171+ TcpSlave* p =(TcpSlave*) _callBackFunction;
172+ p->cbResponse (EX_TIMEOUT, nullptr , 0 );
173+ }
165174 _slaveId = 0 ;
166175 return true ;
167176 }
168177 return false ;
169178}
179+
180+ void RtuMaster::reset () {
181+ _slaveId = 0 ;
182+ return ;
183+ }
170184}
0 commit comments