Skip to content

Commit

Permalink
Added function "modbus_read_device_information_object" to access device
Browse files Browse the repository at this point in the history
specific information like "vendor name" or "product name".

Definitions for device information objects are added to modbus.h.

Signed-off-by: Oliver Schwaneberg <oliver.schwaneberg@gmail.com>
  • Loading branch information
Schwaneberg committed Jul 31, 2018
1 parent ddac0cf commit 4c4abd6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static unsigned int compute_response_length_from_request(modbus_t *ctx, uint8_t
length = 3;
break;
case MODBUS_FC_REPORT_SLAVE_ID:
case MODBUS_FC_READ_DEVICE_INFORMATION:
/* The response is device specific (the header provides the
length) */
return MSG_LENGTH_UNDEFINED;
Expand Down Expand Up @@ -280,6 +281,9 @@ static uint8_t compute_meta_length_after_function(int function,
case MODBUS_FC_MASK_WRITE_REGISTER:
length = 6;
break;
case MODBUS_FC_READ_DEVICE_INFORMATION:
length = 8;
break;
default:
length = 1;
}
Expand Down Expand Up @@ -313,6 +317,8 @@ static int compute_data_length_after_meta(modbus_t *ctx, uint8_t *msg,
function == MODBUS_FC_REPORT_SLAVE_ID ||
function == MODBUS_FC_WRITE_AND_READ_REGISTERS) {
length = msg[ctx->backend->header_length + 1];
} else if (function == MODBUS_FC_READ_DEVICE_INFORMATION) {
length = msg[ctx->backend->header_length + 8];
} else {
length = 0;
}
Expand Down Expand Up @@ -600,6 +606,9 @@ static int check_confirmation(modbus_t *ctx, uint8_t *req,
/* Report slave ID (bytes received) */
req_nb_value = rsp_nb_value = rsp[offset + 1];
break;
case MODBUS_FC_READ_DEVICE_INFORMATION:
req_nb_value = rsp_nb_value = rsp[offset + 8];
break;
default:
/* 1 Write functions & others */
req_nb_value = rsp_nb_value = 1;
Expand Down Expand Up @@ -1399,6 +1408,35 @@ int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *src)
return rc;
}

int modbus_read_device_information_object(modbus_t *ctx, unsigned char object_id, char* dest)
{
uint8_t req[_MIN_REQ_LENGTH];
uint8_t rsp[MAX_MESSAGE_LENGTH];
int rc;
int req_length;
int offset;
int i;
req_length = ctx->backend->build_request_basis(ctx, MODBUS_FC_READ_DEVICE_INFORMATION, 0x0E04, (object_id<<8), req) - 1;
rc = send_msg(ctx, req, req_length);
if (rc > 0) {
rc = _modbus_receive_msg(ctx, rsp, MSG_CONFIRMATION);
if (rc == -1)
return -1;

rc = check_confirmation(ctx, req, rsp, rc);
if (rc == -1)
return -1;

offset = ctx->backend->header_length + 9;

for (i = 0; i < rc; i++) {
dest[i] = rsp[offset + i];
}
dest[i] = '\0';
}
return rc;
}

int modbus_mask_write_register(modbus_t *ctx, int addr, uint16_t and_mask, uint16_t or_mask)
{
int rc;
Expand Down
11 changes: 11 additions & 0 deletions src/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,19 @@ MODBUS_BEGIN_DECLS
#define MODBUS_FC_REPORT_SLAVE_ID 0x11
#define MODBUS_FC_MASK_WRITE_REGISTER 0x16
#define MODBUS_FC_WRITE_AND_READ_REGISTERS 0x17
#define MODBUS_FC_READ_DEVICE_INFORMATION 0x2B

#define MODBUS_BROADCAST_ADDRESS 0

/* Modbus device information object codes */
#define MODBUS_OBJID_VENDORNAME 0x00
#define MODBUS_OBJID_PRODUCTCODE 0x01
#define MODBUS_OBJID_MAJOR_MINOR_REVISION 0x02
#define MODBUS_OBJID_VENDOR_URL 0x03
#define MODBUS_OBJID_PRODUCTNAME 0x04
#define MODBUS_OBJID_MODELNAME 0x05
#define MODBUS_OBJID_USERAPPLICATION 0x06

/* Modbus_Application_Protocol_V1_1b.pdf (chapter 6 section 1 page 12)
* Quantity of Coils to read (2 bytes): 1 to 2000 (0x7D0)
* (chapter 6 section 11 page 29)
Expand Down Expand Up @@ -203,6 +213,7 @@ MODBUS_API int modbus_set_debug(modbus_t *ctx, int flag);

MODBUS_API const char *modbus_strerror(int errnum);

MODBUS_API int modbus_read_device_information_object(modbus_t *ctx, unsigned char object_id, char* dest);
MODBUS_API int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
MODBUS_API int modbus_read_input_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest);
MODBUS_API int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest);
Expand Down

0 comments on commit 4c4abd6

Please sign in to comment.