Skip to content

Commit 861f0d9

Browse files
committed
ModbusTCP: Fix Raw callback processing
1 parent 7b4b42a commit 861f0d9

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/ModbusSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ ESP32 only. Outgoing connection attempt timeout
8282
#define MODBUSIP_UNIQUE_CLIENTS
8383
#define MODBUSIP_MAX_READMS 100
8484
#define MODBUSIP_FULL
85-
#define MODBUSIP_DEBUG
85+
//#define MODBUSIP_DEBUG
8686
/*
8787
Allows to use DNS names as target
8888
Otherwise IP addresses only must be used

src/ModbusTCPTemplate.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ModbusTCPTemplate : public Modbus {
6464
#else
6565
DArray<TTransaction, 2, 2> _trans;
6666
#endif
67-
int16_t transactionId = 0; // Last started transaction. Increments on unsuccessful transaction start too.
67+
int16_t transactionId = 1; // Last started transaction. Increments on unsuccessful transaction start too.
6868
int8_t n = -1;
6969
bool autoConnectMode = false;
7070
uint16_t serverPort = 0;
@@ -115,6 +115,7 @@ class ModbusTCPTemplate : public Modbus {
115115
uint32_t eventSource() override;
116116
void autoConnect(bool enabled = true);
117117
void dropTransactions();
118+
uint16_t setTransactionId(uint16_t);
118119
#if defined(MODBUS_USE_STL)
119120
static IPAddress defaultResolver(const char*) {return IPADDR_NONE;}
120121
#else
@@ -371,19 +372,24 @@ uint16_t ModbusTCPTemplate<SERVER, CLIENT>::send(IPAddress ip, TAddress startreg
371372
#endif
372373
if (!ip)
373374
return 0;
374-
p = getSlave(ip);
375+
if (tcpserver) {
376+
p = getMaster(ip);
377+
} else {
378+
p = getSlave(ip);
379+
}
375380
if (p == -1 || !tcpclient[p]->connected()) {
376381
if (!autoConnectMode)
377382
goto cleanup;
378383
if (!connect(ip))
379384
goto cleanup;
380385
}
381-
transactionId++;
382-
if (!transactionId) transactionId = 1;
383386
_MBAP.transactionId = __swap_16(transactionId);
384387
_MBAP.protocolId = __swap_16(0);
385388
_MBAP.length = __swap_16(_len+1); //_len+1 for last byte from MBAP
386389
_MBAP.unitId = unit;
390+
transactionId++;
391+
if (!transactionId)
392+
transactionId = 1;
387393
bool writeResult;
388394
{ // for sbuf isolation
389395
size_t send_len = _len + sizeof(_MBAP.raw);
@@ -569,3 +575,12 @@ ModbusTCPTemplate<SERVER, CLIENT>::~ModbusTCPTemplate() {
569575
tcpclient[i] = nullptr;
570576
}
571577
}
578+
579+
template <class SERVER, class CLIENT>
580+
uint16_t ModbusTCPTemplate<SERVER, CLIENT>::setTransactionId(uint16_t t) {
581+
transactionId = t;
582+
if (!transactionId)
583+
transactionId = 1;
584+
return transactionId;
585+
}
586+

0 commit comments

Comments
 (0)