123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #ifndef _MODBUS_H_
- #define _MODBUS_H_
- #include <stdint.h>
- #include <termios.h>
- #include <arpa/inet.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define MODBUS_TCP_DEFAULT_PORT 502
- #define HEADER_LENGTH_RTU 0
- #define PRESET_QUERY_LENGTH_RTU 6
- #define PRESET_RESPONSE_LENGTH_RTU 2
- #define HEADER_LENGTH_TCP 6
- #define PRESET_QUERY_LENGTH_TCP 12
- #define PRESET_RESPONSE_LENGTH_TCP 8
- #define CHECKSUM_LENGTH_RTU 2
- #define CHECKSUM_LENGTH_TCP 0
- #define MIN_QUERY_LENGTH 12
- #define MAX_MESSAGE_LENGTH 256
- #define MAX_STATUS 800
- #define MAX_REGISTERS 100
- #define REPORT_SLAVE_ID_LENGTH 75
- #define TIME_OUT_BEGIN_OF_TRAME 500000
- #define TIME_OUT_END_OF_TRAME 500000
- #ifndef FALSE
- #define FALSE 0
- #endif
- #ifndef TRUE
- #define TRUE 1
- #endif
- #ifndef OFF
- #define OFF 0
- #endif
- #ifndef ON
- #define ON 1
- #endif
- #define FC_READ_COIL_STATUS 0x01
- #define FC_READ_INPUT_STATUS 0x02
- #define FC_READ_HOLDING_REGISTERS 0x03
- #define FC_READ_INPUT_REGISTERS 0x04
- #define FC_FORCE_SINGLE_COIL 0x05
- #define FC_PRESET_SINGLE_REGISTER 0x06
- #define FC_READ_EXCEPTION_STATUS 0x07
- #define FC_FORCE_MULTIPLE_COILS 0x0F
- #define FC_PRESET_MULTIPLE_REGISTERS 0x10
- #define FC_REPORT_SLAVE_ID 0x11
- #define ILLEGAL_FUNCTION -0x01
- #define ILLEGAL_DATA_ADDRESS -0x02
- #define ILLEGAL_DATA_VALUE -0x03
- #define SLAVE_DEVICE_FAILURE -0x04
- #define SERVER_FAILURE -0x04
- #define ACKNOWLEDGE -0x05
- #define SLAVE_DEVICE_BUSY -0x06
- #define SERVER_BUSY -0x06
- #define NEGATIVE_ACKNOWLEDGE -0x07
- #define MEMORY_PARITY_ERROR -0x08
- #define GATEWAY_PROBLEM_PATH -0x0A
- #define GATEWAY_PROBLEM_TARGET -0x0B
- #define COMM_TIME_OUT -0x0C
- #define PORT_SOCKET_FAILURE -0x0D
- #define SELECT_FAILURE -0x0E
- #define TOO_MANY_DATA -0x0F
- #define INVALID_CRC -0x10
- #define INVALID_EXCEPTION_CODE -0x11
- #define CONNECTION_CLOSED -0x12
- #define MSG_LENGTH_UNDEFINED -1
- typedef enum { RTU, TCP } type_com_t;
- typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
- typedef struct {
-
- int fd;
-
- type_com_t type_com;
-
- int debug;
-
- int header_length;
-
- int checksum_length;
-
- int port;
-
- #ifdef __APPLE_CC__
- char device[64];
- #else
- char device[16];
- #endif
-
- int baud;
-
- uint8_t data_bit;
-
- uint8_t stop_bit;
-
- char parity[5];
-
- uint8_t error_handling;
-
- char ip[16];
-
- struct termios old_tios;
- } modbus_param_t;
- typedef struct {
- int nb_coil_status;
- int nb_input_status;
- int nb_input_registers;
- int nb_holding_registers;
- uint8_t *tab_coil_status;
- uint8_t *tab_input_status;
- uint16_t *tab_input_registers;
- uint16_t *tab_holding_registers;
- } modbus_mapping_t;
- int read_coil_status(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, uint8_t *dest);
- int read_input_status(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, uint8_t *dest);
- int read_holding_registers(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, uint16_t *dest);
- int read_input_registers(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, uint16_t *dest);
- int force_single_coil(modbus_param_t *mb_param, int slave,
- int coil_addr, int state);
- int preset_single_register(modbus_param_t *mb_param, int slave,
- int reg_addr, int value);
- int force_multiple_coils(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, const uint8_t *data);
- int preset_multiple_registers(modbus_param_t *mb_param, int slave,
- int start_addr, int nb, const uint16_t *data);
- int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
- void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
- int baud, const char *parity, int data_bit,
- int stop_bit);
-
- void modbus_init_tcp(modbus_param_t *mb_param, const char *ip_address, int port);
- void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
- int modbus_connect(modbus_param_t *mb_param);
- void modbus_close(modbus_param_t *mb_param);
- void modbus_set_debug(modbus_param_t *mb_param, int boolean);
- int modbus_mapping_new(modbus_mapping_t *mb_mapping,
- int nb_coil_status, int nb_input_status,
- int nb_holding_registers, int nb_input_registers);
- void modbus_mapping_free(modbus_mapping_t *mb_mapping);
- int modbus_slave_listen_tcp(modbus_param_t *mb_param, int nb_connection);
- int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket);
- int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
- uint8_t *query, int *query_length);
- void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
- int query_length, modbus_mapping_t *mb_mapping);
- void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value);
- void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits,
- const uint8_t *tab_byte);
- uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits);
- #ifdef __cplusplus
- }
- #endif
- #endif
|