From d081505c6405e8a6445196995ba82a15e4c34083 Mon Sep 17 00:00:00 2001 From: Wiktor Burdecki Date: Sun, 27 Feb 2022 18:56:33 +0100 Subject: [PATCH 1/2] fixed WRITE_REGS(16) function first 2 bytes were flipped(high byte swapped with low byte) and all registers got value from first register --- src/modbus.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index 0206ff0..27144c8 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -173,15 +173,15 @@ inline mbus_status_t mbus_poll_response(mbus_t mb_context) { return mbus_send_data(mb_context, 6); case MBUS_FUNC_WRITE_REGS: - value = (uint16_t *)ctx->conf.recvbuf; - for (int i = 0; i < ctx->header.num; i++) { - ctx->conf.write(la + i, *value); - } - ctx->conf.sendbuf[2] = ctx->header.addr >> 8; - ctx->conf.sendbuf[3] = ctx->header.addr & 0xFF; - ctx->conf.sendbuf[4] = ctx->header.num >> 8; - ctx->conf.sendbuf[5] = ctx->header.num & 0xFF; - return mbus_send_data(mb_context, 6); + for (int i = 0; i < ctx->header.num; i++) { + uint16_t regvalue = ((uint16_t)ctx->conf.recvbuf[i*2]<<8)|((uint16_t)ctx->conf.recvbuf[i*2+1]); + ctx->conf.write(la + i, regvalue); + } + ctx->conf.sendbuf[2] = ctx->header.addr >> 8; + ctx->conf.sendbuf[3] = ctx->header.addr & 0xFF; + ctx->conf.sendbuf[4] = ctx->header.num >> 8; + ctx->conf.sendbuf[5] = ctx->header.num & 0xFF; + return mbus_send_data(mb_context, 6); } // end of switch } } From ab26806802792f31378bc0f6667f39c31df26af0 Mon Sep 17 00:00:00 2001 From: Wiktor Burdecki Date: Sun, 27 Feb 2022 19:03:31 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aec2769..5558e0f 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,18 @@ int mbus_send(const mbus_t context,const uint8_t* data, const uint16_t size){ /* Just ptr on any external object, you can get it by context */ mb_config.device = (void*) 0; - + + /*set up buffers for received and sent modbus data, declare MYMODBUSSENDBUFFER globally with adequate size*/ + uint8_t * pmodbusSendBuffer; + pmodbusSendBuffer=&MYMODBUSSENDBUFFER; + mb_config.sendbuf = pmodbusSendBuffer; + mb_config.sendbuf_sz = sizeof(MYMODBUSSENDBUFFER); + + uint8_t * pmodbusRecvBuffer; + pmodbusRecvBuffer=&MYMODBUSRECEIVEBUFFER; + mb_config.recvbuf = pmodbusRecvBuffer; + mb_config.recvbuf_sz = sizeof(MYMODBUSRECEIVEBUFFER); + /* This that function for sending some data (use sendbuf for buf) */ mb_config.send = &mbus_send;