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
63 changes: 63 additions & 0 deletions doc/modbus_mapping_new_extend.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
modbus_mapping_new_extend(3)
=====================


NAME
----
modbus_mapping_new_extend - allocate four arrays of bits and registers and file registers


SYNOPSIS
--------
*modbus_mapping_t* modbus_mapping_new_extend(int 'nb_bits', int 'nb_input_bits', int 'nb_registers', int 'nb_input_registers', uint16_t nb_file_register[MODBUS_MAX_REFERENCE_FILES]);*


DESCRIPTION
-----------
The *modbus_mapping_new_extend()* calls linkmb:modbus_mapping_new[3] and additionally allocates up to
MODBUS_MAX_REFERENCE_FILES (current specification limit it to 10) arrays for file registers.

Each _value_ in the array, which is not 0 allocates _value_ registers in the file of _index_ -1 .
( The file registers start with "1" for the first file )

RETURN VALUE
------------
The function shall return the new allocated structure if successful. Otherwise
it shall return NULL and set errno.


ERRORS
------
*ENOMEM*::
Not enough memory


EXAMPLE
-------
[source,c]
-------------------
/* The first value of each array is accessible from the 0 address. */
mb_mapping = modbus_mapping_new_extend(BITS_ADDRESS + BITS_NB,
INPUT_BITS_ADDRESS + INPUT_BITS_NB,
REGISTERS_ADDRESS + REGISTERS_NB,
INPUT_REGISTERS_ADDRESS + INPUT_REGISTERS_NB,
FILE_REGISTERS_NB[MODBUS_MAX_REFERENCE_FILES]);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
-------------------

SEE ALSO
--------
linkmb:modbus_mapping_new[3]
linkmb:modbus_mapping_free[3]
linkmb:modbus_mapping_new_start_address[3]


AUTHORS
-------
The libmodbus documentation was written by Stéphane Raimbault
<stephane.raimbault@gmail.com>
81 changes: 81 additions & 0 deletions doc/modbus_mapping_new_start_address_extend.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
modbus_mapping_new_start_address_extend(3)
===================================


NAME
----
modbus_mapping_new_start_address_extend - allocate four arrays of bits and registers accessible from their starting addresses and file registers


SYNOPSIS
--------
*modbus_mapping_t* modbus_mapping_new_start_address(int 'start_bits', int 'nb_bits',
int 'start_input_bits', int 'nb_input_bits',
int 'start_registers', int 'nb_registers',
int 'start_input_registers', int 'nb_input_registers',
'uint16_t nb_file_register[MODBUS_MAX_REFERENCE_FILES]');*


DESCRIPTION
-----------
The *modbus_mapping_new_extend()* calls linkmb:modbus_mapping_new_start_address[3] and additionally allocates up to
MODBUS_MAX_REFERENCE_FILES (current specification limit it to 10) arrays for file registers.

Each _value_ in the array, which is not 0 allocates _value_ registers in the file of _index_ -1 .
( The file registers start with "1" for the first file )

[source,c]
-------------------
mb_mapping = modbus_mapping_new_start_address_extend(0, 0, 0, 0, 10000, 10, 0, 0,{0,100,0,20,0,0,10000,0,0,0,0});
-------------------

With this code, 3 file registers ( 2, 4 and 7 ) with the size of 100 , 20 and 10000 registers are allocated.

If it isn't necessary to allocate an array for a specific type of data, you can
pass the zero value in argument, the associated pointer will be NULL.

This function is convenient to handle requests in a Modbus server/slave.


RETURN VALUE
------------
The _modbus_mapping_new_start_address_extend()_ function shall return the new allocated structure if
successful. Otherwise it shall return NULL and set errno.


ERRORS
------
ENOMEM::
Not enough memory


EXAMPLE
-------
[source,c]
-------------------
/* The first value of each array is accessible at the defined address.
The end address is ADDRESS + NB - 1. */
mb_mapping = modbus_mapping_new_start_address_extend(BITS_ADDRESS, BITS_NB,
INPUT_BITS_ADDRESS, INPUT_BITS_NB,
REGISTERS_ADDRESS, REGISTERS_NB,
INPUT_REGISTERS_ADDRESS, INPUT_REGISTERS_NB,
FILE_REGISTERS_NB[MODBUS_MAX_REFERENCE_FILES]);
if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
-------------------

SEE ALSO
--------
linkmb:modbus_mapping_new_start_address[3]
linkmb:modbus_mapping_new[3]
linkmb:modbus_mapping_free[3]


