Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 9 additions & 9 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down