Commit 9b803dd4ee046edea06b588233e4b51a48432523

Authored by Stéphane Raimbault
1 parent fb89fa36

Add documentation for many functions and tweak argument names

Add macros in libmodbus.txt and:
- modbus_get_byte_from_bits.txt
- modbus_get_float.txt
- modbus_reply.txt
- modbus_reply_exception.txt
- modbus_set_bits_from_byte.txt
- modbus_set_bits_from_bytes.txt
- modbus_set_float.txt
... ... @@ -10,8 +10,9 @@ libmodbus 2.9.5 (2011-06-XX)
10 10 * modbus_read_and_write_registers -> modbus_write_and_read_registers
11 11 The function name was confusing because the write operation is performed
12 12 before the read. Take care to swap the arguments in the migration process.
13   -- Documentation of modbus_write_and_read_registers, modbus_mapping_new/free
14   - and report_slave_id.
  13 +- Documentation of modbus_write_and_read_registers, modbus_mapping_new/free,
  14 + report_slave_id. modbus_get_byte_from_bits, modbus_set_bits_from_byte(s),
  15 + modbus_[gs]et_float, modbus_reply and modbus_reply_exception.
15 16 - Enhanced report slave ID
16 17  
17 18 libmodbus 2.9.4 (2011-06-05)
... ...
doc/Makefile.am
... ... @@ -4,6 +4,9 @@ MAN3 = \
4 4 modbus_flush.3 \
5 5 modbus_free.3 \
6 6 modbus_get_byte_timeout.3 \
  7 + modbus_get_byte_from_bits.3 \
  8 + modbus_get_float.3 \
  9 + modbus_set_float.3 \
7 10 modbus_get_header_length.3 \
8 11 modbus_get_socket.3 \
9 12 modbus_get_response_timeout.3 \
... ... @@ -20,10 +23,14 @@ MAN3 = \
20 23 modbus_receive_confirmation.3 \
21 24 modbus_receive_from.3 \
22 25 modbus_report_slave_id.3 \
  26 + modbus_reply.3 \
  27 + modbus_reply_exception.3 \
23 28 modbus_rtu_get_serial_mode.3 \
24 29 modbus_rtu_set_serial_mode.3 \
25 30 modbus_send_raw_request.3 \
26 31 modbus_set_byte_timeout.3 \
  32 + modbus_set_bits_from_byte.3 \
  33 + modbus_set_bits_from_bytes.3 \
27 34 modbus_set_debug.3 \
28 35 modbus_set_error_recovery.3 \
29 36 modbus_set_slave.3 \
... ...
doc/libmodbus.txt
... ... @@ -120,6 +120,23 @@ A libmodbus 'context' is thread safe and may be shared among as many application
120 120 threads as necessary, without any additional locking required on the part of the
121 121 caller.
122 122  
  123 +Macros for data manipulation::
  124 + MODBUS_GET_HIGH_BYTE(data), extracts the high byte from a byte
  125 + MODBUS_GET_LOW_BYTE(data), extracts the low byte from a byte
  126 + MODBUS_GET_INT32_FROM_INT16(tab_int16, index), builds an int32 from the two
  127 + first int16 starting at tab_int16[index]
  128 + MODBUS_GET_INT16_FROM_INT8(tab_int8, index), builds an int16 from the two
  129 + first int8 starting at tab_int8[index]
  130 + MODBUS_SET_INT16_TO_INT8(tab_int8, index, value), set an int16 value into
  131 + the two first bytes starting at tab_int8[index]
  132 +
  133 +Functions for data manipulation::
  134 + linkmb:modbus_set_bits_from_byte[3]
  135 + linkmb:modbus_set_bits_from_bytes[3]
  136 + linkmb:modbus_get_byte_from_bits[3]
  137 + linkmb:modbus_get_float[3]
  138 + linkmb:modbus_set_float[3]
  139 +
123 140  
124 141 Connection
125 142 ~~~~~~~~~~
... ... @@ -162,6 +179,9 @@ Raw requests::
162 179 linkmb:modbus_send_raw_request[3]
163 180 linkmb:modbus_receive_confirmation[3]
164 181  
  182 +Reply an exception::
  183 + linkmb:modbus_reply_exception[3]
  184 +
165 185  
166 186 Server
167 187 ~~~~~~
... ... @@ -178,6 +198,7 @@ Receive::
178 198  
179 199 Reply::
180 200 linkmb:modbus_reply[3]
  201 + linkmb:modbus_reply_exception[3]
