Jason Engelman
/
Adaptronictmp
First Working Master with Adaptronic simulator
Protocol/modbus.h@0:150538eb0bf3, 2011-07-24 (annotated)
- Committer:
- tecnosys
- Date:
- Sun Jul 24 05:04:06 2011 +0000
- Revision:
- 0:150538eb0bf3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tecnosys | 0:150538eb0bf3 | 1 | /* |
tecnosys | 0:150538eb0bf3 | 2 | * Copyright � 2001-2008 St�phane Raimbault <stephane.raimbault@gmail.com> |
tecnosys | 0:150538eb0bf3 | 3 | * |
tecnosys | 0:150538eb0bf3 | 4 | * This program is free software: you can redistribute it and/or modify |
tecnosys | 0:150538eb0bf3 | 5 | * it under the terms of the GNU Lesser Public License as published by |
tecnosys | 0:150538eb0bf3 | 6 | * the Free Software Foundation; either version 3 of the License, or |
tecnosys | 0:150538eb0bf3 | 7 | * (at your option) any later version. |
tecnosys | 0:150538eb0bf3 | 8 | * |
tecnosys | 0:150538eb0bf3 | 9 | * This program is distributed in the hope that it will be useful, |
tecnosys | 0:150538eb0bf3 | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
tecnosys | 0:150538eb0bf3 | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
tecnosys | 0:150538eb0bf3 | 12 | * GNU Lesser Public License for more details. |
tecnosys | 0:150538eb0bf3 | 13 | * |
tecnosys | 0:150538eb0bf3 | 14 | * You should have received a copy of the GNU Lesser Public License |
tecnosys | 0:150538eb0bf3 | 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
tecnosys | 0:150538eb0bf3 | 16 | */ |
tecnosys | 0:150538eb0bf3 | 17 | |
tecnosys | 0:150538eb0bf3 | 18 | #pragma once |
tecnosys | 0:150538eb0bf3 | 19 | |
tecnosys | 0:150538eb0bf3 | 20 | #include <stdint.h> |
tecnosys | 0:150538eb0bf3 | 21 | //#include <termios.h> |
tecnosys | 0:150538eb0bf3 | 22 | //#include <arpa/inet.h> |
tecnosys | 0:150538eb0bf3 | 23 | |
tecnosys | 0:150538eb0bf3 | 24 | |
tecnosys | 0:150538eb0bf3 | 25 | #define MODBUS_TCP_DEFAULT_PORT 502 |
tecnosys | 0:150538eb0bf3 | 26 | |
tecnosys | 0:150538eb0bf3 | 27 | #define HEADER_LENGTH_RTU 0 |
tecnosys | 0:150538eb0bf3 | 28 | #define PRESET_QUERY_LENGTH_RTU 6 |
tecnosys | 0:150538eb0bf3 | 29 | #define PRESET_RESPONSE_LENGTH_RTU 2 |
tecnosys | 0:150538eb0bf3 | 30 | |
tecnosys | 0:150538eb0bf3 | 31 | #define HEADER_LENGTH_TCP 6 |
tecnosys | 0:150538eb0bf3 | 32 | #define PRESET_QUERY_LENGTH_TCP 12 |
tecnosys | 0:150538eb0bf3 | 33 | #define PRESET_RESPONSE_LENGTH_TCP 8 |
tecnosys | 0:150538eb0bf3 | 34 | |
tecnosys | 0:150538eb0bf3 | 35 | #define CHECKSUM_LENGTH_RTU 2 |
tecnosys | 0:150538eb0bf3 | 36 | #define CHECKSUM_LENGTH_TCP 0 |
tecnosys | 0:150538eb0bf3 | 37 | |
tecnosys | 0:150538eb0bf3 | 38 | /* It's not really the minimal length (the real one is report slave ID |
tecnosys | 0:150538eb0bf3 | 39 | * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP |
tecnosys | 0:150538eb0bf3 | 40 | * communications to read many values or write a single one. |
tecnosys | 0:150538eb0bf3 | 41 | * Maximum between : |
tecnosys | 0:150538eb0bf3 | 42 | * - HEADER_LENGTH_TCP (6) + slave (1) + function (1) + address (2) + |
tecnosys | 0:150538eb0bf3 | 43 | * number (2) |
tecnosys | 0:150538eb0bf3 | 44 | * - HEADER_LENGTH_RTU (0) + slave (1) + function (1) + address (2) + |
tecnosys | 0:150538eb0bf3 | 45 | * number (2) + CRC (2) |
tecnosys | 0:150538eb0bf3 | 46 | */ |
tecnosys | 0:150538eb0bf3 | 47 | #define MIN_QUERY_LENGTH 12 |
tecnosys | 0:150538eb0bf3 | 48 | |
tecnosys | 0:150538eb0bf3 | 49 | /* Page 102, Application Notes of PI�MBUS�300: |
tecnosys | 0:150538eb0bf3 | 50 | * The maximum length of the entire message must not exceed 256 |
tecnosys | 0:150538eb0bf3 | 51 | * bytes. |
tecnosys | 0:150538eb0bf3 | 52 | */ |
tecnosys | 0:150538eb0bf3 | 53 | #define MAX_MESSAGE_LENGTH 256 |
tecnosys | 0:150538eb0bf3 | 54 | |
tecnosys | 0:150538eb0bf3 | 55 | #define MAX_STATUS 800 |
tecnosys | 0:150538eb0bf3 | 56 | #define MAX_REGISTERS 100 |
tecnosys | 0:150538eb0bf3 | 57 | |
tecnosys | 0:150538eb0bf3 | 58 | #define REPORT_SLAVE_ID_LENGTH 75 |
tecnosys | 0:150538eb0bf3 | 59 | |
tecnosys | 0:150538eb0bf3 | 60 | /* Time out between trames in microsecond */ |
tecnosys | 0:150538eb0bf3 | 61 | #define TIME_OUT_BEGIN_OF_TRAME 500000 |
tecnosys | 0:150538eb0bf3 | 62 | #define TIME_OUT_END_OF_TRAME 500000 |
tecnosys | 0:150538eb0bf3 | 63 | |
tecnosys | 0:150538eb0bf3 | 64 | #ifndef FALSE |
tecnosys | 0:150538eb0bf3 | 65 | #define FALSE 0 |
tecnosys | 0:150538eb0bf3 | 66 | #endif |
tecnosys | 0:150538eb0bf3 | 67 | |
tecnosys | 0:150538eb0bf3 | 68 | #ifndef TRUE |
tecnosys | 0:150538eb0bf3 | 69 | #define TRUE 1 |
tecnosys | 0:150538eb0bf3 | 70 | #endif |
tecnosys | 0:150538eb0bf3 | 71 | |
tecnosys | 0:150538eb0bf3 | 72 | #ifndef OFF |
tecnosys | 0:150538eb0bf3 | 73 | #define OFF 0 |
tecnosys | 0:150538eb0bf3 | 74 | #endif |
tecnosys | 0:150538eb0bf3 | 75 | |
tecnosys | 0:150538eb0bf3 | 76 | #ifndef ON |
tecnosys | 0:150538eb0bf3 | 77 | #define ON 1 |
tecnosys | 0:150538eb0bf3 | 78 | #endif |
tecnosys | 0:150538eb0bf3 | 79 | |
tecnosys | 0:150538eb0bf3 | 80 | /* Function codes */ |
tecnosys | 0:150538eb0bf3 | 81 | #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */ |
tecnosys | 0:150538eb0bf3 | 82 | #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */ |
tecnosys | 0:150538eb0bf3 | 83 | #define FC_READ_HOLDING_REGISTERS 0x03 |
tecnosys | 0:150538eb0bf3 | 84 | #define FC_READ_INPUT_REGISTERS 0x04 |
tecnosys | 0:150538eb0bf3 | 85 | #define FC_FORCE_SINGLE_COIL 0x05 |
tecnosys | 0:150538eb0bf3 | 86 | #define FC_PRESET_SINGLE_REGISTER 0x06 |
tecnosys | 0:150538eb0bf3 | 87 | #define FC_READ_EXCEPTION_STATUS 0x07 |
tecnosys | 0:150538eb0bf3 | 88 | #define FC_FORCE_MULTIPLE_COILS 0x0F |
tecnosys | 0:150538eb0bf3 | 89 | #define FC_PRESET_MULTIPLE_REGISTERS 0x10 |
tecnosys | 0:150538eb0bf3 | 90 | #define FC_REPORT_SLAVE_ID 0x11 |
tecnosys | 0:150538eb0bf3 | 91 | |
tecnosys | 0:150538eb0bf3 | 92 | /* Protocol exceptions */ |
tecnosys | 0:150538eb0bf3 | 93 | #define ILLEGAL_FUNCTION -0x01 |
tecnosys | 0:150538eb0bf3 | 94 | #define ILLEGAL_DATA_ADDRESS -0x02 |
tecnosys | 0:150538eb0bf3 | 95 | #define ILLEGAL_DATA_VALUE -0x03 |
tecnosys | 0:150538eb0bf3 | 96 | #define SLAVE_DEVICE_FAILURE -0x04 |
tecnosys | 0:150538eb0bf3 | 97 | #define SERVER_FAILURE -0x04 |
tecnosys | 0:150538eb0bf3 | 98 | #define ACKNOWLEDGE -0x05 |
tecnosys | 0:150538eb0bf3 | 99 | #define SLAVE_DEVICE_BUSY -0x06 |
tecnosys | 0:150538eb0bf3 | 100 | #define SERVER_BUSY -0x06 |
tecnosys | 0:150538eb0bf3 | 101 | #define NEGATIVE_ACKNOWLEDGE -0x07 |
tecnosys | 0:150538eb0bf3 | 102 | #define MEMORY_PARITY_ERROR -0x08 |
tecnosys | 0:150538eb0bf3 | 103 | #define GATEWAY_PROBLEM_PATH -0x0A |
tecnosys | 0:150538eb0bf3 | 104 | #define GATEWAY_PROBLEM_TARGET -0x0B |
tecnosys | 0:150538eb0bf3 | 105 | |
tecnosys | 0:150538eb0bf3 | 106 | /* Local */ |
tecnosys | 0:150538eb0bf3 | 107 | #define COMM_TIME_OUT -0x0C |
tecnosys | 0:150538eb0bf3 | 108 | #define PORT_SOCKET_FAILURE -0x0D |
tecnosys | 0:150538eb0bf3 | 109 | #define SELECT_FAILURE -0x0E |
tecnosys | 0:150538eb0bf3 | 110 | #define TOO_MANY_DATA -0x0F |
tecnosys | 0:150538eb0bf3 | 111 | #define INVALID_CRC -0x10 |
tecnosys | 0:150538eb0bf3 | 112 | #define INVALID_EXCEPTION_CODE -0x11 |
tecnosys | 0:150538eb0bf3 | 113 | #define CONNECTION_CLOSED -0x12 |
tecnosys | 0:150538eb0bf3 | 114 | |
tecnosys | 0:150538eb0bf3 | 115 | /* Internal using */ |
tecnosys | 0:150538eb0bf3 | 116 | #define MSG_LENGTH_UNDEFINED -1 |
tecnosys | 0:150538eb0bf3 | 117 | |
tecnosys | 0:150538eb0bf3 | 118 | typedef enum { RTU, TCP } type_com_t; |
tecnosys | 0:150538eb0bf3 | 119 | typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t; |
tecnosys | 0:150538eb0bf3 | 120 | |
tecnosys | 0:150538eb0bf3 | 121 | /* This structure is byte-aligned */ |
tecnosys | 0:150538eb0bf3 | 122 | typedef struct { |
tecnosys | 0:150538eb0bf3 | 123 | /* Descriptor (tty or socket) */ |
tecnosys | 0:150538eb0bf3 | 124 | int fd; |
tecnosys | 0:150538eb0bf3 | 125 | /* Communication mode: RTU or TCP */ |
tecnosys | 0:150538eb0bf3 | 126 | type_com_t type_com; |
tecnosys | 0:150538eb0bf3 | 127 | /* Flag debug */ |
tecnosys | 0:150538eb0bf3 | 128 | int debug; |
tecnosys | 0:150538eb0bf3 | 129 | /* Header length used for offset */ |
tecnosys | 0:150538eb0bf3 | 130 | int header_length; |
tecnosys | 0:150538eb0bf3 | 131 | /* Checksum length RTU = 2 and TCP = 0 */ |
tecnosys | 0:150538eb0bf3 | 132 | int checksum_length; |
tecnosys | 0:150538eb0bf3 | 133 | /* TCP port */ |
tecnosys | 0:150538eb0bf3 | 134 | int port; |
tecnosys | 0:150538eb0bf3 | 135 | /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" |
tecnosys | 0:150538eb0bf3 | 136 | on Mac OS X for KeySpan USB<->Serial adapters this string |
tecnosys | 0:150538eb0bf3 | 137 | had to be made bigger on OS X as the directory+file name |
tecnosys | 0:150538eb0bf3 | 138 | was bigger than 19 bytes. Making it 67 bytes for now, but |
tecnosys | 0:150538eb0bf3 | 139 | OS X does support 256 byte file names. May become a problem |
tecnosys | 0:150538eb0bf3 | 140 | in the future. */ |
tecnosys | 0:150538eb0bf3 | 141 | #ifdef __APPLE_CC__ |
tecnosys | 0:150538eb0bf3 | 142 | char device[64]; |
tecnosys | 0:150538eb0bf3 | 143 | #else |
tecnosys | 0:150538eb0bf3 | 144 | char device[16]; |
tecnosys | 0:150538eb0bf3 | 145 | #endif |
tecnosys | 0:150538eb0bf3 | 146 | /* Bauds: 9600, 19200, 57600, 115200, etc */ |
tecnosys | 0:150538eb0bf3 | 147 | int baud; |
tecnosys | 0:150538eb0bf3 | 148 | /* Data bit */ |
tecnosys | 0:150538eb0bf3 | 149 | uint8_t data_bit; |
tecnosys | 0:150538eb0bf3 | 150 | /* Stop bit */ |
tecnosys | 0:150538eb0bf3 | 151 | uint8_t stop_bit; |
tecnosys | 0:150538eb0bf3 | 152 | /* Parity: "even", "odd", "none" */ |
tecnosys | 0:150538eb0bf3 | 153 | char parity[5]; |
tecnosys | 0:150538eb0bf3 | 154 | /* In error_treat with TCP, do a reconnect or just dump the error */ |
tecnosys | 0:150538eb0bf3 | 155 | uint8_t error_handling; |
tecnosys | 0:150538eb0bf3 | 156 | /* IP address */ |
tecnosys | 0:150538eb0bf3 | 157 | char ip[16]; |
tecnosys | 0:150538eb0bf3 | 158 | /* Save old termios settings */ |
tecnosys | 0:150538eb0bf3 | 159 | // struct termios old_tios; |
tecnosys | 0:150538eb0bf3 | 160 | } modbus_param_t; |
tecnosys | 0:150538eb0bf3 | 161 | |
tecnosys | 0:150538eb0bf3 | 162 | typedef struct { |
tecnosys | 0:150538eb0bf3 | 163 | int nb_coil_status; |
tecnosys | 0:150538eb0bf3 | 164 | int nb_input_status; |
tecnosys | 0:150538eb0bf3 | 165 | int nb_input_registers; |
tecnosys | 0:150538eb0bf3 | 166 | int nb_holding_registers; |
tecnosys | 0:150538eb0bf3 | 167 | uint8_t *tab_coil_status; |
tecnosys | 0:150538eb0bf3 | 168 | uint8_t *tab_input_status; |
tecnosys | 0:150538eb0bf3 | 169 | uint16_t *tab_input_registers; |
tecnosys | 0:150538eb0bf3 | 170 | uint16_t *tab_holding_registers; |
tecnosys | 0:150538eb0bf3 | 171 | } modbus_mapping_t; |
tecnosys | 0:150538eb0bf3 | 172 | |
tecnosys | 0:150538eb0bf3 | 173 | |
tecnosys | 0:150538eb0bf3 | 174 | /* All functions used for sending or receiving data return: |
tecnosys | 0:150538eb0bf3 | 175 | - the numbers of values (bits or word) if success (0 or more) |
tecnosys | 0:150538eb0bf3 | 176 | - less than 0 for exceptions errors |
tecnosys | 0:150538eb0bf3 | 177 | */ |
tecnosys | 0:150538eb0bf3 | 178 | |
tecnosys | 0:150538eb0bf3 | 179 | /* Reads the boolean status of coils and sets the array elements in |
tecnosys | 0:150538eb0bf3 | 180 | the destination to TRUE or FALSE */ |
tecnosys | 0:150538eb0bf3 | 181 | int read_coil_status(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 182 | int start_addr, int nb, uint8_t *dest); |
tecnosys | 0:150538eb0bf3 | 183 | |
tecnosys | 0:150538eb0bf3 | 184 | /* Same as read_coil_status but reads the slaves input table */ |
tecnosys | 0:150538eb0bf3 | 185 | int read_input_status(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 186 | int start_addr, int nb, uint8_t *dest); |
tecnosys | 0:150538eb0bf3 | 187 | |
tecnosys | 0:150538eb0bf3 | 188 | /* Reads the holding registers in a slave and put the data into an |
tecnosys | 0:150538eb0bf3 | 189 | array */ |
tecnosys | 0:150538eb0bf3 | 190 | int read_holding_registers(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 191 | int start_addr, int nb, uint16_t *dest); |
tecnosys | 0:150538eb0bf3 | 192 | |
tecnosys | 0:150538eb0bf3 | 193 | /* Reads the input registers in a slave and put the data into an |
tecnosys | 0:150538eb0bf3 | 194 | array */ |
tecnosys | 0:150538eb0bf3 | 195 | int read_input_registers(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 196 | int start_addr, int nb, uint16_t *dest); |
tecnosys | 0:150538eb0bf3 | 197 | |
tecnosys | 0:150538eb0bf3 | 198 | /* Turns ON or OFF a single coil in the slave device */ |
tecnosys | 0:150538eb0bf3 | 199 | int force_single_coil(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 200 | int coil_addr, int state); |
tecnosys | 0:150538eb0bf3 | 201 | |
tecnosys | 0:150538eb0bf3 | 202 | /* Sets a value in one holding register in the slave device */ |
tecnosys | 0:150538eb0bf3 | 203 | int preset_single_register(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 204 | int reg_addr, int value); |
tecnosys | 0:150538eb0bf3 | 205 | |
tecnosys | 0:150538eb0bf3 | 206 | /* Sets/resets the coils in the slave from an array in argument */ |
tecnosys | 0:150538eb0bf3 | 207 | int force_multiple_coils(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 208 | int start_addr, int nb, const uint8_t *data); |
tecnosys | 0:150538eb0bf3 | 209 | |
tecnosys | 0:150538eb0bf3 | 210 | /* Copies the values in the slave from the array given in argument */ |
tecnosys | 0:150538eb0bf3 | 211 | int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
tecnosys | 0:150538eb0bf3 | 212 | int start_addr, int nb, const uint16_t *data); |
tecnosys | 0:150538eb0bf3 | 213 | |
tecnosys | 0:150538eb0bf3 | 214 | /* Returns the slave id! */ |
tecnosys | 0:150538eb0bf3 | 215 | int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest); |
tecnosys | 0:150538eb0bf3 | 216 | |
tecnosys | 0:150538eb0bf3 | 217 | /* Initializes the modbus_param_t structure for RTU. |
tecnosys | 0:150538eb0bf3 | 218 | - device: "/dev/ttyS0" |
tecnosys | 0:150538eb0bf3 | 219 | - baud: 9600, 19200, 57600, 115200, etc |
tecnosys | 0:150538eb0bf3 | 220 | - parity: "even", "odd" or "none" |
tecnosys | 0:150538eb0bf3 | 221 | - data_bits: 5, 6, 7, 8 |
tecnosys | 0:150538eb0bf3 | 222 | - stop_bits: 1, 2 |
tecnosys | 0:150538eb0bf3 | 223 | */ |
tecnosys | 0:150538eb0bf3 | 224 | void modbus_init_rtu(modbus_param_t *mb_param, const char *device, |
tecnosys | 0:150538eb0bf3 | 225 | int baud, const char *parity, int data_bit, |
tecnosys | 0:150538eb0bf3 | 226 | int stop_bit); |
tecnosys | 0:150538eb0bf3 | 227 | |
tecnosys | 0:150538eb0bf3 | 228 | /* Initializes the modbus_param_t structure for TCP. |
tecnosys | 0:150538eb0bf3 | 229 | - ip : "192.168.0.5" |
tecnosys | 0:150538eb0bf3 | 230 | - port : 1099 |
tecnosys | 0:150538eb0bf3 | 231 | |
tecnosys | 0:150538eb0bf3 | 232 | Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one |
tecnosys | 0:150538eb0bf3 | 233 | (502). It's convenient to use a port number greater than or equal |
tecnosys | 0:150538eb0bf3 | 234 | to 1024 because it's not necessary to be root to use this port |
tecnosys | 0:150538eb0bf3 | 235 | number. |
tecnosys | 0:150538eb0bf3 | 236 | */ |
tecnosys | 0:150538eb0bf3 | 237 | |
tecnosys | 0:150538eb0bf3 | 238 | /* By default, the error handling mode used is RECONNECT_ON_ERROR. |
tecnosys | 0:150538eb0bf3 | 239 | |
tecnosys | 0:150538eb0bf3 | 240 | With RECONNECT_ON_ERROR, the library will attempt an immediate |
tecnosys | 0:150538eb0bf3 | 241 | reconnection which may hang for several seconds if the network to |
tecnosys | 0:150538eb0bf3 | 242 | the remote target unit is down. |
tecnosys | 0:150538eb0bf3 | 243 | |
tecnosys | 0:150538eb0bf3 | 244 | With NOP_ON_ERROR, it is expected that the application will |
tecnosys | 0:150538eb0bf3 | 245 | check for network error returns and deal with them as necessary. |
tecnosys | 0:150538eb0bf3 | 246 | |
tecnosys | 0:150538eb0bf3 | 247 | This function is only useful in TCP mode. |
tecnosys | 0:150538eb0bf3 | 248 | */ |
tecnosys | 0:150538eb0bf3 | 249 | void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling); |
tecnosys | 0:150538eb0bf3 | 250 | |
tecnosys | 0:150538eb0bf3 | 251 | /* Establishes a modbus connexion. |
tecnosys | 0:150538eb0bf3 | 252 | Returns -1 if an error occured. */ |
tecnosys | 0:150538eb0bf3 | 253 | int modbus_connect(modbus_param_t *mb_param); |
tecnosys | 0:150538eb0bf3 | 254 | |
tecnosys | 0:150538eb0bf3 | 255 | /* Closes a modbus connection */ |
tecnosys | 0:150538eb0bf3 | 256 | void modbus_close(modbus_param_t *mb_param); |
tecnosys | 0:150538eb0bf3 | 257 | |
tecnosys | 0:150538eb0bf3 | 258 | /* Activates the debug messages */ |
tecnosys | 0:150538eb0bf3 | 259 | void modbus_set_debug(modbus_param_t *mb_param, int boolean); |
tecnosys | 0:150538eb0bf3 | 260 | |
tecnosys | 0:150538eb0bf3 | 261 | /** |
tecnosys | 0:150538eb0bf3 | 262 | * SLAVE/CLIENT FUNCTIONS |
tecnosys | 0:150538eb0bf3 | 263 | **/ |
tecnosys | 0:150538eb0bf3 | 264 | |
tecnosys | 0:150538eb0bf3 | 265 | /* Allocates 4 arrays to store coils, input status, input registers and |
tecnosys | 0:150538eb0bf3 | 266 | holding registers. The pointers are stored in modbus_mapping structure. |
tecnosys | 0:150538eb0bf3 | 267 | |
tecnosys | 0:150538eb0bf3 | 268 | Returns: TRUE if ok, FALSE on failure |
tecnosys | 0:150538eb0bf3 | 269 | */ |
tecnosys | 0:150538eb0bf3 | 270 | int modbus_mapping_new(modbus_mapping_t *mb_mapping, |
tecnosys | 0:150538eb0bf3 | 271 | int nb_coil_status, int nb_input_status, |
tecnosys | 0:150538eb0bf3 | 272 | int nb_holding_registers, int nb_input_registers); |
tecnosys | 0:150538eb0bf3 | 273 | |
tecnosys | 0:150538eb0bf3 | 274 | /* Frees the 4 arrays */ |
tecnosys | 0:150538eb0bf3 | 275 | void modbus_mapping_free(modbus_mapping_t *mb_mapping); |
tecnosys | 0:150538eb0bf3 | 276 | |
tecnosys | 0:150538eb0bf3 | 277 | |
tecnosys | 0:150538eb0bf3 | 278 | /* Listens for any query from a modbus master in TCP |
tecnosys | 0:150538eb0bf3 | 279 | Not tested in RTU communication. */ |
tecnosys | 0:150538eb0bf3 | 280 | int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length); |
tecnosys | 0:150538eb0bf3 | 281 | |
tecnosys | 0:150538eb0bf3 | 282 | /* Manages the received query. |
tecnosys | 0:150538eb0bf3 | 283 | Analyses the query and constructs a response. |
tecnosys | 0:150538eb0bf3 | 284 | |
tecnosys | 0:150538eb0bf3 | 285 | If an error occurs, this function construct the response |
tecnosys | 0:150538eb0bf3 | 286 | accordingly. |
tecnosys | 0:150538eb0bf3 | 287 | */ |
tecnosys | 0:150538eb0bf3 | 288 | void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
tecnosys | 0:150538eb0bf3 | 289 | int query_length, modbus_mapping_t *mb_mapping); |
tecnosys | 0:150538eb0bf3 | 290 | |
tecnosys | 0:150538eb0bf3 | 291 | |
tecnosys | 0:150538eb0bf3 | 292 | /** |
tecnosys | 0:150538eb0bf3 | 293 | * UTILS FUNCTIONS |
tecnosys | 0:150538eb0bf3 | 294 | **/ |
tecnosys | 0:150538eb0bf3 | 295 | |
tecnosys | 0:150538eb0bf3 | 296 | /* Sets many input/coil status from a single byte value (all 8 bits of |
tecnosys | 0:150538eb0bf3 | 297 | the byte value are setted) */ |
tecnosys | 0:150538eb0bf3 | 298 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value); |
tecnosys | 0:150538eb0bf3 | 299 | |
tecnosys | 0:150538eb0bf3 | 300 | /* Sets many input/coil status from a table of bytes (only the bits |
tecnosys | 0:150538eb0bf3 | 301 | between address and address + nb_bits are setted) */ |
tecnosys | 0:150538eb0bf3 | 302 | void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, |
tecnosys | 0:150538eb0bf3 | 303 | const uint8_t *tab_byte); |
tecnosys | 0:150538eb0bf3 | 304 | |
tecnosys | 0:150538eb0bf3 | 305 | /* Gets the byte value from many input/coil status. |
tecnosys | 0:150538eb0bf3 | 306 | To obtain a full byte, set nb_bits to 8. */ |
tecnosys | 0:150538eb0bf3 | 307 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits); |
tecnosys | 0:150538eb0bf3 | 308 | |
tecnosys | 0:150538eb0bf3 | 309 | int build_query_basis_rtu(int slave, int function, |
tecnosys | 0:150538eb0bf3 | 310 | int start_addr, int nb, |
tecnosys | 0:150538eb0bf3 | 311 | uint8_t *query); |
tecnosys | 0:150538eb0bf3 | 312 | int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
tecnosys | 0:150538eb0bf3 | 313 | int query_length); |
tecnosys | 0:150538eb0bf3 | 314 | |
tecnosys | 0:150538eb0bf3 | 315 | int modbus_receive(modbus_param_t *mb_param, |
tecnosys | 0:150538eb0bf3 | 316 | uint8_t *query, |
tecnosys | 0:150538eb0bf3 | 317 | uint8_t *response); |
tecnosys | 0:150538eb0bf3 | 318 | int receive_msg(modbus_param_t *mb_param, |
tecnosys | 0:150538eb0bf3 | 319 | int msg_length_computed, |
tecnosys | 0:150538eb0bf3 | 320 | uint8_t *msg, int *p_msg_length); |
tecnosys | 0:150538eb0bf3 | 321 |