modbus: rename internal designations and do few minor fixups
Prefix internal functions and structs with modbus_. Use unit_id consistently instead of node_addr. Fix mbm_ remainder and rename to mbc_. Rename struct modbus_frame to modbus_adu since ADU is closer to what the structure represents. Let the compiler/linker do the job and remove ifdef around mbc_validate_fc03fp_response(). Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
This commit is contained in:
parent
669d3e9e91
commit
e3c630d25f
6 changed files with 279 additions and 281 deletions
|
@ -41,7 +41,7 @@ extern "C" {
|
|||
* Sends a Modbus message to read the status of coils from a server.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Coil starting address
|
||||
* @param coil_tbl Pointer to an array of bytes containing the value
|
||||
* of the coils read.
|
||||
|
@ -63,7 +63,7 @@ extern "C" {
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_read_coils(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const coil_tbl,
|
||||
const uint16_t num_coils);
|
||||
|
@ -75,7 +75,7 @@ int modbus_read_coils(const int iface,
|
|||
* a server.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Discrete input starting address
|
||||
* @param di_tbl Pointer to an array that will receive the state
|
||||
* of the discrete inputs.
|
||||
|
@ -97,7 +97,7 @@ int modbus_read_coils(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_read_dinputs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const di_tbl,
|
||||
const uint16_t num_di);
|
||||
|
@ -109,7 +109,7 @@ int modbus_read_dinputs(const int iface,
|
|||
* from a server.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Register starting address
|
||||
* @param reg_buf Is a pointer to an array that will receive
|
||||
* the current values of the holding registers from
|
||||
|
@ -120,7 +120,7 @@ int modbus_read_dinputs(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_read_holding_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs);
|
||||
|
@ -132,7 +132,7 @@ int modbus_read_holding_regs(const int iface,
|
|||
* a server.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Register starting address
|
||||
* @param reg_buf Is a pointer to an array that will receive
|
||||
* the current value of the holding registers
|
||||
|
@ -143,7 +143,7 @@ int modbus_read_holding_regs(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_read_input_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs);
|
||||
|
@ -154,14 +154,14 @@ int modbus_read_input_regs(const int iface,
|
|||
* Sends a Modbus message to write the value of single coil to a server.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param coil_addr Coils starting address
|
||||
* @param coil_state Is the desired state of the coil
|
||||
*
|
||||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_write_coil(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t coil_addr,
|
||||
const bool coil_state);
|
||||
|
||||
|
@ -172,14 +172,14 @@ int modbus_write_coil(const int iface,
|
|||
* to a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Coils starting address
|
||||
* @param reg_val Desired value of the holding register
|
||||
*
|
||||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_write_holding_reg(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
const uint16_t reg_val);
|
||||
|
||||
|
@ -189,7 +189,7 @@ int modbus_write_holding_reg(const int iface,
|
|||
* Sends a Modbus message to perform a diagnostic function of a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param sfunc Diagnostic sub-function code
|
||||
* @param data Sub-function data
|
||||
* @param data_out Pointer to the data value
|
||||
|
@ -197,7 +197,7 @@ int modbus_write_holding_reg(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_request_diagnostic(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t sfunc,
|
||||
const uint16_t data,
|
||||
uint16_t *const data_out);
|
||||
|
@ -208,7 +208,7 @@ int modbus_request_diagnostic(const int iface,
|
|||
* Sends a Modbus message to write to coils on a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Coils starting address
|
||||
* @param coil_tbl Pointer to an array of bytes containing the value
|
||||
* of the coils to write.
|
||||
|
@ -230,7 +230,7 @@ int modbus_request_diagnostic(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_write_coils(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const coil_tbl,
|
||||
const uint16_t num_coils);
|
||||
|
@ -242,7 +242,7 @@ int modbus_write_coils(const int iface,
|
|||
* to a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Register starting address
|
||||
* @param reg_buf Is a pointer to an array containing
|
||||
* the value of the holding registers to write.
|
||||
|
@ -253,7 +253,7 @@ int modbus_write_coils(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_write_holding_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs);
|
||||
|
@ -265,7 +265,7 @@ int modbus_write_holding_regs(const int iface,
|
|||
* holding registers from a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Register starting address
|
||||
* @param reg_buf Is a pointer to an array that will receive
|
||||
* the current values of the holding registers from
|
||||
|
@ -276,7 +276,7 @@ int modbus_write_holding_regs(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_read_holding_regs_fp(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
float *const reg_buf,
|
||||
const uint16_t num_regs);
|
||||
|
@ -288,7 +288,7 @@ int modbus_read_holding_regs_fp(const int iface,
|
|||
* to a server unit.
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param start_addr Register starting address
|
||||
* @param reg_buf Is a pointer to an array containing
|
||||
* the value of the holding registers to write.
|
||||
|
@ -299,7 +299,7 @@ int modbus_read_holding_regs_fp(const int iface,
|
|||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_write_holding_regs_fp(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
float *const reg_buf,
|
||||
const uint16_t num_regs);
|
||||
|
@ -350,7 +350,7 @@ int modbus_iface_get_by_name(const char *iface_name);
|
|||
* @brief Configure Modbus Interface as server
|
||||
*
|
||||
* @param iface Modbus interface index
|
||||
* @param node_addr Modbus unit address of the server
|
||||
* @param unit_id Modbus unit ID of the server
|
||||
* @param baud Baudrate of the serial line
|
||||
* @param parity UART's parity setting:
|
||||
* UART_CFG_PARITY_NONE,
|
||||
|
@ -361,7 +361,7 @@ int modbus_iface_get_by_name(const char *iface_name);
|
|||
*
|
||||
* @retval 0 If the function was successful
|
||||
*/
|
||||
int modbus_init_server(const uint8_t iface, const uint8_t node_addr,
|
||||
int modbus_init_server(const uint8_t iface, const uint8_t unit_id,
|
||||
const uint32_t baud, enum uart_config_parity parity,
|
||||
struct modbus_user_callbacks *const cb,
|
||||
bool ascii_mode);
|
||||
|
|
|
@ -26,16 +26,16 @@
|
|||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(modbus_c, CONFIG_MODBUS_LOG_LEVEL);
|
||||
|
||||
static int mbm_validate_response_fc(struct modbus_context *ctx,
|
||||
const uint8_t node_addr,
|
||||
static int mbc_validate_response_fc(struct modbus_context *ctx,
|
||||
const uint8_t unit_id,
|
||||
uint8_t fc)
|
||||
{
|
||||
uint8_t resp_fc = ctx->rx_frame.fc;
|
||||
uint8_t excep_code = ctx->rx_frame.data[0];
|
||||
uint8_t resp_fc = ctx->rx_adu.fc;
|
||||
uint8_t excep_code = ctx->rx_adu.data[0];
|
||||
const uint8_t excep_bit = BIT(7);
|
||||
const uint8_t excep_mask = BIT_MASK(7);
|
||||
|
||||
if (node_addr != ctx->rx_frame.addr) {
|
||||
if (unit_id != ctx->rx_adu.unit_id) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
|
@ -54,17 +54,16 @@ static int mbm_validate_response_fc(struct modbus_context *ctx,
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
|
||||
static int mbm_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl)
|
||||
static int mbc_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl)
|
||||
{
|
||||
uint8_t resp_byte_cnt;
|
||||
uint8_t req_byte_cnt;
|
||||
uint16_t req_qty;
|
||||
uint8_t *resp_data;
|
||||
|
||||
resp_byte_cnt = ctx->rx_frame.data[0];
|
||||
resp_data = &ctx->rx_frame.data[1];
|
||||
req_qty = sys_get_be16(&ctx->tx_frame.data[2]);
|
||||
resp_byte_cnt = ctx->rx_adu.data[0];
|
||||
resp_data = &ctx->rx_adu.data[1];
|
||||
req_qty = sys_get_be16(&ctx->tx_adu.data[2]);
|
||||
req_byte_cnt = req_qty * sizeof(float);
|
||||
|
||||
if (req_byte_cnt != resp_byte_cnt) {
|
||||
|
@ -81,10 +80,9 @@ static int mbm_validate_fc03fp_response(struct modbus_context *ctx, float *ptbl)
|
|||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int mbm_validate_rd_response(struct modbus_context *ctx,
|
||||
const uint8_t node_addr,
|
||||
static int mbc_validate_rd_response(struct modbus_context *ctx,
|
||||
const uint8_t unit_id,
|
||||
uint8_t fc,
|
||||
uint8_t *data)
|
||||
{
|
||||
|
@ -100,10 +98,10 @@ static int mbm_validate_rd_response(struct modbus_context *ctx,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
resp_byte_cnt = ctx->rx_frame.data[0];
|
||||
resp_data = &ctx->rx_frame.data[1];
|
||||
req_qty = sys_get_be16(&ctx->tx_frame.data[2]);
|
||||
req_addr = sys_get_be16(&ctx->tx_frame.data[0]);
|
||||
resp_byte_cnt = ctx->rx_adu.data[0];
|
||||
resp_data = &ctx->rx_adu.data[1];
|
||||
req_qty = sys_get_be16(&ctx->tx_adu.data[2]);
|
||||
req_addr = sys_get_be16(&ctx->tx_adu.data[0]);
|
||||
|
||||
switch (fc) {
|
||||
case MODBUS_FC01_COIL_RD:
|
||||
|
@ -124,10 +122,10 @@ static int mbm_validate_rd_response(struct modbus_context *ctx,
|
|||
case MODBUS_FC03_HOLDING_REG_RD:
|
||||
if (IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS) &&
|
||||
(req_addr >= MODBUS_FP_EXTENSIONS_ADDR)) {
|
||||
err = mbm_validate_fc03fp_response(ctx, (float *)data);
|
||||
err = mbc_validate_fc03fp_response(ctx, (float *)data);
|
||||
break;
|
||||
}
|
||||
/* fallthrough */
|
||||
__fallthrough;
|
||||
case MODBUS_FC04_IN_REG_RD:
|
||||
req_byte_cnt = req_qty * sizeof(uint16_t);
|
||||
if (req_byte_cnt != resp_byte_cnt) {
|
||||
|
@ -151,8 +149,8 @@ static int mbm_validate_rd_response(struct modbus_context *ctx,
|
|||
return err;
|
||||
}
|
||||
|
||||
static int mbm_validate_fc08_response(struct modbus_context *ctx,
|
||||
const uint8_t node_addr,
|
||||
static int mbc_validate_fc08_response(struct modbus_context *ctx,
|
||||
const uint8_t unit_id,
|
||||
uint16_t *data)
|
||||
{
|
||||
int err;
|
||||
|
@ -165,10 +163,10 @@ static int mbm_validate_fc08_response(struct modbus_context *ctx,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
req_sfunc = sys_get_be16(&ctx->tx_frame.data[0]);
|
||||
req_data = sys_get_be16(&ctx->tx_frame.data[2]);
|
||||
resp_sfunc = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
resp_data = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
req_sfunc = sys_get_be16(&ctx->tx_adu.data[0]);
|
||||
req_data = sys_get_be16(&ctx->tx_adu.data[2]);
|
||||
resp_sfunc = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
resp_data = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
if (req_sfunc != resp_sfunc) {
|
||||
LOG_ERR("Mismatch in the sub-function code");
|
||||
|
@ -203,8 +201,8 @@ static int mbm_validate_fc08_response(struct modbus_context *ctx,
|
|||
return err;
|
||||
}
|
||||
|
||||
static int mbm_validate_wr_response(struct modbus_context *ctx,
|
||||
const uint8_t node_addr,
|
||||
static int mbc_validate_wr_response(struct modbus_context *ctx,
|
||||
const uint8_t unit_id,
|
||||
uint8_t fc)
|
||||
{
|
||||
int err;
|
||||
|
@ -213,10 +211,10 @@ static int mbm_validate_wr_response(struct modbus_context *ctx,
|
|||
uint16_t resp_addr;
|
||||
uint16_t resp_value;
|
||||
|
||||
req_addr = sys_get_be16(&ctx->tx_frame.data[0]);
|
||||
req_value = sys_get_be16(&ctx->tx_frame.data[2]);
|
||||
resp_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
resp_value = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
req_addr = sys_get_be16(&ctx->tx_adu.data[0]);
|
||||
req_value = sys_get_be16(&ctx->tx_adu.data[2]);
|
||||
resp_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
resp_value = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
switch (fc) {
|
||||
case MODBUS_FC05_COIL_WR:
|
||||
|
@ -238,15 +236,15 @@ static int mbm_validate_wr_response(struct modbus_context *ctx,
|
|||
return err;
|
||||
}
|
||||
|
||||
static int mbm_send_cmd(struct modbus_context *ctx, const uint8_t node_addr,
|
||||
static int mbc_send_cmd(struct modbus_context *ctx, const uint8_t unit_id,
|
||||
uint8_t fc, void *data)
|
||||
{
|
||||
int err;
|
||||
|
||||
ctx->tx_frame.addr = node_addr;
|
||||
ctx->tx_frame.fc = fc;
|
||||
ctx->tx_adu.unit_id = unit_id;
|
||||
ctx->tx_adu.fc = fc;
|
||||
|
||||
mb_tx_frame(ctx);
|
||||
modbus_tx_adu(ctx);
|
||||
|
||||
if (k_sem_take(&ctx->client_wait_sem, K_USEC(ctx->rxwait_to)) != 0) {
|
||||
LOG_WRN("Client wait-for-RX timeout");
|
||||
|
@ -254,12 +252,12 @@ static int mbm_send_cmd(struct modbus_context *ctx, const uint8_t node_addr,
|
|||
goto exit_error;
|
||||
}
|
||||
|
||||
if (ctx->rx_frame_err != 0) {
|
||||
err = ctx->rx_frame_err;
|
||||
if (ctx->rx_adu_err != 0) {
|
||||
err = ctx->rx_adu_err;
|
||||
goto exit_error;
|
||||
}
|
||||
|
||||
err = mbm_validate_response_fc(ctx, node_addr, fc);
|
||||
err = mbc_validate_response_fc(ctx, unit_id, fc);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Failed to validate address or function code");
|
||||
goto exit_error;
|
||||
|
@ -273,18 +271,18 @@ static int mbm_send_cmd(struct modbus_context *ctx, const uint8_t node_addr,
|
|||
case MODBUS_FC02_DI_RD:
|
||||
case MODBUS_FC03_HOLDING_REG_RD:
|
||||
case MODBUS_FC04_IN_REG_RD:
|
||||
err = mbm_validate_rd_response(ctx, node_addr, fc, data);
|
||||
err = mbc_validate_rd_response(ctx, unit_id, fc, data);
|
||||
break;
|
||||
|
||||
case MODBUS_FC08_DIAGNOSTICS:
|
||||
err = mbm_validate_fc08_response(ctx, node_addr, data);
|
||||
err = mbc_validate_fc08_response(ctx, unit_id, data);
|
||||
break;
|
||||
|
||||
case MODBUS_FC05_COIL_WR:
|
||||
case MODBUS_FC06_HOLDING_REG_WR:
|
||||
case MODBUS_FC15_COILS_WR:
|
||||
case MODBUS_FC16_HOLDING_REGS_WR:
|
||||
err = mbm_validate_wr_response(ctx, node_addr, fc);
|
||||
err = mbc_validate_wr_response(ctx, unit_id, fc);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -297,12 +295,12 @@ exit_error:
|
|||
}
|
||||
|
||||
int modbus_read_coils(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const coil_tbl,
|
||||
const uint16_t num_coils)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -311,23 +309,23 @@ int modbus_read_coils(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_coils, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_coils, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC01_COIL_RD, coil_tbl);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC01_COIL_RD, coil_tbl);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_read_dinputs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const di_tbl,
|
||||
const uint16_t num_di)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -336,23 +334,23 @@ int modbus_read_dinputs(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_di, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_di, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC02_DI_RD, di_tbl);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC02_DI_RD, di_tbl);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_read_holding_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -361,11 +359,11 @@ int modbus_read_holding_regs(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
|
@ -374,12 +372,12 @@ int modbus_read_holding_regs(const int iface,
|
|||
|
||||
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
|
||||
int modbus_read_holding_regs_fp(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
float *const reg_buf,
|
||||
const uint16_t num_regs)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -388,11 +386,11 @@ int modbus_read_holding_regs_fp(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC03_HOLDING_REG_RD, reg_buf);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
|
@ -400,12 +398,12 @@ int modbus_read_holding_regs_fp(const int iface,
|
|||
#endif
|
||||
|
||||
int modbus_read_input_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -414,22 +412,22 @@ int modbus_read_input_regs(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, 4, reg_buf);
|
||||
err = mbc_send_cmd(ctx, unit_id, 4, reg_buf);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_write_coil(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t coil_addr,
|
||||
const bool coil_state)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
uint16_t coil_val;
|
||||
|
||||
|
@ -445,22 +443,22 @@ int modbus_write_coil(const int iface,
|
|||
coil_val = MODBUS_COIL_ON_CODE;
|
||||
}
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(coil_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(coil_val, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(coil_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(coil_val, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC05_COIL_WR, NULL);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC05_COIL_WR, NULL);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_write_holding_reg(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
const uint16_t reg_val)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -469,23 +467,23 @@ int modbus_write_holding_reg(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(reg_val, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(reg_val, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC06_HOLDING_REG_WR, NULL);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC06_HOLDING_REG_WR, NULL);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_request_diagnostic(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t sfunc,
|
||||
const uint16_t data,
|
||||
uint16_t *const data_out)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
int err;
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -494,23 +492,23 @@ int modbus_request_diagnostic(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
ctx->tx_frame.length = 4;
|
||||
sys_put_be16(sfunc, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(data, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = 4;
|
||||
sys_put_be16(sfunc, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(data, &ctx->tx_adu.data[2]);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC08_DIAGNOSTICS, data_out);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC08_DIAGNOSTICS, data_out);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_write_coils(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint8_t *const coil_tbl,
|
||||
const uint16_t num_coils)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
uint8_t num_bytes;
|
||||
int err;
|
||||
uint8_t *data_ptr;
|
||||
|
@ -521,29 +519,29 @@ int modbus_write_coils(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_coils, &ctx->tx_frame.data[2]);
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_coils, &ctx->tx_adu.data[2]);
|
||||
|
||||
num_bytes = (uint8_t)(((num_coils - 1) / 8) + 1);
|
||||
ctx->tx_frame.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_frame.data[5];
|
||||
ctx->tx_frame.length = 5 + num_bytes;
|
||||
ctx->tx_adu.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_adu.data[5];
|
||||
ctx->tx_adu.length = 5 + num_bytes;
|
||||
|
||||
memcpy(data_ptr, coil_tbl, num_bytes);
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC15_COILS_WR, NULL);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC15_COILS_WR, NULL);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int modbus_write_holding_regs(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
uint16_t *const reg_buf,
|
||||
const uint16_t num_regs)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
uint8_t num_bytes;
|
||||
int err;
|
||||
uint8_t *data_ptr;
|
||||
|
@ -554,20 +552,20 @@ int modbus_write_holding_regs(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_frame.data[2]);
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
|
||||
|
||||
num_bytes = (uint8_t) (num_regs * sizeof(uint16_t));
|
||||
ctx->tx_frame.length = num_bytes + 5;
|
||||
ctx->tx_frame.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_frame.data[5];
|
||||
ctx->tx_adu.length = num_bytes + 5;
|
||||
ctx->tx_adu.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_adu.data[5];
|
||||
|
||||
for (uint16_t i = 0; i < num_regs; i++) {
|
||||
sys_put_be16(reg_buf[i], data_ptr);
|
||||
data_ptr += sizeof(uint16_t);
|
||||
}
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC16_HOLDING_REGS_WR, NULL);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
|
@ -575,12 +573,12 @@ int modbus_write_holding_regs(const int iface,
|
|||
|
||||
#ifdef CONFIG_MODBUS_FP_EXTENSIONS
|
||||
int modbus_write_holding_regs_fp(const int iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint16_t start_addr,
|
||||
float *const reg_buf,
|
||||
const uint16_t num_regs)
|
||||
{
|
||||
struct modbus_context *ctx = mb_get_context(iface);
|
||||
struct modbus_context *ctx = modbus_get_context(iface);
|
||||
uint8_t num_bytes;
|
||||
int err;
|
||||
uint8_t *data_ptr;
|
||||
|
@ -591,13 +589,13 @@ int modbus_write_holding_regs_fp(const int iface,
|
|||
|
||||
k_mutex_lock(&ctx->iface_lock, K_FOREVER);
|
||||
|
||||
sys_put_be16(start_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_frame.data[2]);
|
||||
sys_put_be16(start_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(num_regs, &ctx->tx_adu.data[2]);
|
||||
|
||||
num_bytes = (uint8_t) (num_regs * sizeof(float));
|
||||
ctx->tx_frame.length = num_bytes + 5;
|
||||
ctx->tx_frame.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_frame.data[5];
|
||||
ctx->tx_adu.length = num_bytes + 5;
|
||||
ctx->tx_adu.data[4] = num_bytes;
|
||||
data_ptr = &ctx->tx_adu.data[5];
|
||||
|
||||
for (uint16_t i = 0; i < num_regs; i++) {
|
||||
uint32_t reg_val;
|
||||
|
@ -607,7 +605,7 @@ int modbus_write_holding_regs_fp(const int iface,
|
|||
data_ptr += sizeof(uint32_t);
|
||||
}
|
||||
|
||||
err = mbm_send_cmd(ctx, node_addr, MODBUS_FC16_HOLDING_REGS_WR, NULL);
|
||||
err = mbc_send_cmd(ctx, unit_id, MODBUS_FC16_HOLDING_REGS_WR, NULL);
|
||||
k_mutex_unlock(&ctx->iface_lock);
|
||||
|
||||
return err;
|
||||
|
|
|
@ -70,7 +70,7 @@ static void modbus_rx_handler(struct k_work *item)
|
|||
case MODBUS_MODE_ASCII:
|
||||
if (IS_ENABLED(CONFIG_MODBUS_SERIAL)) {
|
||||
modbus_serial_rx_disable(ctx);
|
||||
ctx->rx_frame_err = modbus_serial_rx_frame(ctx);
|
||||
ctx->rx_adu_err = modbus_serial_rx_adu(ctx);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
@ -84,7 +84,7 @@ static void modbus_rx_handler(struct k_work *item)
|
|||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_MODBUS_SERVER)) {
|
||||
bool respond = mbs_rx_handler(ctx);
|
||||
bool respond = modbus_server_handler(ctx);
|
||||
|
||||
switch (ctx->mode) {
|
||||
case MODBUS_MODE_RTU:
|
||||
|
@ -101,13 +101,13 @@ static void modbus_rx_handler(struct k_work *item)
|
|||
}
|
||||
}
|
||||
|
||||
void mb_tx_frame(struct modbus_context *ctx)
|
||||
void modbus_tx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
switch (ctx->mode) {
|
||||
case MODBUS_MODE_RTU:
|
||||
case MODBUS_MODE_ASCII:
|
||||
if (IS_ENABLED(CONFIG_MODBUS_SERIAL) &&
|
||||
modbus_serial_tx_frame(ctx)) {
|
||||
modbus_serial_tx_adu(ctx)) {
|
||||
LOG_ERR("Unsupported MODBUS serial mode");
|
||||
}
|
||||
break;
|
||||
|
@ -116,7 +116,7 @@ void mb_tx_frame(struct modbus_context *ctx)
|
|||
}
|
||||
}
|
||||
|
||||
struct modbus_context *mb_get_context(const uint8_t iface)
|
||||
struct modbus_context *modbus_get_context(const uint8_t iface)
|
||||
{
|
||||
struct modbus_context *ctx;
|
||||
|
||||
|
@ -136,7 +136,7 @@ struct modbus_context *mb_get_context(const uint8_t iface)
|
|||
}
|
||||
|
||||
static struct modbus_context *mb_cfg_iface(const uint8_t iface,
|
||||
const uint8_t node_addr,
|
||||
const uint8_t unit_id,
|
||||
const uint32_t baud,
|
||||
const enum uart_config_parity parity,
|
||||
const uint32_t rx_timeout,
|
||||
|
@ -165,7 +165,7 @@ static struct modbus_context *mb_cfg_iface(const uint8_t iface,
|
|||
}
|
||||
|
||||
ctx->rxwait_to = rx_timeout;
|
||||
ctx->node_addr = node_addr;
|
||||
ctx->unit_id = unit_id;
|
||||
ctx->client = client;
|
||||
ctx->mbs_user_cb = NULL;
|
||||
k_mutex_init(&ctx->iface_lock);
|
||||
|
@ -174,7 +174,7 @@ static struct modbus_context *mb_cfg_iface(const uint8_t iface,
|
|||
k_work_init(&ctx->server_work, modbus_rx_handler);
|
||||
|
||||
if (IS_ENABLED(CONFIG_MODBUS_FC08_DIAGNOSTIC)) {
|
||||
mbs_reset_statistics(ctx);
|
||||
modbus_reset_stats(ctx);
|
||||
}
|
||||
|
||||
switch (ctx->mode) {
|
||||
|
@ -196,7 +196,7 @@ static struct modbus_context *mb_cfg_iface(const uint8_t iface,
|
|||
return ctx;
|
||||
}
|
||||
|
||||
int modbus_init_server(const uint8_t iface, const uint8_t node_addr,
|
||||
int modbus_init_server(const uint8_t iface, const uint8_t unit_id,
|
||||
const uint32_t baud, const enum uart_config_parity parity,
|
||||
struct modbus_user_callbacks *const cb,
|
||||
const bool ascii_mode)
|
||||
|
@ -213,7 +213,7 @@ int modbus_init_server(const uint8_t iface, const uint8_t node_addr,
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
ctx = mb_cfg_iface(iface, node_addr, baud,
|
||||
ctx = mb_cfg_iface(iface, unit_id, baud,
|
||||
parity, 0, false, ascii_mode);
|
||||
|
||||
if (ctx == NULL) {
|
||||
|
@ -281,7 +281,7 @@ int modbus_disable(const uint8_t iface)
|
|||
}
|
||||
|
||||
ctx->rxwait_to = 0;
|
||||
ctx->node_addr = 0;
|
||||
ctx->unit_id = 0;
|
||||
ctx->mode = MODBUS_MODE_RTU;
|
||||
ctx->mbs_user_cb = NULL;
|
||||
atomic_clear_bit(&ctx->state, MODBUS_STATE_CONFIGURED);
|
||||
|
|
|
@ -72,9 +72,9 @@
|
|||
#define MODBUS_ASCII_END_FRAME_CHAR1 '\r'
|
||||
#define MODBUS_ASCII_END_FRAME_CHAR2 '\n'
|
||||
|
||||
struct mb_rtu_frame {
|
||||
struct modbus_adu {
|
||||
uint16_t length;
|
||||
uint8_t addr;
|
||||
uint8_t unit_id;
|
||||
uint8_t fc;
|
||||
uint8_t data[CONFIG_MODBUS_BUFFER_SIZE - 4];
|
||||
uint16_t crc;
|
||||
|
@ -138,12 +138,12 @@ struct modbus_context {
|
|||
/* Server work item */
|
||||
struct k_work server_work;
|
||||
/* Received frame */
|
||||
struct mb_rtu_frame rx_frame;
|
||||
struct modbus_adu rx_adu;
|
||||
/* Frame to transmit */
|
||||
struct mb_rtu_frame tx_frame;
|
||||
struct modbus_adu tx_adu;
|
||||
|
||||
/* Records error from frame reception, e.g. CRC error */
|
||||
int rx_frame_err;
|
||||
int rx_adu_err;
|
||||
|
||||
#ifdef CONFIG_MODBUS_FC08_DIAGNOSTIC
|
||||
uint16_t mbs_msg_ctr;
|
||||
|
@ -152,21 +152,21 @@ struct modbus_context {
|
|||
uint16_t mbs_server_msg_ctr;
|
||||
uint16_t mbs_noresp_ctr;
|
||||
#endif
|
||||
/* Node address */
|
||||
uint8_t node_addr;
|
||||
/* Unit ID */
|
||||
uint8_t unit_id;
|
||||
|
||||
};
|
||||
|
||||
struct modbus_context *mb_get_context(const uint8_t iface);
|
||||
void mb_tx_frame(struct modbus_context *ctx);
|
||||
struct modbus_context *modbus_get_context(const uint8_t iface);
|
||||
void modbus_tx_adu(struct modbus_context *ctx);
|
||||
|
||||
bool mbs_rx_handler(struct modbus_context *ctx);
|
||||
void mbs_reset_statistics(struct modbus_context *pch);
|
||||
bool modbus_server_handler(struct modbus_context *ctx);
|
||||
void modbus_reset_stats(struct modbus_context *ctx);
|
||||
|
||||
void modbus_serial_rx_disable(struct modbus_context *ctx);
|
||||
void modbus_serial_rx_enable(struct modbus_context *ctx);
|
||||
int modbus_serial_rx_frame(struct modbus_context *ctx);
|
||||
int modbus_serial_tx_frame(struct modbus_context *ctx);
|
||||
int modbus_serial_rx_adu(struct modbus_context *ctx);
|
||||
int modbus_serial_tx_adu(struct modbus_context *ctx);
|
||||
int modbus_serial_init(struct modbus_context *ctx,
|
||||
uint32_t baudrate,
|
||||
enum uart_config_parity parity,
|
||||
|
|
|
@ -95,7 +95,7 @@ static uint8_t modbus_ascii_get_lrc(uint8_t *src, size_t length)
|
|||
}
|
||||
|
||||
/* Parses and converts an ASCII mode frame into a Modbus RTU frame. */
|
||||
static int modbus_ascii_rx_frame(struct modbus_context *ctx)
|
||||
static int modbus_ascii_rx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
struct modbus_serial_config *cfg = ctx->cfg;
|
||||
uint8_t *pmsg;
|
||||
|
@ -105,7 +105,7 @@ static int modbus_ascii_rx_frame(struct modbus_context *ctx)
|
|||
uint8_t calc_lrc;
|
||||
|
||||
rx_size = cfg->uart_buf_ctr;
|
||||
prx_data = &ctx->rx_frame.data[0];
|
||||
prx_data = &ctx->rx_adu.data[0];
|
||||
|
||||
if (!(rx_size & 0x01)) {
|
||||
LOG_WRN("Message should have an odd number of bytes");
|
||||
|
@ -129,27 +129,27 @@ static int modbus_ascii_rx_frame(struct modbus_context *ctx)
|
|||
/* Point past the ':' to the address. */
|
||||
pmsg = &cfg->uart_buf[1];
|
||||
|
||||
hex2bin(pmsg, 2, &ctx->rx_frame.addr, 1);
|
||||
hex2bin(pmsg, 2, &ctx->rx_adu.unit_id, 1);
|
||||
pmsg += 2;
|
||||
rx_size -= 2;
|
||||
hex2bin(pmsg, 2, &ctx->rx_frame.fc, 1);
|
||||
hex2bin(pmsg, 2, &ctx->rx_adu.fc, 1);
|
||||
pmsg += 2;
|
||||
rx_size -= 2;
|
||||
|
||||
/* Get the data from the message */
|
||||
ctx->rx_frame.length = 0;
|
||||
ctx->rx_adu.length = 0;
|
||||
while (rx_size > 2) {
|
||||
hex2bin(pmsg, 2, prx_data, 1);
|
||||
prx_data++;
|
||||
pmsg += 2;
|
||||
rx_size -= 2;
|
||||
/* Increment the number of Modbus packets received */
|
||||
ctx->rx_frame.length++;
|
||||
ctx->rx_adu.length++;
|
||||
}
|
||||
|
||||
/* Extract the message's LRC */
|
||||
hex2bin(pmsg, 2, &frame_lrc, 1);
|
||||
ctx->rx_frame.crc = frame_lrc;
|
||||
ctx->rx_adu.crc = frame_lrc;
|
||||
|
||||
/*
|
||||
* The LRC is calculated on the ADDR, FC and Data fields,
|
||||
|
@ -181,7 +181,7 @@ static uint8_t *modbus_ascii_bin2hex(uint8_t value, uint8_t *pbuf)
|
|||
return pbuf;
|
||||
}
|
||||
|
||||
static void modbus_ascii_tx_frame(struct modbus_context *ctx)
|
||||
static void modbus_ascii_tx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
struct modbus_serial_config *cfg = ctx->cfg;
|
||||
uint16_t tx_bytes = 0;
|
||||
|
@ -193,13 +193,13 @@ static void modbus_ascii_tx_frame(struct modbus_context *ctx)
|
|||
tx_bytes = 1;
|
||||
|
||||
pbuf = &cfg->uart_buf[1];
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_frame.addr, pbuf);
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.unit_id, pbuf);
|
||||
tx_bytes += 2;
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_frame.fc, pbuf);
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.fc, pbuf);
|
||||
tx_bytes += 2;
|
||||
|
||||
for (int i = 0; i < ctx->tx_frame.length; i++) {
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_frame.data[i], pbuf);
|
||||
for (int i = 0; i < ctx->tx_adu.length; i++) {
|
||||
pbuf = modbus_ascii_bin2hex(ctx->tx_adu.data[i], pbuf);
|
||||
tx_bytes += 2;
|
||||
}
|
||||
|
||||
|
@ -228,12 +228,12 @@ static void modbus_ascii_tx_frame(struct modbus_context *ctx)
|
|||
modbus_serial_tx_on(ctx);
|
||||
}
|
||||
#else
|
||||
static int modbus_ascii_rx_frame(struct modbus_context *ctx)
|
||||
static int modbus_ascii_rx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void modbus_ascii_tx_frame(struct modbus_context *ctx)
|
||||
static void modbus_ascii_tx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
@ -270,7 +270,7 @@ static uint16_t modbus_rtu_crc16(uint8_t *src, size_t length)
|
|||
}
|
||||
|
||||
/* Copy Modbus RTU frame and check if the CRC is valid. */
|
||||
static int modbus_rtu_rx_frame(struct modbus_context *ctx)
|
||||
static int modbus_rtu_rx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
struct modbus_serial_config *cfg = ctx->cfg;
|
||||
uint16_t calc_crc;
|
||||
|
@ -284,22 +284,22 @@ static int modbus_rtu_rx_frame(struct modbus_context *ctx)
|
|||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
ctx->rx_frame.addr = cfg->uart_buf[0];
|
||||
ctx->rx_frame.fc = cfg->uart_buf[1];
|
||||
ctx->rx_adu.unit_id = cfg->uart_buf[0];
|
||||
ctx->rx_adu.fc = cfg->uart_buf[1];
|
||||
data_ptr = &cfg->uart_buf[2];
|
||||
/* Payload length without node address, function code, and CRC */
|
||||
ctx->rx_frame.length = cfg->uart_buf_ctr - 4;
|
||||
ctx->rx_adu.length = cfg->uart_buf_ctr - 4;
|
||||
/* CRC index */
|
||||
crc_idx = cfg->uart_buf_ctr - sizeof(uint16_t);
|
||||
|
||||
memcpy(ctx->rx_frame.data, data_ptr, ctx->rx_frame.length);
|
||||
memcpy(ctx->rx_adu.data, data_ptr, ctx->rx_adu.length);
|
||||
|
||||
ctx->rx_frame.crc = sys_get_le16(&cfg->uart_buf[crc_idx]);
|
||||
ctx->rx_adu.crc = sys_get_le16(&cfg->uart_buf[crc_idx]);
|
||||
/* Calculate CRC over address, function code, and payload */
|
||||
calc_crc = modbus_rtu_crc16(&cfg->uart_buf[0],
|
||||
cfg->uart_buf_ctr - sizeof(ctx->rx_frame.crc));
|
||||
cfg->uart_buf_ctr - sizeof(ctx->rx_adu.crc));
|
||||
|
||||
if (ctx->rx_frame.crc != calc_crc) {
|
||||
if (ctx->rx_adu.crc != calc_crc) {
|
||||
LOG_WRN("Calculated CRC does not match received CRC");
|
||||
return -EIO;
|
||||
}
|
||||
|
@ -307,23 +307,23 @@ static int modbus_rtu_rx_frame(struct modbus_context *ctx)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void rtu_tx_frame(struct modbus_context *ctx)
|
||||
static void rtu_tx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
struct modbus_serial_config *cfg = ctx->cfg;
|
||||
uint16_t tx_bytes = 0;
|
||||
uint8_t *data_ptr;
|
||||
|
||||
cfg->uart_buf[0] = ctx->tx_frame.addr;
|
||||
cfg->uart_buf[1] = ctx->tx_frame.fc;
|
||||
tx_bytes = 2 + ctx->tx_frame.length;
|
||||
cfg->uart_buf[0] = ctx->tx_adu.unit_id;
|
||||
cfg->uart_buf[1] = ctx->tx_adu.fc;
|
||||
tx_bytes = 2 + ctx->tx_adu.length;
|
||||
data_ptr = &cfg->uart_buf[2];
|
||||
|
||||
memcpy(data_ptr, ctx->tx_frame.data, ctx->tx_frame.length);
|
||||
memcpy(data_ptr, ctx->tx_adu.data, ctx->tx_adu.length);
|
||||
|
||||
ctx->tx_frame.crc = modbus_rtu_crc16(&cfg->uart_buf[0],
|
||||
ctx->tx_frame.length + 2);
|
||||
sys_put_le16(ctx->tx_frame.crc,
|
||||
&cfg->uart_buf[ctx->tx_frame.length + 2]);
|
||||
ctx->tx_adu.crc = modbus_rtu_crc16(&cfg->uart_buf[0],
|
||||
ctx->tx_adu.length + 2);
|
||||
sys_put_le16(ctx->tx_adu.crc,
|
||||
&cfg->uart_buf[ctx->tx_adu.length + 2]);
|
||||
tx_bytes += 2;
|
||||
|
||||
cfg->uart_buf_ctr = tx_bytes;
|
||||
|
@ -480,21 +480,21 @@ void modbus_serial_rx_enable(struct modbus_context *ctx)
|
|||
modbus_serial_rx_on(ctx);
|
||||
}
|
||||
|
||||
int modbus_serial_rx_frame(struct modbus_context *ctx)
|
||||
int modbus_serial_rx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
struct modbus_serial_config *cfg = ctx->cfg;
|
||||
int rc = 0;
|
||||
|
||||
switch (ctx->mode) {
|
||||
case MODBUS_MODE_RTU:
|
||||
rc = modbus_rtu_rx_frame(ctx);
|
||||
rc = modbus_rtu_rx_adu(ctx);
|
||||
break;
|
||||
case MODBUS_MODE_ASCII:
|
||||
if (!IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
rc = modbus_ascii_rx_frame(ctx);
|
||||
rc = modbus_ascii_rx_adu(ctx);
|
||||
break;
|
||||
default:
|
||||
LOG_ERR("Unsupported MODBUS mode");
|
||||
|
@ -507,15 +507,15 @@ int modbus_serial_rx_frame(struct modbus_context *ctx)
|
|||
return rc;
|
||||
}
|
||||
|
||||
int modbus_serial_tx_frame(struct modbus_context *ctx)
|
||||
int modbus_serial_tx_adu(struct modbus_context *ctx)
|
||||
{
|
||||
switch (ctx->mode) {
|
||||
case MODBUS_MODE_RTU:
|
||||
rtu_tx_frame(ctx);
|
||||
rtu_tx_adu(ctx);
|
||||
return 0;
|
||||
case MODBUS_MODE_ASCII:
|
||||
if (IS_ENABLED(CONFIG_MODBUS_ASCII_MODE)) {
|
||||
modbus_ascii_tx_frame(ctx);
|
||||
modbus_ascii_tx_adu(ctx);
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
|
|
|
@ -31,7 +31,7 @@ LOG_MODULE_REGISTER(modbus_s, CONFIG_MODBUS_LOG_LEVEL);
|
|||
* statistics and communications counters.
|
||||
*/
|
||||
#ifdef CONFIG_MODBUS_FC08_DIAGNOSTIC
|
||||
void mbs_reset_statistics(struct modbus_context *ctx)
|
||||
void modbus_reset_stats(struct modbus_context *ctx)
|
||||
{
|
||||
/* Initialize all MODBUS event counters. */
|
||||
ctx->mbs_msg_ctr = 0;
|
||||
|
@ -66,7 +66,7 @@ static void update_noresp_ctr(struct modbus_context *ctx)
|
|||
ctx->mbs_noresp_ctr++;
|
||||
}
|
||||
#else
|
||||
#define mbs_reset_statistics(...)
|
||||
#define modbus_reset_stats(...)
|
||||
#define update_msg_ctr(...)
|
||||
#define update_crcerr_ctr(...)
|
||||
#define update_excep_ctr(...)
|
||||
|
@ -82,13 +82,13 @@ static void mbs_exception_rsp(struct modbus_context *ctx, uint8_t excep_code)
|
|||
{
|
||||
const uint8_t excep_bit = BIT(7);
|
||||
|
||||
LOG_INF("FC 0x%02x Error 0x%02x", ctx->rx_frame.fc, excep_code);
|
||||
LOG_INF("FC 0x%02x Error 0x%02x", ctx->rx_adu.fc, excep_code);
|
||||
|
||||
update_excep_ctr(ctx);
|
||||
|
||||
ctx->tx_frame.fc |= excep_bit;
|
||||
ctx->tx_frame.data[0] = excep_code;
|
||||
ctx->tx_frame.length = 1;
|
||||
ctx->tx_adu.fc |= excep_bit;
|
||||
ctx->tx_adu.data[0] = excep_code;
|
||||
ctx->tx_adu.length = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -117,7 +117,7 @@ static bool mbs_fc01_coil_read(struct modbus_context *ctx)
|
|||
uint8_t bit_mask;
|
||||
uint16_t coil_cntr;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length");
|
||||
return false;
|
||||
}
|
||||
|
@ -127,8 +127,8 @@ static bool mbs_fc01_coil_read(struct modbus_context *ctx)
|
|||
return true;
|
||||
}
|
||||
|
||||
coil_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
coil_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
coil_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
coil_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
/* Make sure we don't exceed the allowed limit per request */
|
||||
if (coil_qty == 0 || coil_qty > coils_limit) {
|
||||
|
@ -140,16 +140,16 @@ static bool mbs_fc01_coil_read(struct modbus_context *ctx)
|
|||
/* Calculate byte count for response. */
|
||||
num_bytes = ((coil_qty - 1) / 8) + 1;
|
||||
/* Number of data bytes + byte count. */
|
||||
ctx->tx_frame.length = num_bytes + 1;
|
||||
ctx->tx_adu.length = num_bytes + 1;
|
||||
/* Set number of data bytes in response message. */
|
||||
ctx->tx_frame.data[0] = (uint8_t)num_bytes;
|
||||
ctx->tx_adu.data[0] = (uint8_t)num_bytes;
|
||||
|
||||
/* Clear bytes in response */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
memset(presp, 0, num_bytes);
|
||||
|
||||
/* Reset the pointer to the start of the response payload */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
/* Start with bit 0 in response byte data mask. */
|
||||
bit_mask = BIT(0);
|
||||
/* Initialize loop counter. */
|
||||
|
@ -216,7 +216,7 @@ static bool mbs_fc02_di_read(struct modbus_context *ctx)
|
|||
uint8_t bit_mask;
|
||||
uint16_t di_cntr;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length");
|
||||
return false;
|
||||
}
|
||||
|
@ -226,8 +226,8 @@ static bool mbs_fc02_di_read(struct modbus_context *ctx)
|
|||
return true;
|
||||
}
|
||||
|
||||
di_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
di_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
di_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
di_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
/* Make sure we don't exceed the allowed limit per request */
|
||||
if (di_qty == 0 || di_qty > di_limit) {
|
||||
|
@ -239,18 +239,18 @@ static bool mbs_fc02_di_read(struct modbus_context *ctx)
|
|||
/* Get number of bytes needed for response. */
|
||||
num_bytes = ((di_qty - 1) / 8) + 1;
|
||||
/* Number of data bytes + byte count. */
|
||||
ctx->tx_frame.length = num_bytes + 1;
|
||||
ctx->tx_adu.length = num_bytes + 1;
|
||||
/* Set number of data bytes in response message. */
|
||||
ctx->tx_frame.data[0] = (uint8_t)num_bytes;
|
||||
ctx->tx_adu.data[0] = (uint8_t)num_bytes;
|
||||
|
||||
/* Clear bytes in response */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
for (di_cntr = 0; di_cntr < num_bytes; di_cntr++) {
|
||||
*presp++ = 0x00;
|
||||
}
|
||||
|
||||
/* Reset the pointer to the start of the response payload */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
/* Start with bit 0 in response byte data mask. */
|
||||
bit_mask = BIT(0);
|
||||
/* Initialize loop counter. */
|
||||
|
@ -314,13 +314,13 @@ static bool mbs_fc03_hreg_read(struct modbus_context *ctx)
|
|||
uint16_t reg_qty;
|
||||
uint16_t num_bytes;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length");
|
||||
return false;
|
||||
}
|
||||
|
||||
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
reg_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
|
||||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
|
||||
|
@ -356,12 +356,12 @@ static bool mbs_fc03_hreg_read(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Number of data bytes + byte count. */
|
||||
ctx->tx_frame.length = num_bytes + 1;
|
||||
ctx->tx_adu.length = num_bytes + 1;
|
||||
/* Set number of data bytes in response message. */
|
||||
ctx->tx_frame.data[0] = (uint8_t)num_bytes;
|
||||
ctx->tx_adu.data[0] = (uint8_t)num_bytes;
|
||||
|
||||
/* Reset the pointer to the start of the response payload */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
/* Loop through each register requested. */
|
||||
while (reg_qty > 0) {
|
||||
if (reg_addr < MODBUS_FP_EXTENSIONS_ADDR) {
|
||||
|
@ -424,13 +424,13 @@ static bool mbs_fc04_inreg_read(struct modbus_context *ctx)
|
|||
uint16_t reg_qty;
|
||||
uint16_t num_bytes;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length");
|
||||
return false;
|
||||
}
|
||||
|
||||
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
reg_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
|
||||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
|
||||
|
@ -466,12 +466,12 @@ static bool mbs_fc04_inreg_read(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Number of data bytes + byte count. */
|
||||
ctx->tx_frame.length = num_bytes + 1;
|
||||
ctx->tx_adu.length = num_bytes + 1;
|
||||
/* Set number of data bytes in response message. */
|
||||
ctx->tx_frame.data[0] = (uint8_t)num_bytes;
|
||||
ctx->tx_adu.data[0] = (uint8_t)num_bytes;
|
||||
|
||||
/* Reset the pointer to the start of the response payload */
|
||||
presp = &ctx->tx_frame.data[1];
|
||||
presp = &ctx->tx_adu.data[1];
|
||||
/* Loop through each register requested. */
|
||||
while (reg_qty > 0) {
|
||||
if (reg_addr < MODBUS_FP_EXTENSIONS_ADDR) {
|
||||
|
@ -533,8 +533,8 @@ static bool mbs_fc05_coil_write(struct modbus_context *ctx)
|
|||
uint16_t coil_val;
|
||||
bool coil_state;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_frame.length);
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_adu.length);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -544,8 +544,8 @@ static bool mbs_fc05_coil_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Get the desired coil address and coil value */
|
||||
coil_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
coil_val = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
coil_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
coil_val = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
/* See if coil needs to be OFF? */
|
||||
if (coil_val == MODBUS_COIL_OFF_CODE) {
|
||||
|
@ -563,9 +563,9 @@ static bool mbs_fc05_coil_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Assemble response payload */
|
||||
ctx->tx_frame.length = response_len;
|
||||
sys_put_be16(coil_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(coil_val, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = response_len;
|
||||
sys_put_be16(coil_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(coil_val, &ctx->tx_adu.data[2]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -591,8 +591,8 @@ static bool mbs_fc06_hreg_write(struct modbus_context *ctx)
|
|||
uint16_t reg_addr;
|
||||
uint16_t reg_val;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_frame.length);
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_adu.length);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -601,8 +601,8 @@ static bool mbs_fc06_hreg_write(struct modbus_context *ctx)
|
|||
return true;
|
||||
}
|
||||
|
||||
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
reg_val = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
reg_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
reg_val = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
err = ctx->mbs_user_cb->holding_reg_wr(reg_addr, reg_val);
|
||||
|
||||
|
@ -613,9 +613,9 @@ static bool mbs_fc06_hreg_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Assemble response payload */
|
||||
ctx->tx_frame.length = response_len;
|
||||
sys_put_be16(reg_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(reg_val, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = response_len;
|
||||
sys_put_be16(reg_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(reg_val, &ctx->tx_adu.data[2]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -641,13 +641,13 @@ static bool mbs_fc08_diagnostics(struct modbus_context *ctx)
|
|||
uint16_t sfunc;
|
||||
uint16_t data;
|
||||
|
||||
if (ctx->rx_frame.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_frame.length);
|
||||
if (ctx->rx_adu.length != request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_adu.length);
|
||||
return false;
|
||||
}
|
||||
|
||||
sfunc = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
data = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
sfunc = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
data = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
|
||||
switch (sfunc) {
|
||||
case MODBUS_FC08_SUBF_QUERY:
|
||||
|
@ -656,7 +656,7 @@ static bool mbs_fc08_diagnostics(struct modbus_context *ctx)
|
|||
|
||||
case MODBUS_FC08_SUBF_CLR_CTR:
|
||||
/* Sub-function 0x0A clear Counters and Diagnostic */
|
||||
mbs_reset_statistics(ctx);
|
||||
modbus_reset_stats(ctx);
|
||||
break;
|
||||
|
||||
case MODBUS_FC08_SUBF_BUS_MSG_CTR:
|
||||
|
@ -691,9 +691,9 @@ static bool mbs_fc08_diagnostics(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Assemble response payload */
|
||||
ctx->tx_frame.length = response_len;
|
||||
sys_put_be16(sfunc, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(data, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = response_len;
|
||||
sys_put_be16(sfunc, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(data, &ctx->tx_adu.data[2]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -735,8 +735,8 @@ static bool mbs_fc15_coils_write(struct modbus_context *ctx)
|
|||
uint8_t data_ix;
|
||||
bool coil_state;
|
||||
|
||||
if (ctx->rx_frame.length < request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_frame.length);
|
||||
if (ctx->rx_adu.length < request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_adu.length);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -745,10 +745,10 @@ static bool mbs_fc15_coils_write(struct modbus_context *ctx)
|
|||
return true;
|
||||
}
|
||||
|
||||
coil_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
coil_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
coil_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
coil_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
/* Get the byte count for the data. */
|
||||
num_bytes = ctx->rx_frame.data[4];
|
||||
num_bytes = ctx->rx_adu.data[4];
|
||||
|
||||
if (coil_qty == 0 || coil_qty > coils_limit) {
|
||||
LOG_ERR("Number of coils limit exceeded");
|
||||
|
@ -758,7 +758,7 @@ static bool mbs_fc15_coils_write(struct modbus_context *ctx)
|
|||
|
||||
/* Be sure byte count is valid for quantity of coils. */
|
||||
if (((((coil_qty - 1) / 8) + 1) != num_bytes) ||
|
||||
(ctx->rx_frame.length != (num_bytes + 5))) {
|
||||
(ctx->rx_adu.length != (num_bytes + 5))) {
|
||||
LOG_ERR("Mismatch in the number of coils");
|
||||
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_DATA_VAL);
|
||||
return true;
|
||||
|
@ -771,7 +771,7 @@ static bool mbs_fc15_coils_write(struct modbus_context *ctx)
|
|||
while (coil_cntr < coil_qty) {
|
||||
/* Move to the next data byte after every eight bits. */
|
||||
if ((coil_cntr % 8) == 0) {
|
||||
temp = ctx->rx_frame.data[data_ix++];
|
||||
temp = ctx->rx_adu.data[data_ix++];
|
||||
}
|
||||
|
||||
if (temp & BIT(0)) {
|
||||
|
@ -796,9 +796,9 @@ static bool mbs_fc15_coils_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Assemble response payload */
|
||||
ctx->tx_frame.length = response_len;
|
||||
sys_put_be16(coil_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(coil_qty, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = response_len;
|
||||
sys_put_be16(coil_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(coil_qty, &ctx->tx_adu.data[2]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -835,15 +835,15 @@ static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
|
|||
uint16_t num_bytes;
|
||||
uint8_t reg_size;
|
||||
|
||||
if (ctx->rx_frame.length < request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_frame.length);
|
||||
if (ctx->rx_adu.length < request_len) {
|
||||
LOG_ERR("Wrong request length %u", ctx->rx_adu.length);
|
||||
return false;
|
||||
}
|
||||
|
||||
reg_addr = sys_get_be16(&ctx->rx_frame.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_frame.data[2]);
|
||||
reg_addr = sys_get_be16(&ctx->rx_adu.data[0]);
|
||||
reg_qty = sys_get_be16(&ctx->rx_adu.data[2]);
|
||||
/* Get the byte count for the data. */
|
||||
num_bytes = ctx->rx_frame.data[4];
|
||||
num_bytes = ctx->rx_adu.data[4];
|
||||
|
||||
if ((reg_addr < MODBUS_FP_EXTENSIONS_ADDR) ||
|
||||
!IS_ENABLED(CONFIG_MODBUS_FP_EXTENSIONS)) {
|
||||
|
@ -877,7 +877,7 @@ static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Compare number of bytes and payload length */
|
||||
if ((ctx->rx_frame.length - 5) != num_bytes) {
|
||||
if ((ctx->rx_adu.length - 5) != num_bytes) {
|
||||
LOG_ERR("Mismatch in the number of bytes");
|
||||
mbs_exception_rsp(ctx, MODBUS_EXC_ILLEGAL_DATA_VAL);
|
||||
return true;
|
||||
|
@ -890,7 +890,7 @@ static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* The 1st registers data byte is 6th element in payload */
|
||||
prx_data = &ctx->rx_frame.data[5];
|
||||
prx_data = &ctx->rx_adu.data[5];
|
||||
|
||||
for (uint16_t reg_cntr = 0; reg_cntr < reg_qty; reg_cntr++) {
|
||||
uint16_t addr = reg_addr + reg_cntr;
|
||||
|
@ -919,9 +919,9 @@ static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
|
|||
}
|
||||
|
||||
/* Assemble response payload */
|
||||
ctx->tx_frame.length = response_len;
|
||||
sys_put_be16(reg_addr, &ctx->tx_frame.data[0]);
|
||||
sys_put_be16(reg_qty, &ctx->tx_frame.data[2]);
|
||||
ctx->tx_adu.length = response_len;
|
||||
sys_put_be16(reg_addr, &ctx->tx_adu.data[0]);
|
||||
sys_put_be16(reg_qty, &ctx->tx_adu.data[2]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -929,16 +929,16 @@ static bool mbs_fc16_hregs_write(struct modbus_context *ctx)
|
|||
static bool mbs_fc_handler(struct modbus_context *ctx)
|
||||
{
|
||||
bool send_reply = false;
|
||||
uint8_t addr = ctx->rx_frame.addr;
|
||||
uint8_t fc = ctx->rx_frame.fc;
|
||||
uint8_t addr = ctx->rx_adu.unit_id;
|
||||
uint8_t fc = ctx->rx_adu.fc;
|
||||
|
||||
if (addr != 0 && addr != ctx->node_addr) {
|
||||
if (addr != 0 && addr != ctx->unit_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Prepare response header */
|
||||
ctx->tx_frame.addr = addr;
|
||||
ctx->tx_frame.fc = fc;
|
||||
ctx->tx_adu.unit_id = addr;
|
||||
ctx->tx_adu.fc = fc;
|
||||
|
||||
update_server_msg_ctr(ctx);
|
||||
|
||||
|
@ -994,20 +994,20 @@ static bool mbs_fc_handler(struct modbus_context *ctx)
|
|||
}
|
||||
}
|
||||
|
||||
bool mbs_rx_handler(struct modbus_context *ctx)
|
||||
bool modbus_server_handler(struct modbus_context *ctx)
|
||||
{
|
||||
LOG_DBG("Server RX handler %p", ctx);
|
||||
|
||||
update_msg_ctr(ctx);
|
||||
|
||||
if (ctx->rx_frame_err == -EIO) {
|
||||
if (ctx->rx_adu_err == -EIO) {
|
||||
update_noresp_ctr(ctx);
|
||||
update_crcerr_ctr(ctx);
|
||||
} else if (ctx->rx_frame_err != 0) {
|
||||
} else if (ctx->rx_adu_err != 0) {
|
||||
update_noresp_ctr(ctx);
|
||||
} else {
|
||||
if (mbs_fc_handler(ctx) == true) {
|
||||
mb_tx_frame(ctx);
|
||||
modbus_tx_adu(ctx);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue