Commit d8a5e5558f7bfb6b8f31d19723aed9c1b3473fd2

Authored by Stéphane Raimbault
1 parent c7a6ffb3

Whitespace cleanup

modbus/modbus.c
@@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
22 The function names used are inspired by the Modicon Modbus Protocol 22 The function names used are inspired by the Modicon Modbus Protocol
23 Reference Guide which can be obtained from Schneider at 23 Reference Guide which can be obtained from Schneider at
24 www.schneiderautomation.com. 24 www.schneiderautomation.com.
25 - 25 +
26 Documentation: 26 Documentation:
27 http://www.easysw.com/~mike/serial/serial.html 27 http://www.easysw.com/~mike/serial/serial.html
28 http://copyleft.free.fr/wordpress/index.php/libmodbus/ 28 http://copyleft.free.fr/wordpress/index.php/libmodbus/
@@ -57,7 +57,7 @@ @@ -57,7 +57,7 @@
57 typedef struct { 57 typedef struct {
58 int slave; 58 int slave;
59 int function; 59 int function;
60 - int t_id; 60 + int t_id;
61 } sft_t; 61 } sft_t;
62 62
63 static const uint8_t NB_TAB_ERROR_MSG = 12; 63 static const uint8_t NB_TAB_ERROR_MSG = 12;
@@ -78,61 +78,61 @@ static const char *TAB_ERROR_MSG[] = { @@ -78,61 +78,61 @@ static const char *TAB_ERROR_MSG[] = {
78 78
79 /* Table of CRC values for high-order byte */ 79 /* Table of CRC values for high-order byte */
80 static uint8_t table_crc_hi[] = { 80 static uint8_t table_crc_hi[] = {
81 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
82 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
83 - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
84 - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
85 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
86 - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,  
87 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
88 - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
89 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
90 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
91 - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
92 - 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
93 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
94 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
95 - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
96 - 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,  
97 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,  
98 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
99 - 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
100 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
101 - 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,  
102 - 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,  
103 - 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,  
104 - 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,  
105 - 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 81 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  82 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  83 + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  84 + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  85 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  86 + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  87 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  88 + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  89 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  90 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  91 + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  92 + 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  93 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  94 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  95 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  96 + 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  97 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  98 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  99 + 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  100 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  101 + 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  102 + 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  103 + 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  104 + 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  105 + 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
106 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 106 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
107 }; 107 };
108 108
109 /* Table of CRC values for low-order byte */ 109 /* Table of CRC values for low-order byte */
110 static uint8_t table_crc_lo[] = { 110 static uint8_t table_crc_lo[] = {
111 - 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,  
112 - 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,  
113 - 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,  
114 - 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,  
115 - 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,  
116 - 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,  
117 - 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,  
118 - 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,  
119 - 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,  
120 - 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,  
121 - 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,  
122 - 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,  
123 - 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,  
124 - 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,  
125 - 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,  
126 - 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,  
127 - 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,  
128 - 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,  
129 - 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,  
130 - 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,  
131 - 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,  
132 - 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,  
133 - 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,  
134 - 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,  
135 - 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 111 + 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
  112 + 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
  113 + 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
  114 + 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
  115 + 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
  116 + 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  117 + 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
  118 + 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
  119 + 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
  120 + 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
  121 + 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
  122 + 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  123 + 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
  124 + 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
  125 + 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
  126 + 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
  127 + 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
  128 + 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  129 + 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
  130 + 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
  131 + 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
  132 + 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
  133 + 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
  134 + 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  135 + 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
136 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 136 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
137 }; 137 };
138 138
@@ -159,7 +159,7 @@ static void error_treat(modbus_param_t *mb_param, int code, const char *string) @@ -159,7 +159,7 @@ static void error_treat(modbus_param_t *mb_param, int code, const char *string)
159 } 159 }
160 160
161 /* Computes the length of the expected response */ 161 /* Computes the length of the expected response */
162 -static unsigned int compute_response_length(modbus_param_t *mb_param, 162 +static unsigned int compute_response_length(modbus_param_t *mb_param,
163 uint8_t *query) 163 uint8_t *query)
164 { 164 {
165 int length; 165 int length;
@@ -178,7 +178,7 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, @@ -178,7 +178,7 @@ static unsigned int compute_response_length(modbus_param_t *mb_param,
178 case FC_READ_HOLDING_REGISTERS: 178 case FC_READ_HOLDING_REGISTERS:
179 case FC_READ_INPUT_REGISTERS: 179 case FC_READ_INPUT_REGISTERS:
180 /* Header + 2 * nb values */ 180 /* Header + 2 * nb values */
181 - length = 3 + 2 * (query[offset + 4] << 8 | 181 + length = 3 + 2 * (query[offset + 4] << 8 |
182 query[offset + 5]); 182 query[offset + 5]);
183 break; 183 break;
184 case FC_READ_EXCEPTION_STATUS: 184 case FC_READ_EXCEPTION_STATUS:
@@ -244,7 +244,7 @@ static int build_query_basis_tcp(int slave, int function, @@ -244,7 +244,7 @@ static int build_query_basis_tcp(int slave, int function,
244 return PRESET_QUERY_LENGTH_TCP; 244 return PRESET_QUERY_LENGTH_TCP;
245 } 245 }
246 246
247 -static int build_query_basis(modbus_param_t *mb_param, int slave, 247 +static int build_query_basis(modbus_param_t *mb_param, int slave,
248 int function, int start_addr, 248 int function, int start_addr,
249 int nb, uint8_t *query) 249 int nb, uint8_t *query)
250 { 250 {
@@ -287,7 +287,7 @@ static int build_response_basis_tcp(sft_t *sft, uint8_t *response) @@ -287,7 +287,7 @@ static int build_response_basis_tcp(sft_t *sft, uint8_t *response)
287 return PRESET_RESPONSE_LENGTH_TCP; 287 return PRESET_RESPONSE_LENGTH_TCP;
288 } 288 }
289 289
290 -static int build_response_basis(modbus_param_t *mb_param, sft_t *sft, 290 +static int build_response_basis(modbus_param_t *mb_param, sft_t *sft,
291 uint8_t *response) 291 uint8_t *response)
292 { 292 {
293 if (mb_param->type_com == RTU) 293 if (mb_param->type_com == RTU)
@@ -331,17 +331,17 @@ static int check_crc16(modbus_param_t *mb_param, @@ -331,17 +331,17 @@ static int check_crc16(modbus_param_t *mb_param,
331 int ret; 331 int ret;
332 uint16_t crc_calc; 332 uint16_t crc_calc;
333 uint16_t crc_received; 333 uint16_t crc_received;
334 - 334 +
335 crc_calc = crc16(msg, msg_length - 2); 335 crc_calc = crc16(msg, msg_length - 2);
336 crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1]; 336 crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
337 - 337 +
338 /* Check CRC of msg */ 338 /* Check CRC of msg */
339 if (crc_calc == crc_received) { 339 if (crc_calc == crc_received) {
340 ret = 0; 340 ret = 0;
341 } else { 341 } else {
342 char s_error[64]; 342 char s_error[64];
343 sprintf(s_error, 343 sprintf(s_error,
344 - "invalid crc received %0X - crc_calc %0X", 344 + "invalid crc received %0X - crc_calc %0X",
345 crc_received, crc_calc); 345 crc_received, crc_calc);
346 ret = INVALID_CRC; 346 ret = INVALID_CRC;
347 error_treat(mb_param, ret, s_error); 347 error_treat(mb_param, ret, s_error);
@@ -357,7 +357,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -357,7 +357,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
357 int ret; 357 int ret;
358 uint16_t s_crc; 358 uint16_t s_crc;
359 int i; 359 int i;
360 - 360 +
361 if (mb_param->type_com == RTU) { 361 if (mb_param->type_com == RTU) {
362 s_crc = crc16(query, query_length); 362 s_crc = crc16(query, query_length);
363 query[query_length++] = s_crc >> 8; 363 query[query_length++] = s_crc >> 8;
@@ -371,7 +371,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -371,7 +371,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
371 printf("[%.2X]", query[i]); 371 printf("[%.2X]", query[i]);
372 printf("\n"); 372 printf("\n");
373 } 373 }
374 - 374 +
375 if (mb_param->type_com == RTU) 375 if (mb_param->type_com == RTU)
376 ret = write(mb_param->fd, query, query_length); 376 ret = write(mb_param->fd, query, query_length);
377 else 377 else
@@ -383,7 +383,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -383,7 +383,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
383 ret = PORT_SOCKET_FAILURE; 383 ret = PORT_SOCKET_FAILURE;
384 error_treat(mb_param, ret, "Write port/socket failure"); 384 error_treat(mb_param, ret, "Write port/socket failure");
385 } 385 }
386 - 386 +
387 return ret; 387 return ret;
388 } 388 }
389 389
@@ -391,7 +391,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, @@ -391,7 +391,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query,
391 static uint8_t compute_query_length_header(int function) 391 static uint8_t compute_query_length_header(int function)
392 { 392 {
393 int length; 393 int length;
394 - 394 +
395 if (function <= FC_FORCE_SINGLE_COIL || 395 if (function <= FC_FORCE_SINGLE_COIL ||
396 function == FC_PRESET_SINGLE_REGISTER) 396 function == FC_PRESET_SINGLE_REGISTER)
397 /* Read and single write */ 397 /* Read and single write */
@@ -402,7 +402,7 @@ static uint8_t compute_query_length_header(int function) @@ -402,7 +402,7 @@ static uint8_t compute_query_length_header(int function)
402 length = 5; 402 length = 5;
403 else 403 else
404 length = 0; 404 length = 0;
405 - 405 +
406 return length; 406 return length;
407 } 407 }
408 408
@@ -411,7 +411,7 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg) @@ -411,7 +411,7 @@ static int compute_query_length_data(modbus_param_t *mb_param, uint8_t *msg)
411 { 411 {
412 int function = msg[mb_param->header_length + 1]; 412 int function = msg[mb_param->header_length + 1];
413 int length; 413 int length;
414 - 414 +
415 if (function == FC_FORCE_MULTIPLE_COILS || 415 if (function == FC_FORCE_MULTIPLE_COILS ||
416 function == FC_PRESET_MULTIPLE_REGISTERS) 416 function == FC_PRESET_MULTIPLE_REGISTERS)
417 length = msg[mb_param->header_length + 6]; 417 length = msg[mb_param->header_length + 6];
@@ -522,7 +522,7 @@ static int receive_msg(modbus_param_t *mb_param, @@ -522,7 +522,7 @@ static int receive_msg(modbus_param_t *mb_param,
522 "Read port/socket failure"); 522 "Read port/socket failure");
523 return PORT_SOCKET_FAILURE; 523 return PORT_SOCKET_FAILURE;
524 } 524 }
525 - 525 +
526 /* Sums bytes received */ 526 /* Sums bytes received */
527 (*p_msg_length) += read_ret; 527 (*p_msg_length) += read_ret;
528 528
@@ -552,7 +552,7 @@ static int receive_msg(modbus_param_t *mb_param, @@ -552,7 +552,7 @@ static int receive_msg(modbus_param_t *mb_param,
552 msg_length_computed += length_to_read; 552 msg_length_computed += length_to_read;
553 if (msg_length_computed > MAX_MESSAGE_LENGTH) { 553 if (msg_length_computed > MAX_MESSAGE_LENGTH) {
554 error_treat(mb_param, TOO_MANY_DATA, "Too many data"); 554 error_treat(mb_param, TOO_MANY_DATA, "Too many data");
555 - return TOO_MANY_DATA; 555 + return TOO_MANY_DATA;
556 } 556 }
557 state = COMPLETE; 557 state = COMPLETE;
558 break; 558 break;
@@ -570,21 +570,21 @@ static int receive_msg(modbus_param_t *mb_param, @@ -570,21 +570,21 @@ static int receive_msg(modbus_param_t *mb_param,
570 TIME_OUT_END_OF_TRAME before to generate an error. */ 570 TIME_OUT_END_OF_TRAME before to generate an error. */
571 tv.tv_sec = 0; 571 tv.tv_sec = 0;
572 tv.tv_usec = TIME_OUT_END_OF_TRAME; 572 tv.tv_usec = TIME_OUT_END_OF_TRAME;
573 - 573 +
574 WAIT_DATA(); 574 WAIT_DATA();
575 } else { 575 } else {
576 /* All chars are received */ 576 /* All chars are received */
577 select_ret = FALSE; 577 select_ret = FALSE;
578 } 578 }
579 } 579 }
580 - 580 +
581 if (mb_param->debug) 581 if (mb_param->debug)
582 printf("\n"); 582 printf("\n");
583 583
584 if (mb_param->type_com == RTU) { 584 if (mb_param->type_com == RTU) {
585 check_crc16(mb_param, msg, (*p_msg_length)); 585 check_crc16(mb_param, msg, (*p_msg_length));
586 } 586 }
587 - 587 +
588 /* OK */ 588 /* OK */
589 return 0; 589 return 0;
590 } 590 }
@@ -608,7 +608,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, @@ -608,7 +608,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
608 608
609 /* The length of the query to receive isn't known. */ 609 /* The length of the query to receive isn't known. */
610 ret = receive_msg(mb_param, MSG_LENGTH_UNDEFINED, query, query_length); 610 ret = receive_msg(mb_param, MSG_LENGTH_UNDEFINED, query, query_length);
611 - 611 +
612 return ret; 612 return ret;
613 } 613 }
614 614
@@ -621,7 +621,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, @@ -621,7 +621,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
621 621
622 Note: all functions used to send or receive data with modbus return 622 Note: all functions used to send or receive data with modbus return
623 these values. */ 623 these values. */
624 -static int modbus_receive(modbus_param_t *mb_param, 624 +static int modbus_receive(modbus_param_t *mb_param,
625 uint8_t *query, 625 uint8_t *query,
626 uint8_t *response) 626 uint8_t *response)
627 { 627 {
@@ -784,7 +784,7 @@ static int response_exception(modbus_param_t *mb_param, sft_t *sft, @@ -784,7 +784,7 @@ static int response_exception(modbus_param_t *mb_param, sft_t *sft,
784 */ 784 */
785 void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, 785 void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
786 int query_length, modbus_mapping_t *mb_mapping) 786 int query_length, modbus_mapping_t *mb_mapping)
787 -{ 787 +{
788 int offset = mb_param->header_length; 788 int offset = mb_param->header_length;
789 int slave = query[offset]; 789 int slave = query[offset];
790 int function = query[offset+1]; 790 int function = query[offset+1];
@@ -805,12 +805,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -805,12 +805,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
805 switch (function) { 805 switch (function) {
806 case FC_READ_COIL_STATUS: { 806 case FC_READ_COIL_STATUS: {
807 int nb = (query[offset+4] << 8) + query[offset+5]; 807 int nb = (query[offset+4] << 8) + query[offset+5];
808 - 808 +
809 if ((address + nb) > mb_mapping->nb_coil_status) { 809 if ((address + nb) > mb_mapping->nb_coil_status) {
810 printf("Illegal data address %0X in read_coil_status\n", 810 printf("Illegal data address %0X in read_coil_status\n",
811 - address + nb); 811 + address + nb);
812 resp_length = response_exception(mb_param, &sft, 812 resp_length = response_exception(mb_param, &sft,
813 - ILLEGAL_DATA_ADDRESS, response); 813 + ILLEGAL_DATA_ADDRESS, response);
814 } else { 814 } else {
815 resp_length = build_response_basis(mb_param, &sft, response); 815 resp_length = build_response_basis(mb_param, &sft, response);
816 response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0); 816 response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0);
@@ -827,7 +827,7 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -827,7 +827,7 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
827 827
828 if ((address + nb) > mb_mapping->nb_input_status) { 828 if ((address + nb) > mb_mapping->nb_input_status) {
829 printf("Illegal data address %0X in read_input_status\n", 829 printf("Illegal data address %0X in read_input_status\n",
830 - address + nb); 830 + address + nb);
831 resp_length = response_exception(mb_param, &sft, 831 resp_length = response_exception(mb_param, &sft,
832 ILLEGAL_DATA_ADDRESS, response); 832 ILLEGAL_DATA_ADDRESS, response);
833 } else { 833 } else {
@@ -841,15 +841,15 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -841,15 +841,15 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
841 break; 841 break;
842 case FC_READ_HOLDING_REGISTERS: { 842 case FC_READ_HOLDING_REGISTERS: {
843 int nb = (query[offset+4] << 8) + query[offset+5]; 843 int nb = (query[offset+4] << 8) + query[offset+5];
844 - 844 +
845 if ((address + nb) > mb_mapping->nb_holding_registers) { 845 if ((address + nb) > mb_mapping->nb_holding_registers) {
846 printf("Illegal data address %0X in read_holding_registers\n", 846 printf("Illegal data address %0X in read_holding_registers\n",
847 - address + nb); 847 + address + nb);
848 resp_length = response_exception(mb_param, &sft, 848 resp_length = response_exception(mb_param, &sft,
849 ILLEGAL_DATA_ADDRESS, response); 849 ILLEGAL_DATA_ADDRESS, response);
850 } else { 850 } else {
851 int i; 851 int i;
852 - 852 +
853 resp_length = build_response_basis(mb_param, &sft, response); 853 resp_length = build_response_basis(mb_param, &sft, response);
854 response[resp_length++] = nb << 1; 854 response[resp_length++] = nb << 1;
855 for (i = address; i < address + nb; i++) { 855 for (i = address; i < address + nb; i++) {
@@ -883,12 +883,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -883,12 +883,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
883 break; 883 break;
884 case FC_FORCE_SINGLE_COIL: 884 case FC_FORCE_SINGLE_COIL:
885 if (address >= mb_mapping->nb_coil_status) { 885 if (address >= mb_mapping->nb_coil_status) {
886 - printf("Illegal data address %0X in force_singe_coil\n", address); 886 + printf("Illegal data address %0X in force_singe_coil\n", address);
887 resp_length = response_exception(mb_param, &sft, 887 resp_length = response_exception(mb_param, &sft,
888 - ILLEGAL_DATA_ADDRESS, response); 888 + ILLEGAL_DATA_ADDRESS, response);
889 } else { 889 } else {
890 int data = (query[offset+4] << 8) + query[offset+5]; 890 int data = (query[offset+4] << 8) + query[offset+5];
891 - 891 +
892 if (data == 0xFF00 || data == 0x0) { 892 if (data == 0xFF00 || data == 0x0) {
893 mb_mapping->tab_coil_status[address] = (data) ? ON : OFF; 893 mb_mapping->tab_coil_status[address] = (data) ? ON : OFF;
894 894
@@ -905,15 +905,15 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -905,15 +905,15 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
905 ILLEGAL_DATA_VALUE, response); 905 ILLEGAL_DATA_VALUE, response);
906 } 906 }
907 } 907 }
908 - break; 908 + break;
909 case FC_PRESET_SINGLE_REGISTER: 909 case FC_PRESET_SINGLE_REGISTER:
910 if (address >= mb_mapping->nb_holding_registers) { 910 if (address >= mb_mapping->nb_holding_registers) {
911 - printf("Illegal data address %0X in preset_holding_register\n", address); 911 + printf("Illegal data address %0X in preset_holding_register\n", address);
912 resp_length = response_exception(mb_param, &sft, 912 resp_length = response_exception(mb_param, &sft,
913 - ILLEGAL_DATA_ADDRESS, response); 913 + ILLEGAL_DATA_ADDRESS, response);
914 } else { 914 } else {
915 int data = (query[offset+4] << 8) + query[offset+5]; 915 int data = (query[offset+4] << 8) + query[offset+5];
916 - 916 +
917 mb_mapping->tab_holding_registers[address] = data; 917 mb_mapping->tab_holding_registers[address] = data;
918 memcpy(response, query, query_length); 918 memcpy(response, query, query_length);
919 resp_length = query_length; 919 resp_length = query_length;
@@ -950,10 +950,10 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -950,10 +950,10 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
950 int i, j; 950 int i, j;
951 for (i = address, j = 0; i < address + nb; i++, j += 2) { 951 for (i = address, j = 0; i < address + nb; i++, j += 2) {
952 /* 6 = byte count, 7 and 8 = first value */ 952 /* 6 = byte count, 7 and 8 = first value */
953 - mb_mapping->tab_holding_registers[i] = 953 + mb_mapping->tab_holding_registers[i] =
954 (query[offset + 7 + j] << 8) + query[offset + 8 + j]; 954 (query[offset + 7 + j] << 8) + query[offset + 8 + j];
955 } 955 }
956 - 956 +
957 resp_length = build_response_basis(mb_param, &sft, response); 957 resp_length = build_response_basis(mb_param, &sft, response);
958 /* 4 to copy the address (2) and the no. of registers */ 958 /* 4 to copy the address (2) and the no. of registers */
959 memcpy(response + resp_length, query + resp_length, 4); 959 memcpy(response + resp_length, query + resp_length, 4);
@@ -980,7 +980,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, @@ -980,7 +980,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function,
980 uint8_t query[MIN_QUERY_LENGTH]; 980 uint8_t query[MIN_QUERY_LENGTH];
981 uint8_t response[MAX_MESSAGE_LENGTH]; 981 uint8_t response[MAX_MESSAGE_LENGTH];
982 982
983 - query_length = build_query_basis(mb_param, slave, function, 983 + query_length = build_query_basis(mb_param, slave, function,
984 start_addr, nb, query); 984 start_addr, nb, query);
985 985
986 ret = modbus_send(mb_param, query, query_length); 986 ret = modbus_send(mb_param, query, query_length);
@@ -996,16 +996,16 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, @@ -996,16 +996,16 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function,
996 996
997 offset = mb_param->header_length; 997 offset = mb_param->header_length;
998 998
999 - offset_length = offset + ret; 999 + offset_length = offset + ret;
1000 for (i = offset; i < offset_length; i++) { 1000 for (i = offset; i < offset_length; i++) {
1001 /* Shift reg hi_byte to temp */ 1001 /* Shift reg hi_byte to temp */
1002 temp = response[3 + i]; 1002 temp = response[3 + i];
1003 - 1003 +
1004 for (bit = 0x01; (bit & 0xff) && (pos < nb);) { 1004 for (bit = 0x01; (bit & 0xff) && (pos < nb);) {
1005 data_dest[pos++] = (temp & bit) ? TRUE : FALSE; 1005 data_dest[pos++] = (temp & bit) ? TRUE : FALSE;
1006 bit = bit << 1; 1006 bit = bit << 1;
1007 } 1007 }
1008 - 1008 +
1009 } 1009 }
1010 } 1010 }
1011 1011
@@ -1030,7 +1030,7 @@ int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr, @@ -1030,7 +1030,7 @@ int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr,
1030 1030
1031 if (status > 0) 1031 if (status > 0)
1032 status = nb; 1032 status = nb;
1033 - 1033 +
1034 return status; 1034 return status;
1035 } 1035 }
1036 1036
@@ -1071,7 +1071,7 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, @@ -1071,7 +1071,7 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function,
1071 return TOO_MANY_DATA; 1071 return TOO_MANY_DATA;
1072 } 1072 }
1073 1073
1074 - query_length = build_query_basis(mb_param, slave, function, 1074 + query_length = build_query_basis(mb_param, slave, function,
1075 start_addr, nb, query); 1075 start_addr, nb, query);
1076 1076
1077 ret = modbus_send(mb_param, query, query_length); 1077 ret = modbus_send(mb_param, query, query_length);
@@ -1080,17 +1080,17 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, @@ -1080,17 +1080,17 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function,
1080 int i; 1080 int i;
1081 1081
1082 ret = modbus_receive(mb_param, query, response); 1082 ret = modbus_receive(mb_param, query, response);
1083 - 1083 +
1084 offset = mb_param->header_length; 1084 offset = mb_param->header_length;
1085 1085
1086 /* If ret is negative, the loop is jumped ! */ 1086 /* If ret is negative, the loop is jumped ! */
1087 for (i = 0; i < ret; i++) { 1087 for (i = 0; i < ret; i++) {
1088 /* shift reg hi_byte to temp OR with lo_byte */ 1088 /* shift reg hi_byte to temp OR with lo_byte */
1089 - data_dest[i] = (response[offset + 3 + (i << 1)] << 8) |  
1090 - response[offset + 4 + (i << 1)]; 1089 + data_dest[i] = (response[offset + 3 + (i << 1)] << 8) |
  1090 + response[offset + 4 + (i << 1)];
1091 } 1091 }
1092 } 1092 }
1093 - 1093 +
1094 return ret; 1094 return ret;
1095 } 1095 }
1096 1096
@@ -1140,7 +1140,7 @@ static int set_single(modbus_param_t *mb_param, int slave, int function, @@ -1140,7 +1140,7 @@ static int set_single(modbus_param_t *mb_param, int slave, int function,
1140 int query_length; 1140 int query_length;
1141 uint8_t query[MIN_QUERY_LENGTH]; 1141 uint8_t query[MIN_QUERY_LENGTH];
1142 1142
1143 - query_length = build_query_basis(mb_param, slave, function, 1143 + query_length = build_query_basis(mb_param, slave, function,
1144 addr, value, query); 1144 addr, value, query);
1145 1145
1146 ret = modbus_send(mb_param, query, query_length); 1146 ret = modbus_send(mb_param, query, query_length);
@@ -1202,7 +1202,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, @@ -1202,7 +1202,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1202 } 1202 }
1203 1203
1204 query_length = build_query_basis(mb_param, slave, 1204 query_length = build_query_basis(mb_param, slave,
1205 - FC_FORCE_MULTIPLE_COILS, 1205 + FC_FORCE_MULTIPLE_COILS,
1206 start_addr, nb, query); 1206 start_addr, nb, query);
1207 byte_count = (nb / 8) + ((nb % 8) ? 1 : 0); 1207 byte_count = (nb / 8) + ((nb % 8) ? 1 : 0);
1208 query[query_length++] = byte_count; 1208 query[query_length++] = byte_count;
@@ -1218,7 +1218,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, @@ -1218,7 +1218,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1218 query[query_length] |= bit; 1218 query[query_length] |= bit;
1219 else 1219 else
1220 query[query_length] &=~ bit; 1220 query[query_length] &=~ bit;
1221 - 1221 +
1222 bit = bit << 1; 1222 bit = bit << 1;
1223 } 1223 }
1224 query_length++; 1224 query_length++;
@@ -1253,7 +1253,7 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, @@ -1253,7 +1253,7 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1253 } 1253 }
1254 1254
1255 query_length = build_query_basis(mb_param, slave, 1255 query_length = build_query_basis(mb_param, slave,
1256 - FC_PRESET_MULTIPLE_REGISTERS, 1256 + FC_PRESET_MULTIPLE_REGISTERS,
1257 start_addr, nb, query); 1257 start_addr, nb, query);
1258 byte_count = nb * 2; 1258 byte_count = nb * 2;
1259 query[query_length++] = byte_count; 1259 query[query_length++] = byte_count;
@@ -1273,19 +1273,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, @@ -1273,19 +1273,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1273 } 1273 }
1274 1274
1275 /* Returns the slave id! */ 1275 /* Returns the slave id! */
1276 -int report_slave_id(modbus_param_t *mb_param, int slave, 1276 +int report_slave_id(modbus_param_t *mb_param, int slave,
1277 uint8_t *data_dest) 1277 uint8_t *data_dest)
1278 { 1278 {
1279 int ret; 1279 int ret;
1280 int query_length; 1280 int query_length;
1281 uint8_t query[MIN_QUERY_LENGTH]; 1281 uint8_t query[MIN_QUERY_LENGTH];
1282 -  
1283 - query_length = build_query_basis(mb_param, slave, FC_REPORT_SLAVE_ID, 1282 +
  1283 + query_length = build_query_basis(mb_param, slave, FC_REPORT_SLAVE_ID,
1284 0, 0, query); 1284 0, 0, query);
1285 - 1285 +
1286 /* HACKISH, start_addr and count are not used */ 1286 /* HACKISH, start_addr and count are not used */
1287 query_length -= 4; 1287 query_length -= 4;
1288 - 1288 +
1289 ret = modbus_send(mb_param, query, query_length); 1289 ret = modbus_send(mb_param, query, query_length);
1290 if (ret > 0) { 1290 if (ret > 0) {
1291 int i; 1291 int i;
@@ -1312,8 +1312,8 @@ int report_slave_id(modbus_param_t *mb_param, int slave, @@ -1312,8 +1312,8 @@ int report_slave_id(modbus_param_t *mb_param, int slave,
1312 /* Initializes the modbus_param_t structure for RTU 1312 /* Initializes the modbus_param_t structure for RTU
1313 - device: "/dev/ttyS0" 1313 - device: "/dev/ttyS0"
1314 - baud: 9600, 19200, 57600, 115200, etc 1314 - baud: 9600, 19200, 57600, 115200, etc
1315 - - parity: "even", "odd" or "none"  
1316 - - data_bits: 5, 6, 7, 8 1315 + - parity: "even", "odd" or "none"
  1316 + - data_bits: 5, 6, 7, 8
1317 - stop_bits: 1, 2 1317 - stop_bits: 1, 2
1318 */ 1318 */
1319 void modbus_init_rtu(modbus_param_t *mb_param, const char *device, 1319 void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
@@ -1333,7 +1333,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, const char *device, @@ -1333,7 +1333,7 @@ void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
1333 } 1333 }
1334 1334
1335 /* Initializes the modbus_param_t structure for TCP. 1335 /* Initializes the modbus_param_t structure for TCP.
1336 - - ip : "192.168.0.5" 1336 + - ip : "192.168.0.5"
1337 - port : 1099 1337 - port : 1099
1338 1338
1339 Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one 1339 Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
@@ -1404,7 +1404,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1404,7 +1404,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1404 tcgetattr(mb_param->fd, &(mb_param->old_tios)); 1404 tcgetattr(mb_param->fd, &(mb_param->old_tios));
1405 1405
1406 memset(&tios, 0, sizeof(struct termios)); 1406 memset(&tios, 0, sizeof(struct termios));
1407 - 1407 +
1408 /* C_ISPEED Input baud (new interface) 1408 /* C_ISPEED Input baud (new interface)
1409 C_OSPEED Output baud (new interface) 1409 C_OSPEED Output baud (new interface)
1410 */ 1410 */
@@ -1427,7 +1427,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1427,7 +1427,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1427 case 4800: 1427 case 4800:
1428 speed = B4800; 1428 speed = B4800;
1429 break; 1429 break;
1430 - case 9600: 1430 + case 9600:
1431 speed = B9600; 1431 speed = B9600;
1432 break; 1432 break;
1433 case 19200: 1433 case 19200:
@@ -1454,7 +1454,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1454,7 +1454,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1454 perror("cfsetispeed/cfsetospeed\n"); 1454 perror("cfsetispeed/cfsetospeed\n");
1455 return -1; 1455 return -1;
1456 } 1456 }
1457 - 1457 +
1458 /* C_CFLAG Control options 1458 /* C_CFLAG Control options
1459 CLOCAL Local line - do not change "owner" of port 1459 CLOCAL Local line - do not change "owner" of port
1460 CREAD Enable receiver 1460 CREAD Enable receiver
@@ -1500,14 +1500,14 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1500,14 +1500,14 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1500 tios.c_cflag |= PARENB; 1500 tios.c_cflag |= PARENB;
1501 tios.c_cflag |= PARODD; 1501 tios.c_cflag |= PARODD;
1502 } 1502 }
1503 - 1503 +
1504 /* Read the man page of termios if you need more information. */ 1504 /* Read the man page of termios if you need more information. */
1505 1505
1506 - /* This field isn't used on POSIX systems  
1507 - tios.c_line = 0; 1506 + /* This field isn't used on POSIX systems
  1507 + tios.c_line = 0;
1508 */ 1508 */
1509 1509
1510 - /* C_LFLAG Line options 1510 + /* C_LFLAG Line options
1511 1511
1512 ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals 1512 ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
1513 ICANON Enable canonical input (else raw) 1513 ICANON Enable canonical input (else raw)
@@ -1529,7 +1529,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1529,7 +1529,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1529 Canonical input is line-oriented. Input characters are put 1529 Canonical input is line-oriented. Input characters are put
1530 into a buffer which can be edited interactively by the user 1530 into a buffer which can be edited interactively by the user
1531 until a CR (carriage return) or LF (line feed) character is 1531 until a CR (carriage return) or LF (line feed) character is
1532 - received. 1532 + received.
1533 1533
1534 Raw input is unprocessed. Input characters are passed 1534 Raw input is unprocessed. Input characters are passed
1535 through exactly as they are received, when they are 1535 through exactly as they are received, when they are
@@ -1540,7 +1540,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1540,7 +1540,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1540 /* Raw input */ 1540 /* Raw input */
1541 tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 1541 tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1542 1542
1543 - /* C_IFLAG Input options 1543 + /* C_IFLAG Input options
1544 1544
1545 Constant Description 1545 Constant Description
1546 INPCK Enable parity check 1546 INPCK Enable parity check
@@ -1566,18 +1566,18 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) @@ -1566,18 +1566,18 @@ static int modbus_connect_rtu(modbus_param_t *mb_param)
1566 1566
1567 /* Software flow control is disabled */ 1567 /* Software flow control is disabled */
1568 tios.c_iflag &= ~(IXON | IXOFF | IXANY); 1568 tios.c_iflag &= ~(IXON | IXOFF | IXANY);
1569 - 1569 +
1570 /* C_OFLAG Output options 1570 /* C_OFLAG Output options
1571 OPOST Postprocess output (not set = raw output) 1571 OPOST Postprocess output (not set = raw output)
1572 ONLCR Map NL to CR-NL 1572 ONLCR Map NL to CR-NL
1573 1573
1574 ONCLR ant others needs OPOST to be enabled 1574 ONCLR ant others needs OPOST to be enabled
1575 - */ 1575 + */
1576 1576
1577 /* Raw ouput */ 1577 /* Raw ouput */
1578 tios.c_oflag &=~ OPOST; 1578 tios.c_oflag &=~ OPOST;
1579 1579
1580 - /* C_CC Control characters 1580 + /* C_CC Control characters
1581 VMIN Minimum number of characters to read 1581 VMIN Minimum number of characters to read
1582 VTIME Time to wait for data (tenths of seconds) 1582 VTIME Time to wait for data (tenths of seconds)
1583 1583
@@ -1667,7 +1667,7 @@ static int modbus_connect_tcp(modbus_param_t *mb_param) @@ -1667,7 +1667,7 @@ static int modbus_connect_tcp(modbus_param_t *mb_param)
1667 1667
1668 addr.sin_family = AF_INET; 1668 addr.sin_family = AF_INET;
1669 addr.sin_port = htons(mb_param->port); 1669 addr.sin_port = htons(mb_param->port);
1670 - addr.sin_addr.s_addr = inet_addr(mb_param->ip); 1670 + addr.sin_addr.s_addr = inet_addr(mb_param->ip);
1671 ret = connect(mb_param->fd, (struct sockaddr *)&addr, 1671 ret = connect(mb_param->fd, (struct sockaddr *)&addr,
1672 sizeof(struct sockaddr_in)); 1672 sizeof(struct sockaddr_in));
1673 if (ret < 0) { 1673 if (ret < 0) {
@@ -1698,7 +1698,7 @@ static void modbus_close_rtu(modbus_param_t *mb_param) @@ -1698,7 +1698,7 @@ static void modbus_close_rtu(modbus_param_t *mb_param)
1698 { 1698 {
1699 if (tcsetattr(mb_param->fd, TCSANOW, &(mb_param->old_tios)) < 0) 1699 if (tcsetattr(mb_param->fd, TCSANOW, &(mb_param->old_tios)) < 0)
1700 perror("tcsetattr"); 1700 perror("tcsetattr");
1701 - 1701 +
1702 close(mb_param->fd); 1702 close(mb_param->fd);
1703 } 1703 }
1704 1704
@@ -1725,7 +1725,7 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean) @@ -1725,7 +1725,7 @@ void modbus_set_debug(modbus_param_t *mb_param, int boolean)
1725 } 1725 }
1726 1726
1727 /* Allocates 4 arrays to store coils, input status, input registers and 1727 /* Allocates 4 arrays to store coils, input status, input registers and
1728 - holding registers. The pointers are stored in modbus_mapping structure. 1728 + holding registers. The pointers are stored in modbus_mapping structure.
1729 1729
1730 Returns: TRUE if ok, FALSE on failure 1730 Returns: TRUE if ok, FALSE on failure
1731 */ 1731 */
@@ -1741,7 +1741,7 @@ int modbus_mapping_new(modbus_mapping_t *mb_mapping, @@ -1741,7 +1741,7 @@ int modbus_mapping_new(modbus_mapping_t *mb_mapping,
1741 nb_coil_status * sizeof(uint8_t)); 1741 nb_coil_status * sizeof(uint8_t));
1742 if (mb_mapping->tab_coil_status == NULL) 1742 if (mb_mapping->tab_coil_status == NULL)
1743 return FALSE; 1743 return FALSE;
1744 - 1744 +
1745 /* 1X */ 1745 /* 1X */
1746 mb_mapping->nb_input_status = nb_input_status; 1746 mb_mapping->nb_input_status = nb_input_status;
1747 mb_mapping->tab_input_status = 1747 mb_mapping->tab_input_status =
@@ -1835,7 +1835,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket) @@ -1835,7 +1835,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket)
1835 { 1835 {
1836 struct sockaddr_in addr; 1836 struct sockaddr_in addr;
1837 socklen_t addrlen; 1837 socklen_t addrlen;
1838 - 1838 +
1839 addrlen = sizeof(struct sockaddr_in); 1839 addrlen = sizeof(struct sockaddr_in);
1840 mb_param->fd = accept(*socket, (struct sockaddr *)&addr, &addrlen); 1840 mb_param->fd = accept(*socket, (struct sockaddr *)&addr, &addrlen);
1841 if (mb_param->fd < 0) { 1841 if (mb_param->fd < 0) {
@@ -1843,7 +1843,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket) @@ -1843,7 +1843,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket)
1843 close(*socket); 1843 close(*socket);
1844 *socket = 0; 1844 *socket = 0;
1845 } else { 1845 } else {
1846 - printf("The client %s is connected\n", 1846 + printf("The client %s is connected\n",
1847 inet_ntoa(addr.sin_addr)); 1847 inet_ntoa(addr.sin_addr));
1848 } 1848 }
1849 1849
@@ -1885,7 +1885,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) @@ -1885,7 +1885,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits)
1885 { 1885 {
1886 int i; 1886 int i;
1887 uint8_t value = 0; 1887 uint8_t value = 0;
1888 - 1888 +
1889 if (nb_bits > 8) { 1889 if (nb_bits > 8) {
1890 printf("Error: nb_bits is too big\n"); 1890 printf("Error: nb_bits is too big\n");
1891 nb_bits = 8; 1891 nb_bits = 8;
@@ -1894,6 +1894,6 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) @@ -1894,6 +1894,6 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits)
1894 for (i=0; i < nb_bits; i++) { 1894 for (i=0; i < nb_bits; i++) {
1895 value |= (src[address+i] << i); 1895 value |= (src[address+i] << i);
1896 } 1896 }
1897 - 1897 +
1898 return value; 1898 return value;
1899 } 1899 }
modbus/modbus.h
@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 24
25 #ifdef __cplusplus 25 #ifdef __cplusplus
26 extern "C" { 26 extern "C" {
27 -#endif 27 +#endif
28 28
29 #define MODBUS_TCP_DEFAULT_PORT 502 29 #define MODBUS_TCP_DEFAULT_PORT 502
30 30
@@ -37,7 +37,7 @@ extern &quot;C&quot; { @@ -37,7 +37,7 @@ extern &quot;C&quot; {
37 #define PRESET_RESPONSE_LENGTH_TCP 8 37 #define PRESET_RESPONSE_LENGTH_TCP 8
38 38
39 #define CHECKSUM_LENGTH_RTU 2 39 #define CHECKSUM_LENGTH_RTU 2
40 -#define CHECKSUM_LENGTH_TCP 0 40 +#define CHECKSUM_LENGTH_TCP 0
41 41
42 /* It's not really the minimal length (the real one is report slave ID 42 /* It's not really the minimal length (the real one is report slave ID
43 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP 43 * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP
@@ -50,7 +50,7 @@ extern &quot;C&quot; { @@ -50,7 +50,7 @@ extern &quot;C&quot; {
50 */ 50 */
51 #define MIN_QUERY_LENGTH 12 51 #define MIN_QUERY_LENGTH 12
52 52
53 -/* Page 102, Application Notes of PI–MBUS–300: 53 +/* Page 102, Application Notes of PI–MBUS–300:
54 * The maximum length of the entire message must not exceed 256 54 * The maximum length of the entire message must not exceed 256
55 * bytes. 55 * bytes.
56 */ 56 */
@@ -84,7 +84,7 @@ extern &quot;C&quot; { @@ -84,7 +84,7 @@ extern &quot;C&quot; {
84 /* Function codes */ 84 /* Function codes */
85 #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */ 85 #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */
86 #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */ 86 #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */
87 -#define FC_READ_HOLDING_REGISTERS 0x03 87 +#define FC_READ_HOLDING_REGISTERS 0x03
88 #define FC_READ_INPUT_REGISTERS 0x04 88 #define FC_READ_INPUT_REGISTERS 0x04
89 #define FC_FORCE_SINGLE_COIL 0x05 89 #define FC_FORCE_SINGLE_COIL 0x05
90 #define FC_PRESET_SINGLE_REGISTER 0x06 90 #define FC_PRESET_SINGLE_REGISTER 0x06
@@ -221,16 +221,16 @@ int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest); @@ -221,16 +221,16 @@ int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest);
221 /* Initializes the modbus_param_t structure for RTU. 221 /* Initializes the modbus_param_t structure for RTU.
222 - device: "/dev/ttyS0" 222 - device: "/dev/ttyS0"
223 - baud: 9600, 19200, 57600, 115200, etc 223 - baud: 9600, 19200, 57600, 115200, etc
224 - - parity: "even", "odd" or "none"  
225 - - data_bits: 5, 6, 7, 8 224 + - parity: "even", "odd" or "none"
  225 + - data_bits: 5, 6, 7, 8
226 - stop_bits: 1, 2 226 - stop_bits: 1, 2
227 */ 227 */
228 void modbus_init_rtu(modbus_param_t *mb_param, const char *device, 228 void modbus_init_rtu(modbus_param_t *mb_param, const char *device,
229 int baud, const char *parity, int data_bit, 229 int baud, const char *parity, int data_bit,
230 int stop_bit); 230 int stop_bit);
231 - 231 +
232 /* Initializes the modbus_param_t structure for TCP. 232 /* Initializes the modbus_param_t structure for TCP.
233 - - ip : "192.168.0.5" 233 + - ip : "192.168.0.5"
234 - port : 1099 234 - port : 1099
235 235
236 Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one 236 Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
@@ -264,11 +264,11 @@ void modbus_close(modbus_param_t *mb_param); @@ -264,11 +264,11 @@ void modbus_close(modbus_param_t *mb_param);
264 void modbus_set_debug(modbus_param_t *mb_param, int boolean); 264 void modbus_set_debug(modbus_param_t *mb_param, int boolean);
265 265
266 /** 266 /**
267 - * SLAVE/CLIENT FUNCTIONS 267 + * SLAVE/CLIENT FUNCTIONS
268 **/ 268 **/
269 269
270 /* Allocates 4 arrays to store coils, input status, input registers and 270 /* Allocates 4 arrays to store coils, input status, input registers and
271 - holding registers. The pointers are stored in modbus_mapping structure. 271 + holding registers. The pointers are stored in modbus_mapping structure.
272 272
273 Returns: TRUE if ok, FALSE on failure 273 Returns: TRUE if ok, FALSE on failure
274 */ 274 */
@@ -294,7 +294,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket); @@ -294,7 +294,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket);
294 Returns: 294 Returns:
295 - 0 if OK, or a negative error number if the request fails 295 - 0 if OK, or a negative error number if the request fails
296 - query, message received 296 - query, message received
297 - - query_length, length in bytes of the message 297 + - query_length, length in bytes of the message
298 */ 298 */
299 int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, 299 int modbus_slave_receive(modbus_param_t *mb_param, int sockfd,
300 uint8_t *query, int *query_length); 300 uint8_t *query, int *query_length);
@@ -309,7 +309,7 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, @@ -309,7 +309,7 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query,
309 int query_length, modbus_mapping_t *mb_mapping); 309 int query_length, modbus_mapping_t *mb_mapping);
310 310
311 311
312 -/** 312 +/**
313 * UTILS FUNCTIONS 313 * UTILS FUNCTIONS
314 **/ 314 **/
315 315
tests/bandwidth-slave-one.c
@@ -43,7 +43,7 @@ int main(void) @@ -43,7 +43,7 @@ int main(void)
43 while (1) { 43 while (1) {
44 uint8_t query[MAX_MESSAGE_LENGTH]; 44 uint8_t query[MAX_MESSAGE_LENGTH];
45 int query_size; 45 int query_size;
46 - 46 +
47 ret = modbus_slave_receive(&mb_param, -1, query, &query_size); 47 ret = modbus_slave_receive(&mb_param, -1, query, &query_size);
48 if (ret == 0) { 48 if (ret == 0) {
49 modbus_manage_query(&mb_param, query, query_size, &mb_mapping); 49 modbus_manage_query(&mb_param, query, query_size, &mb_mapping);
@@ -58,7 +58,6 @@ int main(void) @@ -58,7 +58,6 @@ int main(void)
58 close(socket); 58 close(socket);
59 modbus_mapping_free(&mb_mapping); 59 modbus_mapping_free(&mb_mapping);
60 modbus_close(&mb_param); 60 modbus_close(&mb_param);
61 - 61 +
62 return 0; 62 return 0;
63 } 63 }
64 -  
tests/random-test-master.c
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
33 - read_holding_registers 33 - read_holding_registers
34 34
35 All these functions are called with random values on a address 35 All these functions are called with random values on a address
36 - range defined by the following defines. 36 + range defined by the following defines.
37 */ 37 */
38 #define LOOP 1 38 #define LOOP 1
39 #define SLAVE 0x11 39 #define SLAVE 0x11
@@ -83,7 +83,7 @@ int main(void) @@ -83,7 +83,7 @@ int main(void)
83 memset(tab_rp_registers, 0, nb * sizeof(uint16_t)); 83 memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
84 84
85 nb_loop = nb_fail = 0; 85 nb_loop = nb_fail = 0;
86 - while (nb_loop++ < LOOP) { 86 + while (nb_loop++ < LOOP) {
87 for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { 87 for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) {
88 int i; 88 int i;
89 89
@@ -105,7 +105,7 @@ int main(void) @@ -105,7 +105,7 @@ int main(void)
105 ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status); 105 ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status);
106 if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) { 106 if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) {
107 printf("ERROR read_coil_status single (%d)\n", ret); 107 printf("ERROR read_coil_status single (%d)\n", ret);
108 - printf("Slave = %d, address = %d\n", 108 + printf("Slave = %d, address = %d\n",
109 SLAVE, addr); 109 SLAVE, addr);
110 nb_fail++; 110 nb_fail++;
111 } 111 }
@@ -129,7 +129,7 @@ int main(void) @@ -129,7 +129,7 @@ int main(void)
129 for (i=0; i<nb; i++) { 129 for (i=0; i<nb; i++) {
130 if (tab_rp_status[i] != tab_rq_status[i]) { 130 if (tab_rp_status[i] != tab_rq_status[i]) {
131 printf("ERROR read_coil_status\n"); 131 printf("ERROR read_coil_status\n");
132 - printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n", 132 + printf("Slave = %d, address = %d, value %d (0x%X) != %d (0x%X)\n",
133 SLAVE, addr, 133 SLAVE, addr,
134 tab_rq_status[i], tab_rq_status[i], 134 tab_rq_status[i], tab_rq_status[i],
135 tab_rp_status[i], tab_rp_status[i]); 135 tab_rp_status[i], tab_rp_status[i]);
@@ -165,7 +165,7 @@ int main(void) @@ -165,7 +165,7 @@ int main(void)
165 } 165 }
166 } 166 }
167 } 167 }
168 - 168 +
169 /* MULTIPLE REGISTERS */ 169 /* MULTIPLE REGISTERS */
170 ret = preset_multiple_registers(&mb_param, SLAVE, 170 ret = preset_multiple_registers(&mb_param, SLAVE,
171 addr, nb, tab_rq_registers); 171 addr, nb, tab_rq_registers);
@@ -197,7 +197,7 @@ int main(void) @@ -197,7 +197,7 @@ int main(void)
197 } 197 }
198 198
199 } 199 }
200 - 200 +
201 printf("Test: "); 201 printf("Test: ");
202 if (nb_fail) 202 if (nb_fail)
203 printf("%d FAILS\n", nb_fail); 203 printf("%d FAILS\n", nb_fail);
@@ -207,13 +207,12 @@ int main(void) @@ -207,13 +207,12 @@ int main(void)
207 207
208 /* Free the memory */ 208 /* Free the memory */
209 free(tab_rq_status); 209 free(tab_rq_status);
210 - free(tab_rp_status); 210 + free(tab_rp_status);
211 free(tab_rq_registers); 211 free(tab_rq_registers);
212 free(tab_rp_registers); 212 free(tab_rp_registers);
213 213
214 /* Close the connection */ 214 /* Close the connection */
215 modbus_close(&mb_param); 215 modbus_close(&mb_param);
216 - 216 +
217 return 0; 217 return 0;
218 } 218 }
219 -  
tests/random-test-slave.c
@@ -36,14 +36,14 @@ int main(void) @@ -36,14 +36,14 @@ int main(void)
36 printf("Memory allocation failed\n"); 36 printf("Memory allocation failed\n");
37 exit(1); 37 exit(1);
38 } 38 }
39 - 39 +
40 socket = modbus_slave_listen_tcp(&mb_param, 1); 40 socket = modbus_slave_listen_tcp(&mb_param, 1);
41 modbus_slave_accept_tcp(&mb_param, &socket); 41 modbus_slave_accept_tcp(&mb_param, &socket);
42 42
43 while (1) { 43 while (1) {
44 uint8_t query[MAX_MESSAGE_LENGTH]; 44 uint8_t query[MAX_MESSAGE_LENGTH];
45 int query_size; 45 int query_size;
46 - 46 +
47 ret = modbus_slave_receive(&mb_param, -1, query, &query_size); 47 ret = modbus_slave_receive(&mb_param, -1, query, &query_size);
48 if (ret == 0) { 48 if (ret == 0) {
49 modbus_manage_query(&mb_param, query, query_size, &mb_mapping); 49 modbus_manage_query(&mb_param, query, query_size, &mb_mapping);
@@ -58,7 +58,6 @@ int main(void) @@ -58,7 +58,6 @@ int main(void)
58 close(socket); 58 close(socket);
59 modbus_mapping_free(&mb_mapping); 59 modbus_mapping_free(&mb_mapping);
60 modbus_close(&mb_param); 60 modbus_close(&mb_param);
61 - 61 +
62 return 0; 62 return 0;
63 } 63 }
64 -  
tests/unit-test-master.c
@@ -44,7 +44,7 @@ int main(void) @@ -44,7 +44,7 @@ int main(void)
44 /* TCP */ 44 /* TCP */
45 modbus_init_tcp(&mb_param, "127.0.0.1", 1502); 45 modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
46 /* modbus_set_debug(&mb_param, TRUE);*/ 46 /* modbus_set_debug(&mb_param, TRUE);*/
47 - 47 +
48 if (modbus_connect(&mb_param) == -1) { 48 if (modbus_connect(&mb_param) == -1) {
49 printf("ERROR Connection failed\n"); 49 printf("ERROR Connection failed\n");
50 exit(1); 50 exit(1);
@@ -55,9 +55,9 @@ int main(void) @@ -55,9 +55,9 @@ int main(void)
55 UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS; 55 UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS;
56 tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t)); 56 tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t));
57 memset(tab_rp_status, 0, nb_points * sizeof(uint8_t)); 57 memset(tab_rp_status, 0, nb_points * sizeof(uint8_t));
58 - 58 +
59 /* Allocate and initialize the memory to store the registers */ 59 /* Allocate and initialize the memory to store the registers */
60 - nb_points = (UT_HOLDING_REGISTERS_NB_POINTS > 60 + nb_points = (UT_HOLDING_REGISTERS_NB_POINTS >
61 UT_INPUT_REGISTERS_NB_POINTS) ? 61 UT_INPUT_REGISTERS_NB_POINTS) ?
62 UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS; 62 UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS;
63 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); 63 tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t));
@@ -83,7 +83,7 @@ int main(void) @@ -83,7 +83,7 @@ int main(void)
83 tab_rp_status); 83 tab_rp_status);
84 printf("2/2 read_coil_status: "); 84 printf("2/2 read_coil_status: ");
85 if (ret != 1) { 85 if (ret != 1) {
86 - printf("FAILED (nb points %d)\n", ret); 86 + printf("FAILED (nb points %d)\n", ret);
87 goto close; 87 goto close;
88 } 88 }
89 89
@@ -97,7 +97,7 @@ int main(void) @@ -97,7 +97,7 @@ int main(void)
97 /* Multiple coils */ 97 /* Multiple coils */
98 { 98 {
99 uint8_t tab_value[UT_COIL_STATUS_NB_POINTS]; 99 uint8_t tab_value[UT_COIL_STATUS_NB_POINTS];
100 - 100 +
101 set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS, 101 set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS,
102 UT_COIL_STATUS_TAB); 102 UT_COIL_STATUS_TAB);
103 ret = force_multiple_coils(&mb_param, SLAVE, 103 ret = force_multiple_coils(&mb_param, SLAVE,
@@ -117,7 +117,7 @@ int main(void) @@ -117,7 +117,7 @@ int main(void)
117 UT_COIL_STATUS_NB_POINTS, tab_rp_status); 117 UT_COIL_STATUS_NB_POINTS, tab_rp_status);
118 printf("2/2 read_coil_status: "); 118 printf("2/2 read_coil_status: ");
119 if (ret != UT_COIL_STATUS_NB_POINTS) { 119 if (ret != UT_COIL_STATUS_NB_POINTS) {
120 - printf("FAILED (nb points %d)\n", ret); 120 + printf("FAILED (nb points %d)\n", ret);
121 goto close; 121 goto close;
122 } 122 }
123 123
@@ -146,7 +146,7 @@ int main(void) @@ -146,7 +146,7 @@ int main(void)
146 printf("1/1 read_input_status: "); 146 printf("1/1 read_input_status: ");
147 147
148 if (ret != UT_INPUT_STATUS_NB_POINTS) { 148 if (ret != UT_INPUT_STATUS_NB_POINTS) {
149 - printf("FAILED (nb points %d)\n", ret); 149 + printf("FAILED (nb points %d)\n", ret);
150 goto close; 150 goto close;
151 } 151 }
152 152
@@ -186,7 +186,7 @@ int main(void) @@ -186,7 +186,7 @@ int main(void)
186 1, tab_rp_registers); 186 1, tab_rp_registers);
187 printf("2/2 read_holding_registers: "); 187 printf("2/2 read_holding_registers: ");
188 if (ret != 1) { 188 if (ret != 1) {
189 - printf("FAILED (nb points %d)\n", ret); 189 + printf("FAILED (nb points %d)\n", ret);
190 goto close; 190 goto close;
191 } 191 }
192 192
@@ -217,7 +217,7 @@ int main(void) @@ -217,7 +217,7 @@ int main(void)
217 tab_rp_registers); 217 tab_rp_registers);
218 printf("2/2 read_holding_registers: "); 218 printf("2/2 read_holding_registers: ");
219 if (ret != UT_HOLDING_REGISTERS_NB_POINTS) { 219 if (ret != UT_HOLDING_REGISTERS_NB_POINTS) {
220 - printf("FAILED (nb points %d)\n", ret); 220 + printf("FAILED (nb points %d)\n", ret);
221 goto close; 221 goto close;
222 } 222 }
223 223
@@ -240,10 +240,10 @@ int main(void) @@ -240,10 +240,10 @@ int main(void)
240 tab_rp_registers); 240 tab_rp_registers);
241 printf("1/1 read_input_registers: "); 241 printf("1/1 read_input_registers: ");
242 if (ret != UT_INPUT_REGISTERS_NB_POINTS) { 242 if (ret != UT_INPUT_REGISTERS_NB_POINTS) {
243 - printf("FAILED (nb points %d)\n", ret); 243 + printf("FAILED (nb points %d)\n", ret);
244 goto close; 244 goto close;
245 } 245 }
246 - 246 +
247 for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) { 247 for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
248 if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) { 248 if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) {
249 printf("FAILED (%0X != %0X)\n", 249 printf("FAILED (%0X != %0X)\n",
@@ -252,14 +252,14 @@ int main(void) @@ -252,14 +252,14 @@ int main(void)
252 } 252 }
253 } 253 }
254 printf("OK\n"); 254 printf("OK\n");
255 - 255 +
256 256
257 /** ILLEGAL DATA ADDRESS **/ 257 /** ILLEGAL DATA ADDRESS **/
258 printf("\nTEST ILLEGAL DATA ADDRESS:\n"); 258 printf("\nTEST ILLEGAL DATA ADDRESS:\n");
259 259
260 /* The mapping begins at 0 and ending at address + nb_points so 260 /* The mapping begins at 0 and ending at address + nb_points so
261 - * the addresses below are not valid. */  
262 - 261 + * the addresses below are not valid. */
  262 +
263 ret = read_coil_status(&mb_param, SLAVE, 263 ret = read_coil_status(&mb_param, SLAVE,
264 UT_COIL_STATUS_ADDRESS, 264 UT_COIL_STATUS_ADDRESS,
265 UT_COIL_STATUS_NB_POINTS + 1, 265 UT_COIL_STATUS_NB_POINTS + 1,
@@ -289,7 +289,7 @@ int main(void) @@ -289,7 +289,7 @@ int main(void)
289 printf("OK\n"); 289 printf("OK\n");
290 else 290 else
291 printf("FAILED\n"); 291 printf("FAILED\n");
292 - 292 +
293 ret = read_input_registers(&mb_param, SLAVE, 293 ret = read_input_registers(&mb_param, SLAVE,
294 UT_INPUT_REGISTERS_ADDRESS, 294 UT_INPUT_REGISTERS_ADDRESS,
295 UT_INPUT_REGISTERS_NB_POINTS + 1, 295 UT_INPUT_REGISTERS_NB_POINTS + 1,
@@ -365,7 +365,7 @@ int main(void) @@ -365,7 +365,7 @@ int main(void)
365 printf("OK\n"); 365 printf("OK\n");
366 else 366 else
367 printf("FAILED\n"); 367 printf("FAILED\n");
368 - 368 +
369 ret = read_input_registers(&mb_param, SLAVE, 369 ret = read_input_registers(&mb_param, SLAVE,
370 UT_INPUT_REGISTERS_ADDRESS, 370 UT_INPUT_REGISTERS_ADDRESS,
371 MAX_REGISTERS + 1, 371 MAX_REGISTERS + 1,
@@ -419,11 +419,11 @@ int main(void) @@ -419,11 +419,11 @@ int main(void)
419 419
420 close: 420 close:
421 /* Free the memory */ 421 /* Free the memory */
422 - free(tab_rp_status); 422 + free(tab_rp_status);
423 free(tab_rp_registers); 423 free(tab_rp_registers);
424 424
425 /* Close the connection */ 425 /* Close the connection */
426 modbus_close(&mb_param); 426 modbus_close(&mb_param);
427 - 427 +
428 return 0; 428 return 0;
429 } 429 }
tests/unit-test-slave.c
@@ -47,7 +47,7 @@ int main(void) @@ -47,7 +47,7 @@ int main(void)
47 47
48 /* Examples from PI_MODBUS_300.pdf. 48 /* Examples from PI_MODBUS_300.pdf.
49 Only the read-only input values are assigned. */ 49 Only the read-only input values are assigned. */
50 - 50 +
51 /** INPUT STATUS **/ 51 /** INPUT STATUS **/
52 set_bits_from_bytes(mb_mapping.tab_input_status, 52 set_bits_from_bytes(mb_mapping.tab_input_status,
53 UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS, 53 UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS,
@@ -65,7 +65,7 @@ int main(void) @@ -65,7 +65,7 @@ int main(void)
65 while (1) { 65 while (1) {
66 uint8_t query[MAX_MESSAGE_LENGTH]; 66 uint8_t query[MAX_MESSAGE_LENGTH];
67 int query_size; 67 int query_size;
68 - 68 +
69 ret = modbus_slave_receive(&mb_param, -1, query, &query_size); 69 ret = modbus_slave_receive(&mb_param, -1, query, &query_size);
70 if (ret == 0) { 70 if (ret == 0) {
71 if (((query[HEADER_LENGTH_TCP + 4] << 8) + query[HEADER_LENGTH_TCP + 5]) 71 if (((query[HEADER_LENGTH_TCP + 4] << 8) + query[HEADER_LENGTH_TCP + 5])
@@ -88,7 +88,6 @@ int main(void) @@ -88,7 +88,6 @@ int main(void)
88 close(socket); 88 close(socket);
89 modbus_mapping_free(&mb_mapping); 89 modbus_mapping_free(&mb_mapping);
90 modbus_close(&mb_param); 90 modbus_close(&mb_param);
91 - 91 +
92 return 0; 92 return 0;
93 } 93 }
94 -