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; 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 } }