MODBUS laajempi esimerkki
modbus.h
Go to the documentation of this file.
1 
22  /****************************** ModBus ASCII Frame **********************************
23  * *
24  * *
25  * | START | ADDRESS | FUNCTION | DATA | LRC | END | *
26  * | 1 CHAR | 2 CHARS | 2 CHARS | n CHARS | 2 CHARS | 2 CHARS | *
27  * *
28  * *
29  *************************************************************************************/
30 
31 
32 
33 /* ModBus Error Counter definitions */
34 #define BUS_MESSAGE_COUNTER 0
35 #define BUS_COMMUNICATION_ERROR_COUNTER 4
36 #define EXCEPTION_ERROR_COUNTER 8
37 #define SLAVE_MESSAGE_COUNTER 12
38 #define BUS_CHARACTER_OVERRUN_COUNTER 16
39 /* #define IOP_OVERRUN_COUNTER 0X13 */ /* Not in use */
40 
41 /* Added Counters (not ModBus) */
42 #define FIRST_START_INDICATOR 16
43 #define BOOT_COUNTER 20
44 
45 /* Message parameters */
46 #define MAX_LENGTH_OF_MESSAGE 128
47 #define MAX_LENGTH_OF_BUFFER MAX_LENGTH_OF_MESSAGE*2
48 
49 /* Quantities of Holding Registers */
50 #define QUANTITY_MEASUREMENT_REGISTERS 8
51 #define QUANTITY_OF_COUNTER_REGISTERS 3
52 
53 /* Holding Register definitions */
54 #define START_OF_MEDIAN_REGISTERS 0
55 #define END_OF_MEDIAN_REGISTERS 8
56 #define START_OF_REAL_TIME_REGISTERS 100
57 #define END_OF_REAL_TIME_REGISTERS 108
58 #define START_OF_COUNTER_REGISTERS 200
59 #define END_OF_COUNTER_REGISTERS 203
60 
61 /* Counter value position in ModBus Response Message */ /* Needed when counters are read from holding registers */
62 #define PLACE_OF_BOOT_COUNTER_HIGH 3
63 #define PLACE_OF_BOOT_COUNTER_LOW 4
64 #define PLACE_OF_EXCEPTION_ERROR_COUNTER_HIGH 5
65 #define PLACE_OF_EXCEPTION_ERROR_COUNTER_LOW 6
66 #define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_HIGH 7
67 #define PLACE_OF_BUS_COMMUNICATION_ERROR_COUNTER_LOW 8
68 
69 /* Query types */
70 #define MEDIAN_MEASUREMENT 1
71 #define REAL_TIME_MEASUREMENT 2
72 #define COUNTER_QUERY 3
73 
74 #define LENGTH_OF_CLEAR_COUNTERS_QUERY 7
75 
76 /* Status definitions for State Machine*/
77 enum
78 {
79  WAIT,
82  MESSAGE_RECEIVED, /* Not in use */
91  DONE,
93 };
94 
95 /* ModBus Message */
96 enum {
99 POSITION_OF_DATA_LENGTH = 3, /* Not in use */
106 };
107 
108 
109 /* ModBus Functions */
110 enum
111 {
113  INFO=0x04,
115 };
116 
117 
118 /* ModBus sub functions for Diagnostics */
119 enum
120 {
126  RETURN_SLAVE_NO_RESPONSE_COUNT = 0x0F, /* Not in use */
127  RETURN_SLAVE_NAK_COUNT = 0x10, /* Not in use */
128  RETURN_SLAVE_BUSY_COUNT = 0x11, /* Not in use */
130  RETURN_IOP_OVERRUN_COUNT = 0x13, /* Not in use */
131  CLEAR_OVERRUN_COUNTER_AND_FLAG = 0x14, /* Not in use */
132  GET_CLEAR_MODBUS_PLUS_STATISTICS = 0x15 /* Not in use */
133 };
134 
135 
136 /* Error Codes for ModBus Exception Response (First 4 are standard ModBus Exception Codes) */
137 enum {
153  };
154 
155 
156 
157 /* Prototypes */
158 unsigned char create_response_message ( unsigned char *response );
159 void clear_receive_buffer(void);
160 void clear_response_buffer(void);
161 unsigned char check_LRC ( unsigned char *response, unsigned char length_of_message );
162 unsigned char calculate_LRC ( unsigned char *message, unsigned char nro_characters );
163 unsigned char calculate_response_LRC ( unsigned char *message, unsigned char nro_characters );
164 
165 unsigned char ascii_hex_to_hex ( unsigned char *start_address);
166 unsigned char get_function ( unsigned char *message );
167 unsigned char check_own_address ( unsigned char *message );
168 
169 void create_echo_of_query( unsigned char *message, unsigned char *length_of_response_hex );
170 void create_diagnostic_response ( unsigned int sub_function, unsigned int diagnostic_counter_value );
171 void create_exception_response ( unsigned char function, unsigned char error_condition );
172 
173 unsigned int get_start_address_of_holding_registers ( unsigned char *message );
174 unsigned int get_number_of_points ( unsigned char *message );
175 void create_and_send_exception_response ( unsigned char function, unsigned char error_condition );
176 unsigned int get_sub_function ( unsigned char *message );
177 void transmit_response(unsigned char *send_pointer,unsigned char length);
178 
179 unsigned char hex_to_ascii_hex ( unsigned char *response ,unsigned char length);
180 
181 void clear_receive_buffer(void);
182 
183 void Fatal_error ( unsigned char error_code );
184 void error ( unsigned char error_code );
185 void error_getting_message ( unsigned char *message,unsigned char error_code );
186 
Definition: modbus.h:83
Definition: modbus.h:124
Definition: modbus.h:80
unsigned int sub_function
Definition: mittari_jumapohja.c:43
unsigned char * send_pointer
Definition: modbus.h:81
Definition: modbus.h:144
void Fatal_error(unsigned char error_code)
On fatal error executin comes here and watch dog will handle the reset.
Definition: util.c:27
unsigned char message[MAX_LENGTH_OF_MESSAGE]
Definition: mittari_jumapohja.c:28
Definition: modbus.h:149
Definition: modbus.h:103
void create_echo_of_query(unsigned char *message, unsigned char *length_of_response_hex)
Creates an echo of the received ModBus query.
Definition: modbus.c:364
Definition: modbus.h:85
unsigned char hex_to_ascii_hex(unsigned char *response, unsigned char length)
Definition: modbus.h:90
void clear_receive_buffer(void)
Clears the receive buffer.
Definition: modbus.c:499
unsigned int start_address
Definition: mittari_jumapohja.c:43
Definition: modbus.h:112
Definition: modbus.h:123
unsigned char length_of_message
Definition: mittari_jumapohja.c:32
Definition: modbus.h:148
Definition: modbus.h:126
Definition: modbus.h:99
Definition: modbus.h:89
Definition: modbus.h:132
Definition: modbus.h:92
Definition: modbus.h:146
Definition: modbus.h:151
unsigned int get_sub_function(unsigned char *message)
Retrieves the sub function code from the received ModBus message. Is called only if function code is ...
Definition: modbus.c:92
Definition: modbus.h:128
Definition: modbus.h:105
void error_getting_message(unsigned char *message, unsigned char error_code)
Sends a debug message based on a specific error.
Definition: modbus.c:383
Definition: modbus.h:140
Definition: modbus.h:138
Definition: modbus.h:98
void create_and_send_exception_response(unsigned char function, unsigned char error_condition)
Definition: modbus.h:88
Definition: modbus.h:91
void error(unsigned char error_code)
Definition: modbus.h:102
unsigned int get_start_address_of_holding_registers(unsigned char *message)
Retrieves the Start Address value for Holding Register reading.
Definition: modbus.c:116
Definition: modbus.h:152
unsigned char check_own_address(unsigned char *message)
Definition: modbus.h:141
Definition: modbus.h:129
Definition: modbus.h:104
unsigned char response[MAX_LENGTH_OF_MESSAGE]
Definition: mittari_jumapohja.c:29
Definition: modbus.h:100
void create_diagnostic_response(unsigned int sub_function, unsigned int diagnostic_counter_value)
Creates a response for ModBus Diagnostic query.
Definition: modbus.c:334
Definition: modbus.h:122
Definition: modbus.h:82
Definition: modbus.h:97
unsigned int get_number_of_points(unsigned char *message)
Retrieves the Number of Points value for Holding Register reading.
Definition: modbus.c:139
Definition: modbus.h:125
Definition: modbus.h:86
unsigned char ascii_hex_to_hex(unsigned char *start_address)
Converts two ASCII-Hex characters into one Hex-character.
Definition: modbus.c:191
Definition: modbus.h:150
Definition: modbus.h:87
void create_exception_response(unsigned char function, unsigned char error_condition)
Creates a ModBus Exceprion Response message. Described in page 95 Modicon Modbus Protocol Reference G...
Definition: modbus.c:314
void transmit_response(unsigned char *send_pointer, unsigned char length)
Transmits the response to a ModBus query.
Definition: modbus.c:482
Definition: modbus.h:79
unsigned char create_response_message(unsigned char *response)
Creates a response message.Not in use.
Definition: modbus.c:293
unsigned char calculate_LRC(unsigned char *message, unsigned char nro_characters)
Calculates LRC checksum from received ModBus message.
Definition: modbus.c:218
Definition: modbus.h:145
unsigned char check_LRC(unsigned char *response, unsigned char length_of_message)
Check the LRC in the received ModBus message.
Definition: modbus.c:268
Definition: modbus.h:114
unsigned char get_function(unsigned char *message)
Retrieves the function code from the received ModBus message.
Definition: modbus.c:74
Definition: modbus.h:113
Definition: modbus.h:143
Definition: modbus.h:130
Definition: modbus.h:147
Definition: modbus.h:101
Definition: modbus.h:142
Definition: modbus.h:127
Definition: modbus.h:139
unsigned char error_condition
Definition: mittari_jumapohja.c:67
unsigned char calculate_response_LRC(unsigned char *message, unsigned char nro_characters)
Calculates LRC checksum for the response to be sended. Called only once.
Definition: modbus.c:245
Definition: modbus.h:131
void clear_response_buffer(void)
Clears the response buffer.
Definition: modbus.c:522
Definition: modbus.h:84