181 202  
182 203  
183 204 ERROR HANDLING
... ...
doc/modbus_get_byte_from_bits.txt 0 → 100644
  1 +modbus_get_byte_from_bits(3)
  2 +============================
  3 +
  4 +NAME
  5 +----
  6 +modbus_get_byte_from_bits - get the value from many bits
  7 +
  8 +
  9 +SYNOPSIS
  10 +--------
  11 +*uint8_t modbus_get_byte_from_bits(const uint8_t *'src', int 'index', unsigned int 'nb_bits');*
  12 +
  13 +
  14 +DESCRIPTION
  15 +-----------
  16 +The _modbus_get_byte_from_bits()_ function shall extract a value from many
  17 +bits. All 'nb_bits' bits from 'src' at position 'index' will be read as a
  18 +single value. To obtain a full byte, set nb_bits to 8.
  19 +
  20 +
  21 +RETURN VALUE
  22 +------------
  23 +The _modbus_get_byte_from_bits()_ function shall return a byte containing the
  24 +bits read.
  25 +
  26 +
  27 +SEE ALSO
  28 +--------
  29 +linkmb:modbus_set_bits_from_byte[3]
  30 +linkmb:modbus_set_bits_from_bytes[3]
  31 +
  32 +
  33 +AUTHORS
  34 +-------
  35 +The libmodbus documentation was written by Stéphane Raimbault
  36 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_get_float.txt 0 → 100644
  1 +modbus_get_float(3)
  2 +===================
  3 +
  4 +
  5 +NAME
  6 +----
  7 +modbus_get_float - get a float value
  8 +
  9 +
  10 +SYNOPSIS
  11 +--------
  12 +*float modbus_get_float(const uint16_t *'src');*
  13 +
  14 +
  15 +DESCRIPTION
  16 +-----------
  17 +The _modbus_get_float()_ function shall get a float from 4 bytes in Modbus
  18 +format. The 'src' array must be pointer on two 16 bits values, for example,
  19 +if the first word is set to 0x4465 and the second to 0x229a, the float value
  20 +read will be 916.540649.
  21 +
  22 +
  23 +RETURN VALUE
  24 +------------
  25 +The _modbus_get_float()_ function shall return a float.
  26 +
  27 +
  28 +SEE ALSO
  29 +--------
  30 +linkmb:modbus_set_float[3]
  31 +
  32 +
  33 +AUTHORS
  34 +-------
  35 +The libmodbus documentation was written by Stéphane Raimbault
  36 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_reply.txt 0 → 100644
  1 +modbus_reply(3)
  2 +===============
  3 +
  4 +NAME
  5 +----
  6 +modbus_reply - send a reponse to the received request
  7 +
  8 +
  9 +SYNOPSIS
  10 +--------
  11 +*int modbus_reply(modbus_t *'ctx', const uint8_t *'req', int 'req_length', modbus_mapping_t *'mb_mapping');
  12 +
  13 +
  14 +DESCRIPTION
  15 +-----------
  16 +The _modbus_reply()_ function shall send a response to received request. The
  17 +request 'req' given in argument is analyzed, a response is then built and sent
  18 +by using the information of the modbus context 'ctx'.
  19 +
  20 +If the request indicates to read or write a value the operation will done in the
  21 +modbus mapping 'mb_mapping' according to the type of the manipulated data.
  22 +
  23 +If an error occurs, an exception response will be sent.
  24 +
  25 +This function is designed for Modbus server.
  26 +
  27 +
  28 +RETURN VALUE
  29 +------------
  30 +The _modbus_reply()_ function shall return the length of the response sent if
  31 +successful. Otherwise it shall return -1 and set errno.
  32 +
  33 +
  34 +ERRORS
  35 +------
  36 +EMBMDATA::
  37 +Sending has failed
  38 +
  39 +See also the errors returned by the syscall used to send the response (eg. send
  40 +or write).
  41 +
  42 +
  43 +SEE ALSO
  44 +--------
  45 +linkmb:modbus_reply_exception[3]
  46 +linkmb:libmodbus[7]
  47 +
  48 +
  49 +AUTHORS
  50 +-------
  51 +The libmodbus documentation was written by Stéphane Raimbault
  52 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_reply_exception.txt 0 → 100644
  1 +modbus_reply_exception(3)
  2 +=========================
  3 +
  4 +NAME
  5 +----
  6 +modbus_reply_exception - send an exception reponse
  7 +
  8 +
  9 +SYNOPSIS
  10 +--------
  11 +*int modbus_reply_exception(modbus_t *'ctx', const uint8_t *'req', unsigned int 'exception_code');
  12 +
  13 +
  14 +DESCRIPTION
  15 +-----------
  16 +The _modbus_reply_exception()_ function shall send an exception response based
  17 +on the 'exception_code' in argument.
  18 +
  19 +The libmodbus provides the following exception codes:
  20 +
  21 +* MODBUS_EXCEPTION_ILLEGAL_FUNCTION (1)
  22 +* MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS (2)
  23 +* MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE (3)
  24 +* MODBUS_EXCEPTION_SLAVE_OR_SERVER_FAILURE (4)
  25 +* MODBUS_EXCEPTION_ACKNOWLEDGE (5)
  26 +* MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY (6)
  27 +* MODBUS_EXCEPTION_NEGATIVE_ACKNOWLEDGE (7)
  28 +* MODBUS_EXCEPTION_MEMORY_PARITY (8)
  29 +* MODBUS_EXCEPTION_NOT_DEFINED (9)
  30 +* MODBUS_EXCEPTION_GATEWAY_PATH (10)
  31 +* MODBUS_EXCEPTION_GATEWAY_TARGET (11)
  32 +
  33 +The initial request 'req' is required to build a valid response.
  34 +
  35 +
  36 +RETURN VALUE
  37 +------------
  38 +The _modbus_reply_exception()_ function shall return the length of the response sent if
  39 +successful. Otherwise it shall return -1 and set errno.
  40 +
  41 +
  42 +ERRORS
  43 +------
  44 +EINVAL::
  45 +The exception code is invalid
  46 +
  47 +
  48 +SEE ALSO
  49 +--------
  50 +linkmb:modbus_reply[3]
  51 +linkmb:libmodbus[7]
  52 +
  53 +
  54 +AUTHORS
  55 +-------
  56 +The libmodbus documentation was written by Stéphane Raimbault
  57 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_set_bits_from_byte.txt 0 → 100644
  1 +modbus_set_bits_from_byte(3)
  2 +============================
  3 +
  4 +
  5 +NAME
  6 +----
  7 +modbus_set_bits_from_byte - set many bits from a single byte value
  8 +
  9 +
  10 +SYNOPSIS
  11 +--------
  12 +*void modbus_set_bits_from_byte(uint8_t *'dest', int 'index', const uint8_t 'value');*
  13 +
  14 +
  15 +DESCRIPTION
  16 +-----------
  17 +The _modbus_set_bits_from_byte_ function shall set many bits from a single byte.
  18 +All 8 bits from the byte 'value' will be written to 'dest' array starting at
  19 +'index' position.
  20 +
  21 +
  22 +RETURN VALUE
  23 +------------
  24 +There is no return values.
  25 +
  26 +
  27 +SEE ALSO
  28 +--------
  29 +linkmb:modbus_set_bits_from_byte[3]
  30 +linkmb:modbus_set_bits_from_bytes[3]
  31 +
  32 +
  33 +AUTHORS
  34 +-------
  35 +The libmodbus documentation was written by Stéphane Raimbault
  36 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_set_bits_from_bytes.txt 0 → 100644
  1 +modbus_set_bits_from_bytes(3)
  2 +============================
  3 +
  4 +
  5 +NAME
  6 +----
  7 +modbus_set_bits_from_bytes - set many bits from an array of bytes
  8 +
  9 +
  10 +SYNOPSIS
  11 +--------
  12 +*void modbus_set_bits_from_bytes(uint8_t *'dest', int 'index', unsigned int 'nb_bits', const uint8_t *'tab_byte');*
  13 +
  14 +
  15 +DESCRIPTION
  16 +-----------
  17 +The _modbus_set_bits_from_bytes_ function shall set bits by reading an array of
  18 +bytes. All the bits of the bytes read from the first position of the array
  19 +'tab_byte' are written as bits in the 'dest' array starting at position 'index'.
  20 +
  21 +
  22 +RETURN VALUE
  23 +------------
  24 +There is no return values.
  25 +
  26 +
  27 +SEE ALSO
  28 +--------
  29 +linkmb:modbus_set_bits_from_byte[3]
  30 +linkmb:modbus_get_byte_from_bits[3]
  31 +
  32 +
  33 +AUTHORS
  34 +-------
  35 +The libmodbus documentation was written by Stéphane Raimbault
  36 +<stephane.raimbault@gmail.com>