AUTHORS
-------
The libmodbus documentation was written by Stéphane Raimbault
<stephane.raimbault@gmail.com>
84 changes: 84 additions & 0 deletions doc/modbus_read_general_reference.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
modbus_read_general_reference(3)
========================


NAME
----
modbus_read_general_reference - read from file registers


SYNOPSIS
--------
*int modbus_read_general_reference(modbus_t *ctx, int file_no, int read_addr, int read_nb, uint16_t *dest);*


DESCRIPTION
-----------
The *modbus_read_general_reference()* function shall read the number of _read_nb_ registers of the _file_no_
reference file at the address _read_addr_ of the remote device. The result of
reading is stored in _dest_ array as word values (16 bits).

_file_no_ has to be >= 1 and <= 10 .

You must take care to allocate enough memory to store the results in _dest_
(at least (_read_nb_ + 1 ) * sizeof(uint16_t)).

The function uses the Modbus function code 0x14 (read file record).


RETURN VALUE
------------
The function shall return the number of read registers + 1
if successful. Otherwise it shall return -1 and set errno.


ERRORS
------
*EMBMDATA*::
Too many registers requested
*EMBXILVAL*::
_file_no_ out of range , or file is not found.


EXAMPLE
-------
[source,c]
-------------------
modbus_t *ctx;
uint16_t tab_reg[64+1]; // one additional reg for filesize and reftype
int rc;
int i;

ctx = modbus_new_tcp("127.0.0.1", 1502);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}

rc = modbus_read_general_reference(ctx, 1, 10 , 64, tab_reg );
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}

printf("size=%d subtype_ref=%d\n", i, tab_reg[0]>>8, tab_reg[0] &0xff);
for (i=1; i < rc; i++) {
printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
}

modbus_close(ctx);
modbus_free(ctx);
-------------------


SEE ALSO
--------
linkmb:modbus_write_general_reference[3]
linkmb:modbus_mapping_new_start_address_extend[3]
linkmb:modbus_mapping_new_extend[3]

AUTHORS
-------
The libmodbus documentation was written by Stéphane Raimbault
<stephane.raimbault@gmail.com>
80 changes: 80 additions & 0 deletions doc/modbus_write_general_reference.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
modbus_write_general_reference(3)
========================


NAME
----
modbus_write_general_reference - write to file registers


SYNOPSIS
--------
*int modbus_write_general_reference(modbus_t *ctx, int file_no, int write_addr, int write_nb, const uint16_t *src);*


DESCRIPTION
-----------
The *modbus_read_general_reference()* function shall write the number of _write_nb_ registers of the _file_no_
reference file at the address _write_addr_ of the remote device. The content for
writing is delivered in _src_ array as word values (16 bits).

_file_no_ has to be >= 1 and <= 10 .


The function uses the Modbus function code 0x15 (write file record).


RETURN VALUE
------------
The function shall return the number of written registers + 1
if successful. Otherwise it shall return -1 and set errno.


ERRORS
------
*EMBMDATA*::
Too many registers requested
*EMBXILVAL*::
_file_no_ out of range , or file is not found.


EXAMPLE
-------
[source,c]
-------------------
modbus_t *ctx;
uint16_t tab_reg[64];
int rc;
int i;

ctx = modbus_new_tcp("127.0.0.1", 1502);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}

for (i=0; i < 64; i++){
tab_reg[i]=i+i*0x100;
}
rc = modbus_write_general_reference(ctx, 1, 10 , 64, tab_reg );
if (rc == -1) {
fprintf(stderr, "%s\n", modbus_strerror(errno));
return -1;
}

modbus_close(ctx);
modbus_free(ctx);
-------------------


SEE ALSO
--------
linkmb:modbus_read_general_reference[3]
linkmb:modbus_mapping_new_start_address_extend[3]
linkmb:modbus_mapping_new_extend[3]

AUTHORS
-------
The libmodbus documentation was written by Stéphane Raimbault
<stephane.raimbault@gmail.com>
3 changes: 3 additions & 0 deletions src/modbus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ MODBUS_BEGIN_DECLS
#define _RESPONSE_TIMEOUT 500000
#define _BYTE_TIMEOUT 500000

#define SUB_REQUEST_LENGHT 0x07
#define SUB_REQUEST_REF_TYPE 0x06 // Reference-Type for General Reference Read + Write

typedef enum {
_MODBUS_BACKEND_TYPE_RTU=0,
_MODBUS_BACKEND_TYPE_TCP
Expand Down
Loading