From 3ff31b07e3ba6ef12e19172fb9e8cde20e6c8ddb Mon Sep 17 00:00:00 2001 From: "Thorben T." Date: Tue, 22 Oct 2024 06:15:51 +0200 Subject: [PATCH] check result of modbus_set_slave() libmodbus considers some slave addresses invalid. it will report an error, but mbpoll previously ignored this, leading to a crash with an assertion failure. --- src/mbpoll.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/mbpoll.c b/src/mbpoll.c index e788742..47c32de 100644 --- a/src/mbpoll.c +++ b/src/mbpoll.c @@ -897,7 +897,11 @@ main (int argc, char **argv) { // libmodbus utilise les adresses PDU ! iStartReg = ctx.piStartRef[0] - ctx.iPduOffset; - modbus_set_slave (ctx.xBus, ctx.piSlaveAddr[0]); + iRet = modbus_set_slave (ctx.xBus, ctx.piSlaveAddr[0]); + if ( iRet != 0 ) { + vIoErrorExit ("setting slave address failed: %s", + modbus_strerror (errno)); + } ctx.iTxCount++; // Ecriture ------------------------------------------------------------ @@ -952,7 +956,11 @@ main (int argc, char **argv) { // Lecture ------------------------------------------------------------- for (i = 0; i < ctx.iSlaveCount; i++) { - modbus_set_slave (ctx.xBus, ctx.piSlaveAddr[i]); + iRet = modbus_set_slave (ctx.xBus, ctx.piSlaveAddr[i]); + if ( iRet != 0 ) { + vIoErrorExit ("setting slave address failed: %s", + modbus_strerror (errno)); + } ctx.iTxCount++; printf ("-- Polling slave %d...", ctx.piSlaveAddr[i]); @@ -1091,8 +1099,13 @@ vPrintReadValues (int iAddr, int iCount, xMbPollContext * ctx) { void vReportSlaveID (const xMbPollContext * ctx) { uint8_t ucReport[256]; + int iRet = 0; - modbus_set_slave (ctx->xBus, ctx->piSlaveAddr[0]); + iRet = modbus_set_slave (ctx->xBus, ctx->piSlaveAddr[0]); + if ( iRet != 0 ) { + vIoErrorExit ("setting slave address failed: %s", + modbus_strerror (errno)); + } // Affichage de la configuration printf ("Protocol configuration: ModBus %s\n", sModeList[ctx->eMode]); printf ("Slave configuration...: address = %d, report slave id\n", @@ -1100,7 +1113,7 @@ vReportSlaveID (const xMbPollContext * ctx) { vPrintCommunicationSetup (ctx); - int iRet = modbus_report_slave_id (ctx->xBus, 256, ucReport); + iRet = modbus_report_slave_id (ctx->xBus, 256, ucReport); if (iRet < 0) {