... ...
doc/modbus_set_float.txt 0 → 100644
  1 +modbus_set_float(3)
  2 +===================
  3 +
  4 +
  5 +NAME
  6 +----
  7 +modbus_set_float - set a float value
  8 +
  9 +
  10 +SYNOPSIS
  11 +--------
  12 +*void modbus_set_float(float 'f', uint16_t *'dest');*
  13 +
  14 +
  15 +DESCRIPTION
  16 +-----------
  17 +The _modbus_set_float()_ function shall set a float to 4 bytes in Modbus
  18 +format. The 'dest' array must be pointer on two 16 bits values to be able to
  19 +store the full result of the conversion.
  20 +
  21 +
  22 +RETURN VALUE
  23 +------------
  24 +The is no return values.
  25 +
  26 +
  27 +SEE ALSO
  28 +--------
  29 +linkmb:modbus_set_float[3]
  30 +
  31 +
  32 +AUTHORS
  33 +-------
  34 +The libmodbus documentation was written by Stéphane Raimbault
  35 +<stephane.raimbault@gmail.com>
... ...
src/modbus-data.c
... ... @@ -27,25 +27,25 @@
27 27  
28 28 /* Sets many bits from a single byte value (all 8 bits of the byte value are
29 29 set) */
30   -void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value)
  30 +void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value)
31 31 {
32 32 int i;
33 33  
34 34 for (i=0; i<8; i++) {
35   - dest[address+i] = (value & (1 << i)) ? 1 : 0;
  35 + dest[index+i] = (value & (1 << i)) ? 1 : 0;
36 36 }
37 37 }
38 38  
39   -/* Sets many bits from a table of bytes (only the bits between address and
40   - address + nb_bits are set) */
41   -void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits,
  39 +/* Sets many bits from a table of bytes (only the bits between index and
  40 + index + nb_bits are set) */
  41 +void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits,
42 42 const uint8_t *tab_byte)
43 43 {
44 44 int i;
45 45 int shift = 0;
46 46  
47   - for (i = address; i < address + nb_bits; i++) {
48   - dest[i] = tab_byte[(i - address) / 8] & (1 << shift) ? 1 : 0;
  47 + for (i = index; i < index + nb_bits; i++) {
  48 + dest[i] = tab_byte[(i - index) / 8] & (1 << shift) ? 1 : 0;
49 49 /* gcc doesn't like: shift = (++shift) % 8; */
50 50 shift++;
51 51 shift %= 8;
... ... @@ -54,7 +54,7 @@ void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits
54 54  
55 55 /* Gets the byte value from many bits.
56 56 To obtain a full byte, set nb_bits to 8. */
57   -uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address,
  57 +uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index,
58 58 unsigned int nb_bits)
59 59 {
60 60 int i;
... ... @@ -67,7 +67,7 @@ uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address,
67 67 }
68 68  
69 69 for (i=0; i < nb_bits; i++) {
70   - value |= (src[address+i] << i);
  70 + value |= (src[index+i] << i);
71 71 }
72 72  
73 73 return value;
... ...
src/modbus.c
... ... @@ -632,7 +632,7 @@ static int response_exception(modbus_t *ctx, sft_t *sft,
632 632 return rsp_length;
633 633 }
634 634  
635   -/* Send a response to the receive request.
  635 +/* Send a response to the received request.
636 636 Analyses the request and constructs a response.
637 637  
638 638 If an error occurs, this function construct the response
... ...
src/modbus.h
... ... @@ -208,10 +208,10 @@ int modbus_reply_exception(modbus_t *ctx, const uint8_t *req,
208 208 tab_int8[(index) + 1] = (value) & 0xFF; \
209 209 } while (0)
210 210  
211   -void modbus_set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
212   -void modbus_set_bits_from_bytes(uint8_t *dest, int address, unsigned int nb_bits,
  211 +void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value);
  212 +void modbus_set_bits_from_bytes(uint8_t *dest, int index, unsigned int nb_bits,
213 213 const uint8_t *tab_byte);
214   -uint8_t modbus_get_byte_from_bits(const uint8_t *src, int address, unsigned int nb_bits);
  214 +uint8_t modbus_get_byte_from_bits(const uint8_t *src, int index, unsigned int nb_bits);
215 215 float modbus_get_float(const uint16_t *src);
216 216 void modbus_set_float(float f, uint16_t *dest);
217 217  
... ...