Commit d8a5e5558f7bfb6b8f31d19723aed9c1b3473fd2
1 parent
c7a6ffb3
Whitespace cleanup
Showing
7 changed files
with
177 additions
and
181 deletions
modbus/modbus.c
| ... | ... | @@ -22,7 +22,7 @@ |
| 22 | 22 | The function names used are inspired by the Modicon Modbus Protocol |
| 23 | 23 | Reference Guide which can be obtained from Schneider at |
| 24 | 24 | www.schneiderautomation.com. |
| 25 | - | |
| 25 | + | |
| 26 | 26 | Documentation: |
| 27 | 27 | http://www.easysw.com/~mike/serial/serial.html |
| 28 | 28 | http://copyleft.free.fr/wordpress/index.php/libmodbus/ |
| ... | ... | @@ -57,7 +57,7 @@ |
| 57 | 57 | typedef struct { |
| 58 | 58 | int slave; |
| 59 | 59 | int function; |
| 60 | - int t_id; | |
| 60 | + int t_id; | |
| 61 | 61 | } sft_t; |
| 62 | 62 | |
| 63 | 63 | static const uint8_t NB_TAB_ERROR_MSG = 12; |
| ... | ... | @@ -78,61 +78,61 @@ static const char *TAB_ERROR_MSG[] = { |
| 78 | 78 | |
| 79 | 79 | /* Table of CRC values for high-order byte */ |
| 80 | 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 | 106 | 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 |
| 107 | 107 | }; |
| 108 | 108 | |
| 109 | 109 | /* Table of CRC values for low-order byte */ |
| 110 | 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 | 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 | 159 | } |
| 160 | 160 | |
| 161 | 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 | 163 | uint8_t *query) |
| 164 | 164 | { |
| 165 | 165 | int length; |
| ... | ... | @@ -178,7 +178,7 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, |
| 178 | 178 | case FC_READ_HOLDING_REGISTERS: |
| 179 | 179 | case FC_READ_INPUT_REGISTERS: |
| 180 | 180 | /* Header + 2 * nb values */ |
| 181 | - length = 3 + 2 * (query[offset + 4] << 8 | | |
| 181 | + length = 3 + 2 * (query[offset + 4] << 8 | | |
| 182 | 182 | query[offset + 5]); |
| 183 | 183 | break; |
| 184 | 184 | case FC_READ_EXCEPTION_STATUS: |
| ... | ... | @@ -244,7 +244,7 @@ static int build_query_basis_tcp(int slave, int function, |
| 244 | 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 | 248 | int function, int start_addr, |
| 249 | 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 | 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 | 291 | uint8_t *response) |
| 292 | 292 | { |
| 293 | 293 | if (mb_param->type_com == RTU) |
| ... | ... | @@ -331,17 +331,17 @@ static int check_crc16(modbus_param_t *mb_param, |
| 331 | 331 | int ret; |
| 332 | 332 | uint16_t crc_calc; |
| 333 | 333 | uint16_t crc_received; |
| 334 | - | |
| 334 | + | |
| 335 | 335 | crc_calc = crc16(msg, msg_length - 2); |
| 336 | 336 | crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1]; |
| 337 | - | |
| 337 | + | |
| 338 | 338 | /* Check CRC of msg */ |
| 339 | 339 | if (crc_calc == crc_received) { |
| 340 | 340 | ret = 0; |
| 341 | 341 | } else { |
| 342 | 342 | char s_error[64]; |
| 343 | 343 | sprintf(s_error, |
| 344 | - "invalid crc received %0X - crc_calc %0X", | |
| 344 | + "invalid crc received %0X - crc_calc %0X", | |
| 345 | 345 | crc_received, crc_calc); |
| 346 | 346 | ret = INVALID_CRC; |
| 347 | 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 | 357 | int ret; |
| 358 | 358 | uint16_t s_crc; |
| 359 | 359 | int i; |
| 360 | - | |
| 360 | + | |
| 361 | 361 | if (mb_param->type_com == RTU) { |
| 362 | 362 | s_crc = crc16(query, query_length); |
| 363 | 363 | query[query_length++] = s_crc >> 8; |
| ... | ... | @@ -371,7 +371,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 371 | 371 | printf("[%.2X]", query[i]); |
| 372 | 372 | printf("\n"); |
| 373 | 373 | } |
| 374 | - | |
| 374 | + | |
| 375 | 375 | if (mb_param->type_com == RTU) |
| 376 | 376 | ret = write(mb_param->fd, query, query_length); |
| 377 | 377 | else |
| ... | ... | @@ -383,7 +383,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 383 | 383 | ret = PORT_SOCKET_FAILURE; |
| 384 | 384 | error_treat(mb_param, ret, "Write port/socket failure"); |
| 385 | 385 | } |
| 386 | - | |
| 386 | + | |
| 387 | 387 | return ret; |
| 388 | 388 | } |
| 389 | 389 | |
| ... | ... | @@ -391,7 +391,7 @@ static int modbus_send(modbus_param_t *mb_param, uint8_t *query, |
| 391 | 391 | static uint8_t compute_query_length_header(int function) |
| 392 | 392 | { |
| 393 | 393 | int length; |
| 394 | - | |
| 394 | + | |
| 395 | 395 | if (function <= FC_FORCE_SINGLE_COIL || |
| 396 | 396 | function == FC_PRESET_SINGLE_REGISTER) |
| 397 | 397 | /* Read and single write */ |
| ... | ... | @@ -402,7 +402,7 @@ static uint8_t compute_query_length_header(int function) |
| 402 | 402 | length = 5; |
| 403 | 403 | else |
| 404 | 404 | length = 0; |
| 405 | - | |
| 405 | + | |
| 406 | 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 | 411 | { |
| 412 | 412 | int function = msg[mb_param->header_length + 1]; |
| 413 | 413 | int length; |
| 414 | - | |
| 414 | + | |
| 415 | 415 | if (function == FC_FORCE_MULTIPLE_COILS || |
| 416 | 416 | function == FC_PRESET_MULTIPLE_REGISTERS) |
| 417 | 417 | length = msg[mb_param->header_length + 6]; |
| ... | ... | @@ -522,7 +522,7 @@ static int receive_msg(modbus_param_t *mb_param, |
| 522 | 522 | "Read port/socket failure"); |
| 523 | 523 | return PORT_SOCKET_FAILURE; |
| 524 | 524 | } |
| 525 | - | |
| 525 | + | |
| 526 | 526 | /* Sums bytes received */ |
| 527 | 527 | (*p_msg_length) += read_ret; |
| 528 | 528 | |
| ... | ... | @@ -552,7 +552,7 @@ static int receive_msg(modbus_param_t *mb_param, |
| 552 | 552 | msg_length_computed += length_to_read; |
| 553 | 553 | if (msg_length_computed > MAX_MESSAGE_LENGTH) { |
| 554 | 554 | error_treat(mb_param, TOO_MANY_DATA, "Too many data"); |
| 555 | - return TOO_MANY_DATA; | |
| 555 | + return TOO_MANY_DATA; | |
| 556 | 556 | } |
| 557 | 557 | state = COMPLETE; |
| 558 | 558 | break; |
| ... | ... | @@ -570,21 +570,21 @@ static int receive_msg(modbus_param_t *mb_param, |
| 570 | 570 | TIME_OUT_END_OF_TRAME before to generate an error. */ |
| 571 | 571 | tv.tv_sec = 0; |
| 572 | 572 | tv.tv_usec = TIME_OUT_END_OF_TRAME; |
| 573 | - | |
| 573 | + | |
| 574 | 574 | WAIT_DATA(); |
| 575 | 575 | } else { |
| 576 | 576 | /* All chars are received */ |
| 577 | 577 | select_ret = FALSE; |
| 578 | 578 | } |
| 579 | 579 | } |
| 580 | - | |
| 580 | + | |
| 581 | 581 | if (mb_param->debug) |
| 582 | 582 | printf("\n"); |
| 583 | 583 | |
| 584 | 584 | if (mb_param->type_com == RTU) { |
| 585 | 585 | check_crc16(mb_param, msg, (*p_msg_length)); |
| 586 | 586 | } |
| 587 | - | |
| 587 | + | |
| 588 | 588 | /* OK */ |
| 589 | 589 | return 0; |
| 590 | 590 | } |
| ... | ... | @@ -608,7 +608,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, |
| 608 | 608 | |
| 609 | 609 | /* The length of the query to receive isn't known. */ |
| 610 | 610 | ret = receive_msg(mb_param, MSG_LENGTH_UNDEFINED, query, query_length); |
| 611 | - | |
| 611 | + | |
| 612 | 612 | return ret; |
| 613 | 613 | } |
| 614 | 614 | |
| ... | ... | @@ -621,7 +621,7 @@ int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, |
| 621 | 621 | |
| 622 | 622 | Note: all functions used to send or receive data with modbus return |
| 623 | 623 | these values. */ |
| 624 | -static int modbus_receive(modbus_param_t *mb_param, | |
| 624 | +static int modbus_receive(modbus_param_t *mb_param, | |
| 625 | 625 | uint8_t *query, |
| 626 | 626 | uint8_t *response) |
| 627 | 627 | { |
| ... | ... | @@ -784,7 +784,7 @@ static int response_exception(modbus_param_t *mb_param, sft_t *sft, |
| 784 | 784 | */ |
| 785 | 785 | void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 786 | 786 | int query_length, modbus_mapping_t *mb_mapping) |
| 787 | -{ | |
| 787 | +{ | |
| 788 | 788 | int offset = mb_param->header_length; |
| 789 | 789 | int slave = query[offset]; |
| 790 | 790 | int function = query[offset+1]; |
| ... | ... | @@ -805,12 +805,12 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 805 | 805 | switch (function) { |
| 806 | 806 | case FC_READ_COIL_STATUS: { |
| 807 | 807 | int nb = (query[offset+4] << 8) + query[offset+5]; |
| 808 | - | |
| 808 | + | |
| 809 | 809 | if ((address + nb) > mb_mapping->nb_coil_status) { |
| 810 | 810 | printf("Illegal data address %0X in read_coil_status\n", |
| 811 | - address + nb); | |
| 811 | + address + nb); | |
| 812 | 812 | resp_length = response_exception(mb_param, &sft, |
| 813 | - ILLEGAL_DATA_ADDRESS, response); | |
| 813 | + ILLEGAL_DATA_ADDRESS, response); | |
| 814 | 814 | } else { |
| 815 | 815 | resp_length = build_response_basis(mb_param, &sft, response); |
| 816 | 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 | 827 | |
| 828 | 828 | if ((address + nb) > mb_mapping->nb_input_status) { |
| 829 | 829 | printf("Illegal data address %0X in read_input_status\n", |
| 830 | - address + nb); | |
| 830 | + address + nb); | |
| 831 | 831 | resp_length = response_exception(mb_param, &sft, |
| 832 | 832 | ILLEGAL_DATA_ADDRESS, response); |
| 833 | 833 | } else { |
| ... | ... | @@ -841,15 +841,15 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 841 | 841 | break; |
| 842 | 842 | case FC_READ_HOLDING_REGISTERS: { |
| 843 | 843 | int nb = (query[offset+4] << 8) + query[offset+5]; |
| 844 | - | |
| 844 | + | |
| 845 | 845 | if ((address + nb) > mb_mapping->nb_holding_registers) { |
| 846 | 846 | printf("Illegal data address %0X in read_holding_registers\n", |
| 847 | - address + nb); | |
| 847 | + address + nb); | |
| 848 | 848 | resp_length = response_exception(mb_param, &sft, |
| 849 | 849 | ILLEGAL_DATA_ADDRESS, response); |
| 850 | 850 | } else { |
| 851 | 851 | int i; |
| 852 | - | |
| 852 | + | |
| 853 | 853 | resp_length = build_response_basis(mb_param, &sft, response); |
| 854 | 854 | response[resp_length++] = nb << 1; |
| 855 | 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 | 883 | break; |
| 884 | 884 | case FC_FORCE_SINGLE_COIL: |
| 885 | 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 | 887 | resp_length = response_exception(mb_param, &sft, |
| 888 | - ILLEGAL_DATA_ADDRESS, response); | |
| 888 | + ILLEGAL_DATA_ADDRESS, response); | |
| 889 | 889 | } else { |
| 890 | 890 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 891 | - | |
| 891 | + | |
| 892 | 892 | if (data == 0xFF00 || data == 0x0) { |
| 893 | 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 | 905 | ILLEGAL_DATA_VALUE, response); |
| 906 | 906 | } |
| 907 | 907 | } |
| 908 | - break; | |
| 908 | + break; | |
| 909 | 909 | case FC_PRESET_SINGLE_REGISTER: |
| 910 | 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 | 912 | resp_length = response_exception(mb_param, &sft, |
| 913 | - ILLEGAL_DATA_ADDRESS, response); | |
| 913 | + ILLEGAL_DATA_ADDRESS, response); | |
| 914 | 914 | } else { |
| 915 | 915 | int data = (query[offset+4] << 8) + query[offset+5]; |
| 916 | - | |
| 916 | + | |
| 917 | 917 | mb_mapping->tab_holding_registers[address] = data; |
| 918 | 918 | memcpy(response, query, query_length); |
| 919 | 919 | resp_length = query_length; |
| ... | ... | @@ -950,10 +950,10 @@ void modbus_manage_query(modbus_param_t *mb_param, const uint8_t *query, |
| 950 | 950 | int i, j; |
| 951 | 951 | for (i = address, j = 0; i < address + nb; i++, j += 2) { |
| 952 | 952 | /* 6 = byte count, 7 and 8 = first value */ |
| 953 | - mb_mapping->tab_holding_registers[i] = | |
| 953 | + mb_mapping->tab_holding_registers[i] = | |
| 954 | 954 | (query[offset + 7 + j] << 8) + query[offset + 8 + j]; |
| 955 | 955 | } |
| 956 | - | |
| 956 | + | |
| 957 | 957 | resp_length = build_response_basis(mb_param, &sft, response); |
| 958 | 958 | /* 4 to copy the address (2) and the no. of registers */ |
| 959 | 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 | 980 | uint8_t query[MIN_QUERY_LENGTH]; |
| 981 | 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 | 984 | start_addr, nb, query); |
| 985 | 985 | |
| 986 | 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 | 996 | |
| 997 | 997 | offset = mb_param->header_length; |
| 998 | 998 | |
| 999 | - offset_length = offset + ret; | |
| 999 | + offset_length = offset + ret; | |
| 1000 | 1000 | for (i = offset; i < offset_length; i++) { |
| 1001 | 1001 | /* Shift reg hi_byte to temp */ |
| 1002 | 1002 | temp = response[3 + i]; |
| 1003 | - | |
| 1003 | + | |
| 1004 | 1004 | for (bit = 0x01; (bit & 0xff) && (pos < nb);) { |
| 1005 | 1005 | data_dest[pos++] = (temp & bit) ? TRUE : FALSE; |
| 1006 | 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 | 1030 | |
| 1031 | 1031 | if (status > 0) |
| 1032 | 1032 | status = nb; |
| 1033 | - | |
| 1033 | + | |
| 1034 | 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 | 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 | 1075 | start_addr, nb, query); |
| 1076 | 1076 | |
| 1077 | 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 | 1080 | int i; |
| 1081 | 1081 | |
| 1082 | 1082 | ret = modbus_receive(mb_param, query, response); |
| 1083 | - | |
| 1083 | + | |
| 1084 | 1084 | offset = mb_param->header_length; |
| 1085 | 1085 | |
| 1086 | 1086 | /* If ret is negative, the loop is jumped ! */ |
| 1087 | 1087 | for (i = 0; i < ret; i++) { |
| 1088 | 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 | 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 | 1140 | int query_length; |
| 1141 | 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 | 1144 | addr, value, query); |
| 1145 | 1145 | |
| 1146 | 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 | 1202 | } |
| 1203 | 1203 | |
| 1204 | 1204 | query_length = build_query_basis(mb_param, slave, |
| 1205 | - FC_FORCE_MULTIPLE_COILS, | |
| 1205 | + FC_FORCE_MULTIPLE_COILS, | |
| 1206 | 1206 | start_addr, nb, query); |
| 1207 | 1207 | byte_count = (nb / 8) + ((nb % 8) ? 1 : 0); |
| 1208 | 1208 | query[query_length++] = byte_count; |
| ... | ... | @@ -1218,7 +1218,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, |
| 1218 | 1218 | query[query_length] |= bit; |
| 1219 | 1219 | else |
| 1220 | 1220 | query[query_length] &=~ bit; |
| 1221 | - | |
| 1221 | + | |
| 1222 | 1222 | bit = bit << 1; |
| 1223 | 1223 | } |
| 1224 | 1224 | query_length++; |
| ... | ... | @@ -1253,7 +1253,7 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 1253 | 1253 | } |
| 1254 | 1254 | |
| 1255 | 1255 | query_length = build_query_basis(mb_param, slave, |
| 1256 | - FC_PRESET_MULTIPLE_REGISTERS, | |
| 1256 | + FC_PRESET_MULTIPLE_REGISTERS, | |
| 1257 | 1257 | start_addr, nb, query); |
| 1258 | 1258 | byte_count = nb * 2; |
| 1259 | 1259 | query[query_length++] = byte_count; |
| ... | ... | @@ -1273,19 +1273,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 1273 | 1273 | } |
| 1274 | 1274 | |
| 1275 | 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 | 1277 | uint8_t *data_dest) |
| 1278 | 1278 | { |
| 1279 | 1279 | int ret; |
| 1280 | 1280 | int query_length; |
| 1281 | 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 | 1284 | 0, 0, query); |
| 1285 | - | |
| 1285 | + | |
| 1286 | 1286 | /* HACKISH, start_addr and count are not used */ |
| 1287 | 1287 | query_length -= 4; |
| 1288 | - | |
| 1288 | + | |
| 1289 | 1289 | ret = modbus_send(mb_param, query, query_length); |
| 1290 | 1290 | if (ret > 0) { |
| 1291 | 1291 | int i; |
| ... | ... | @@ -1312,8 +1312,8 @@ int report_slave_id(modbus_param_t *mb_param, int slave, |
| 1312 | 1312 | /* Initializes the modbus_param_t structure for RTU |
| 1313 | 1313 | - device: "/dev/ttyS0" |
| 1314 | 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 | 1317 | - stop_bits: 1, 2 |
| 1318 | 1318 | */ |
| 1319 | 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 | 1333 | } |
| 1334 | 1334 | |
| 1335 | 1335 | /* Initializes the modbus_param_t structure for TCP. |
| 1336 | - - ip : "192.168.0.5" | |
| 1336 | + - ip : "192.168.0.5" | |
| 1337 | 1337 | - port : 1099 |
| 1338 | 1338 | |
| 1339 | 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 | 1404 | tcgetattr(mb_param->fd, &(mb_param->old_tios)); |
| 1405 | 1405 | |
| 1406 | 1406 | memset(&tios, 0, sizeof(struct termios)); |
| 1407 | - | |
| 1407 | + | |
| 1408 | 1408 | /* C_ISPEED Input baud (new interface) |
| 1409 | 1409 | C_OSPEED Output baud (new interface) |
| 1410 | 1410 | */ |
| ... | ... | @@ -1427,7 +1427,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1427 | 1427 | case 4800: |
| 1428 | 1428 | speed = B4800; |
| 1429 | 1429 | break; |
| 1430 | - case 9600: | |
| 1430 | + case 9600: | |
| 1431 | 1431 | speed = B9600; |
| 1432 | 1432 | break; |
| 1433 | 1433 | case 19200: |
| ... | ... | @@ -1454,7 +1454,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1454 | 1454 | perror("cfsetispeed/cfsetospeed\n"); |
| 1455 | 1455 | return -1; |
| 1456 | 1456 | } |
| 1457 | - | |
| 1457 | + | |
| 1458 | 1458 | /* C_CFLAG Control options |
| 1459 | 1459 | CLOCAL Local line - do not change "owner" of port |
| 1460 | 1460 | CREAD Enable receiver |
| ... | ... | @@ -1500,14 +1500,14 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1500 | 1500 | tios.c_cflag |= PARENB; |
| 1501 | 1501 | tios.c_cflag |= PARODD; |
| 1502 | 1502 | } |
| 1503 | - | |
| 1503 | + | |
| 1504 | 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 | 1512 | ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals |
| 1513 | 1513 | ICANON Enable canonical input (else raw) |
| ... | ... | @@ -1529,7 +1529,7 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1529 | 1529 | Canonical input is line-oriented. Input characters are put |
| 1530 | 1530 | into a buffer which can be edited interactively by the user |
| 1531 | 1531 | until a CR (carriage return) or LF (line feed) character is |
| 1532 | - received. | |
| 1532 | + received. | |
| 1533 | 1533 | |
| 1534 | 1534 | Raw input is unprocessed. Input characters are passed |
| 1535 | 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 | 1540 | /* Raw input */ |
| 1541 | 1541 | tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 1542 | 1542 | |
| 1543 | - /* C_IFLAG Input options | |
| 1543 | + /* C_IFLAG Input options | |
| 1544 | 1544 | |
| 1545 | 1545 | Constant Description |
| 1546 | 1546 | INPCK Enable parity check |
| ... | ... | @@ -1566,18 +1566,18 @@ static int modbus_connect_rtu(modbus_param_t *mb_param) |
| 1566 | 1566 | |
| 1567 | 1567 | /* Software flow control is disabled */ |
| 1568 | 1568 | tios.c_iflag &= ~(IXON | IXOFF | IXANY); |
| 1569 | - | |
| 1569 | + | |
| 1570 | 1570 | /* C_OFLAG Output options |
| 1571 | 1571 | OPOST Postprocess output (not set = raw output) |
| 1572 | 1572 | ONLCR Map NL to CR-NL |
| 1573 | 1573 | |
| 1574 | 1574 | ONCLR ant others needs OPOST to be enabled |
| 1575 | - */ | |
| 1575 | + */ | |
| 1576 | 1576 | |
| 1577 | 1577 | /* Raw ouput */ |
| 1578 | 1578 | tios.c_oflag &=~ OPOST; |
| 1579 | 1579 | |
| 1580 | - /* C_CC Control characters | |
| 1580 | + /* C_CC Control characters | |
| 1581 | 1581 | VMIN Minimum number of characters to read |
| 1582 | 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 | 1667 | |
| 1668 | 1668 | addr.sin_family = AF_INET; |
| 1669 | 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 | 1671 | ret = connect(mb_param->fd, (struct sockaddr *)&addr, |
| 1672 | 1672 | sizeof(struct sockaddr_in)); |
| 1673 | 1673 | if (ret < 0) { |
| ... | ... | @@ -1698,7 +1698,7 @@ static void modbus_close_rtu(modbus_param_t *mb_param) |
| 1698 | 1698 | { |
| 1699 | 1699 | if (tcsetattr(mb_param->fd, TCSANOW, &(mb_param->old_tios)) < 0) |
| 1700 | 1700 | perror("tcsetattr"); |
| 1701 | - | |
| 1701 | + | |
| 1702 | 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 | 1725 | } |
| 1726 | 1726 | |
| 1727 | 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 | 1730 | Returns: TRUE if ok, FALSE on failure |
| 1731 | 1731 | */ |
| ... | ... | @@ -1741,7 +1741,7 @@ int modbus_mapping_new(modbus_mapping_t *mb_mapping, |
| 1741 | 1741 | nb_coil_status * sizeof(uint8_t)); |
| 1742 | 1742 | if (mb_mapping->tab_coil_status == NULL) |
| 1743 | 1743 | return FALSE; |
| 1744 | - | |
| 1744 | + | |
| 1745 | 1745 | /* 1X */ |
| 1746 | 1746 | mb_mapping->nb_input_status = nb_input_status; |
| 1747 | 1747 | mb_mapping->tab_input_status = |
| ... | ... | @@ -1835,7 +1835,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket) |
| 1835 | 1835 | { |
| 1836 | 1836 | struct sockaddr_in addr; |
| 1837 | 1837 | socklen_t addrlen; |
| 1838 | - | |
| 1838 | + | |
| 1839 | 1839 | addrlen = sizeof(struct sockaddr_in); |
| 1840 | 1840 | mb_param->fd = accept(*socket, (struct sockaddr *)&addr, &addrlen); |
| 1841 | 1841 | if (mb_param->fd < 0) { |
| ... | ... | @@ -1843,7 +1843,7 @@ int modbus_slave_accept_tcp(modbus_param_t *mb_param, int *socket) |
| 1843 | 1843 | close(*socket); |
| 1844 | 1844 | *socket = 0; |
| 1845 | 1845 | } else { |
| 1846 | - printf("The client %s is connected\n", | |
| 1846 | + printf("The client %s is connected\n", | |
| 1847 | 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 | 1885 | { |
| 1886 | 1886 | int i; |
| 1887 | 1887 | uint8_t value = 0; |
| 1888 | - | |
| 1888 | + | |
| 1889 | 1889 | if (nb_bits > 8) { |
| 1890 | 1890 | printf("Error: nb_bits is too big\n"); |
| 1891 | 1891 | nb_bits = 8; |
| ... | ... | @@ -1894,6 +1894,6 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) |
| 1894 | 1894 | for (i=0; i < nb_bits; i++) { |
| 1895 | 1895 | value |= (src[address+i] << i); |
| 1896 | 1896 | } |
| 1897 | - | |
| 1897 | + | |
| 1898 | 1898 | return value; |
| 1899 | 1899 | } | ... | ... |
modbus/modbus.h
| ... | ... | @@ -24,7 +24,7 @@ |
| 24 | 24 | |
| 25 | 25 | #ifdef __cplusplus |
| 26 | 26 | extern "C" { |
| 27 | -#endif | |
| 27 | +#endif | |
| 28 | 28 | |
| 29 | 29 | #define MODBUS_TCP_DEFAULT_PORT 502 |
| 30 | 30 | |
| ... | ... | @@ -37,7 +37,7 @@ extern "C" { |
| 37 | 37 | #define PRESET_RESPONSE_LENGTH_TCP 8 |
| 38 | 38 | |
| 39 | 39 | #define CHECKSUM_LENGTH_RTU 2 |
| 40 | -#define CHECKSUM_LENGTH_TCP 0 | |
| 40 | +#define CHECKSUM_LENGTH_TCP 0 | |
| 41 | 41 | |
| 42 | 42 | /* It's not really the minimal length (the real one is report slave ID |
| 43 | 43 | * in RTU (4 bytes)) but it's a convenient size to use in RTU or TCP |
| ... | ... | @@ -50,7 +50,7 @@ extern "C" { |
| 50 | 50 | */ |
| 51 | 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 | 54 | * The maximum length of the entire message must not exceed 256 |
| 55 | 55 | * bytes. |
| 56 | 56 | */ |
| ... | ... | @@ -84,7 +84,7 @@ extern "C" { |
| 84 | 84 | /* Function codes */ |
| 85 | 85 | #define FC_READ_COIL_STATUS 0x01 /* discretes inputs */ |
| 86 | 86 | #define FC_READ_INPUT_STATUS 0x02 /* discretes outputs */ |
| 87 | -#define FC_READ_HOLDING_REGISTERS 0x03 | |
| 87 | +#define FC_READ_HOLDING_REGISTERS 0x03 | |
| 88 | 88 | #define FC_READ_INPUT_REGISTERS 0x04 |
| 89 | 89 | #define FC_FORCE_SINGLE_COIL 0x05 |
| 90 | 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 | 221 | /* Initializes the modbus_param_t structure for RTU. |
| 222 | 222 | - device: "/dev/ttyS0" |
| 223 | 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 | 226 | - stop_bits: 1, 2 |
| 227 | 227 | */ |
| 228 | 228 | void modbus_init_rtu(modbus_param_t *mb_param, const char *device, |
| 229 | 229 | int baud, const char *parity, int data_bit, |
| 230 | 230 | int stop_bit); |
| 231 | - | |
| 231 | + | |
| 232 | 232 | /* Initializes the modbus_param_t structure for TCP. |
| 233 | - - ip : "192.168.0.5" | |
| 233 | + - ip : "192.168.0.5" | |
| 234 | 234 | - port : 1099 |
| 235 | 235 | |
| 236 | 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 | 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 | 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 | 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 | 294 | Returns: |
| 295 | 295 | - 0 if OK, or a negative error number if the request fails |
| 296 | 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 | 299 | int modbus_slave_receive(modbus_param_t *mb_param, int sockfd, |
| 300 | 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 | 309 | int query_length, modbus_mapping_t *mb_mapping); |
| 310 | 310 | |
| 311 | 311 | |
| 312 | -/** | |
| 312 | +/** | |
| 313 | 313 | * UTILS FUNCTIONS |
| 314 | 314 | **/ |
| 315 | 315 | ... | ... |
tests/bandwidth-slave-one.c
| ... | ... | @@ -43,7 +43,7 @@ int main(void) |
| 43 | 43 | while (1) { |
| 44 | 44 | uint8_t query[MAX_MESSAGE_LENGTH]; |
| 45 | 45 | int query_size; |
| 46 | - | |
| 46 | + | |
| 47 | 47 | ret = modbus_slave_receive(&mb_param, -1, query, &query_size); |
| 48 | 48 | if (ret == 0) { |
| 49 | 49 | modbus_manage_query(&mb_param, query, query_size, &mb_mapping); |
| ... | ... | @@ -58,7 +58,6 @@ int main(void) |
| 58 | 58 | close(socket); |
| 59 | 59 | modbus_mapping_free(&mb_mapping); |
| 60 | 60 | modbus_close(&mb_param); |
| 61 | - | |
| 61 | + | |
| 62 | 62 | return 0; |
| 63 | 63 | } |
| 64 | - | ... | ... |
tests/random-test-master.c
| ... | ... | @@ -33,7 +33,7 @@ |
| 33 | 33 | - read_holding_registers |
| 34 | 34 | |
| 35 | 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 | 38 | #define LOOP 1 |
| 39 | 39 | #define SLAVE 0x11 |
| ... | ... | @@ -83,7 +83,7 @@ int main(void) |
| 83 | 83 | memset(tab_rp_registers, 0, nb * sizeof(uint16_t)); |
| 84 | 84 | |
| 85 | 85 | nb_loop = nb_fail = 0; |
| 86 | - while (nb_loop++ < LOOP) { | |
| 86 | + while (nb_loop++ < LOOP) { | |
| 87 | 87 | for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { |
| 88 | 88 | int i; |
| 89 | 89 | |
| ... | ... | @@ -105,7 +105,7 @@ int main(void) |
| 105 | 105 | ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status); |
| 106 | 106 | if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) { |
| 107 | 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 | 109 | SLAVE, addr); |
| 110 | 110 | nb_fail++; |
| 111 | 111 | } |
| ... | ... | @@ -129,7 +129,7 @@ int main(void) |
| 129 | 129 | for (i=0; i<nb; i++) { |
| 130 | 130 | if (tab_rp_status[i] != tab_rq_status[i]) { |
| 131 | 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 | 133 | SLAVE, addr, |
| 134 | 134 | tab_rq_status[i], tab_rq_status[i], |
| 135 | 135 | tab_rp_status[i], tab_rp_status[i]); |
| ... | ... | @@ -165,7 +165,7 @@ int main(void) |
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | - | |
| 168 | + | |
| 169 | 169 | /* MULTIPLE REGISTERS */ |
| 170 | 170 | ret = preset_multiple_registers(&mb_param, SLAVE, |
| 171 | 171 | addr, nb, tab_rq_registers); |
| ... | ... | @@ -197,7 +197,7 @@ int main(void) |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | } |
| 200 | - | |
| 200 | + | |
| 201 | 201 | printf("Test: "); |
| 202 | 202 | if (nb_fail) |
| 203 | 203 | printf("%d FAILS\n", nb_fail); |
| ... | ... | @@ -207,13 +207,12 @@ int main(void) |
| 207 | 207 | |
| 208 | 208 | /* Free the memory */ |
| 209 | 209 | free(tab_rq_status); |
| 210 | - free(tab_rp_status); | |
| 210 | + free(tab_rp_status); | |
| 211 | 211 | free(tab_rq_registers); |
| 212 | 212 | free(tab_rp_registers); |
| 213 | 213 | |
| 214 | 214 | /* Close the connection */ |
| 215 | 215 | modbus_close(&mb_param); |
| 216 | - | |
| 216 | + | |
| 217 | 217 | return 0; |
| 218 | 218 | } |
| 219 | - | ... | ... |
tests/random-test-slave.c
| ... | ... | @@ -36,14 +36,14 @@ int main(void) |
| 36 | 36 | printf("Memory allocation failed\n"); |
| 37 | 37 | exit(1); |
| 38 | 38 | } |
| 39 | - | |
| 39 | + | |
| 40 | 40 | socket = modbus_slave_listen_tcp(&mb_param, 1); |
| 41 | 41 | modbus_slave_accept_tcp(&mb_param, &socket); |
| 42 | 42 | |
| 43 | 43 | while (1) { |
| 44 | 44 | uint8_t query[MAX_MESSAGE_LENGTH]; |
| 45 | 45 | int query_size; |
| 46 | - | |
| 46 | + | |
| 47 | 47 | ret = modbus_slave_receive(&mb_param, -1, query, &query_size); |
| 48 | 48 | if (ret == 0) { |
| 49 | 49 | modbus_manage_query(&mb_param, query, query_size, &mb_mapping); |
| ... | ... | @@ -58,7 +58,6 @@ int main(void) |
| 58 | 58 | close(socket); |
| 59 | 59 | modbus_mapping_free(&mb_mapping); |
| 60 | 60 | modbus_close(&mb_param); |
| 61 | - | |
| 61 | + | |
| 62 | 62 | return 0; |
| 63 | 63 | } |
| 64 | - | ... | ... |
tests/unit-test-master.c
| ... | ... | @@ -44,7 +44,7 @@ int main(void) |
| 44 | 44 | /* TCP */ |
| 45 | 45 | modbus_init_tcp(&mb_param, "127.0.0.1", 1502); |
| 46 | 46 | /* modbus_set_debug(&mb_param, TRUE);*/ |
| 47 | - | |
| 47 | + | |
| 48 | 48 | if (modbus_connect(&mb_param) == -1) { |
| 49 | 49 | printf("ERROR Connection failed\n"); |
| 50 | 50 | exit(1); |
| ... | ... | @@ -55,9 +55,9 @@ int main(void) |
| 55 | 55 | UT_COIL_STATUS_NB_POINTS : UT_INPUT_STATUS_NB_POINTS; |
| 56 | 56 | tab_rp_status = (uint8_t *) malloc(nb_points * sizeof(uint8_t)); |
| 57 | 57 | memset(tab_rp_status, 0, nb_points * sizeof(uint8_t)); |
| 58 | - | |
| 58 | + | |
| 59 | 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 | 61 | UT_INPUT_REGISTERS_NB_POINTS) ? |
| 62 | 62 | UT_HOLDING_REGISTERS_NB_POINTS : UT_INPUT_REGISTERS_NB_POINTS; |
| 63 | 63 | tab_rp_registers = (uint16_t *) malloc(nb_points * sizeof(uint16_t)); |
| ... | ... | @@ -83,7 +83,7 @@ int main(void) |
| 83 | 83 | tab_rp_status); |
| 84 | 84 | printf("2/2 read_coil_status: "); |
| 85 | 85 | if (ret != 1) { |
| 86 | - printf("FAILED (nb points %d)\n", ret); | |
| 86 | + printf("FAILED (nb points %d)\n", ret); | |
| 87 | 87 | goto close; |
| 88 | 88 | } |
| 89 | 89 | |
| ... | ... | @@ -97,7 +97,7 @@ int main(void) |
| 97 | 97 | /* Multiple coils */ |
| 98 | 98 | { |
| 99 | 99 | uint8_t tab_value[UT_COIL_STATUS_NB_POINTS]; |
| 100 | - | |
| 100 | + | |
| 101 | 101 | set_bits_from_bytes(tab_value, 0, UT_COIL_STATUS_NB_POINTS, |
| 102 | 102 | UT_COIL_STATUS_TAB); |
| 103 | 103 | ret = force_multiple_coils(&mb_param, SLAVE, |
| ... | ... | @@ -117,7 +117,7 @@ int main(void) |
| 117 | 117 | UT_COIL_STATUS_NB_POINTS, tab_rp_status); |
| 118 | 118 | printf("2/2 read_coil_status: "); |
| 119 | 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 | 121 | goto close; |
| 122 | 122 | } |
| 123 | 123 | |
| ... | ... | @@ -146,7 +146,7 @@ int main(void) |
| 146 | 146 | printf("1/1 read_input_status: "); |
| 147 | 147 | |
| 148 | 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 | 150 | goto close; |
| 151 | 151 | } |
| 152 | 152 | |
| ... | ... | @@ -186,7 +186,7 @@ int main(void) |
| 186 | 186 | 1, tab_rp_registers); |
| 187 | 187 | printf("2/2 read_holding_registers: "); |
| 188 | 188 | if (ret != 1) { |
| 189 | - printf("FAILED (nb points %d)\n", ret); | |
| 189 | + printf("FAILED (nb points %d)\n", ret); | |
| 190 | 190 | goto close; |
| 191 | 191 | } |
| 192 | 192 | |
| ... | ... | @@ -217,7 +217,7 @@ int main(void) |
| 217 | 217 | tab_rp_registers); |
| 218 | 218 | printf("2/2 read_holding_registers: "); |
| 219 | 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 | 221 | goto close; |
| 222 | 222 | } |
| 223 | 223 | |
| ... | ... | @@ -240,10 +240,10 @@ int main(void) |
| 240 | 240 | tab_rp_registers); |
| 241 | 241 | printf("1/1 read_input_registers: "); |
| 242 | 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 | 244 | goto close; |
| 245 | 245 | } |
| 246 | - | |
| 246 | + | |
| 247 | 247 | for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) { |
| 248 | 248 | if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) { |
| 249 | 249 | printf("FAILED (%0X != %0X)\n", |
| ... | ... | @@ -252,14 +252,14 @@ int main(void) |
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | printf("OK\n"); |
| 255 | - | |
| 255 | + | |
| 256 | 256 | |
| 257 | 257 | /** ILLEGAL DATA ADDRESS **/ |
| 258 | 258 | printf("\nTEST ILLEGAL DATA ADDRESS:\n"); |
| 259 | 259 | |
| 260 | 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 | 263 | ret = read_coil_status(&mb_param, SLAVE, |
| 264 | 264 | UT_COIL_STATUS_ADDRESS, |
| 265 | 265 | UT_COIL_STATUS_NB_POINTS + 1, |
| ... | ... | @@ -289,7 +289,7 @@ int main(void) |
| 289 | 289 | printf("OK\n"); |
| 290 | 290 | else |
| 291 | 291 | printf("FAILED\n"); |
| 292 | - | |
| 292 | + | |
| 293 | 293 | ret = read_input_registers(&mb_param, SLAVE, |
| 294 | 294 | UT_INPUT_REGISTERS_ADDRESS, |
| 295 | 295 | UT_INPUT_REGISTERS_NB_POINTS + 1, |
| ... | ... | @@ -365,7 +365,7 @@ int main(void) |
| 365 | 365 | printf("OK\n"); |
| 366 | 366 | else |
| 367 | 367 | printf("FAILED\n"); |
| 368 | - | |
| 368 | + | |
| 369 | 369 | ret = read_input_registers(&mb_param, SLAVE, |
| 370 | 370 | UT_INPUT_REGISTERS_ADDRESS, |
| 371 | 371 | MAX_REGISTERS + 1, |
| ... | ... | @@ -419,11 +419,11 @@ int main(void) |
| 419 | 419 | |
| 420 | 420 | close: |
| 421 | 421 | /* Free the memory */ |
| 422 | - free(tab_rp_status); | |
| 422 | + free(tab_rp_status); | |
| 423 | 423 | free(tab_rp_registers); |
| 424 | 424 | |
| 425 | 425 | /* Close the connection */ |
| 426 | 426 | modbus_close(&mb_param); |
| 427 | - | |
| 427 | + | |
| 428 | 428 | return 0; |
| 429 | 429 | } | ... | ... |
tests/unit-test-slave.c
| ... | ... | @@ -47,7 +47,7 @@ int main(void) |
| 47 | 47 | |
| 48 | 48 | /* Examples from PI_MODBUS_300.pdf. |
| 49 | 49 | Only the read-only input values are assigned. */ |
| 50 | - | |
| 50 | + | |
| 51 | 51 | /** INPUT STATUS **/ |
| 52 | 52 | set_bits_from_bytes(mb_mapping.tab_input_status, |
| 53 | 53 | UT_INPUT_STATUS_ADDRESS, UT_INPUT_STATUS_NB_POINTS, |
| ... | ... | @@ -65,7 +65,7 @@ int main(void) |
| 65 | 65 | while (1) { |
| 66 | 66 | uint8_t query[MAX_MESSAGE_LENGTH]; |
| 67 | 67 | int query_size; |
| 68 | - | |
| 68 | + | |
| 69 | 69 | ret = modbus_slave_receive(&mb_param, -1, query, &query_size); |
| 70 | 70 | if (ret == 0) { |
| 71 | 71 | if (((query[HEADER_LENGTH_TCP + 4] << 8) + query[HEADER_LENGTH_TCP + 5]) |
| ... | ... | @@ -88,7 +88,6 @@ int main(void) |
| 88 | 88 | close(socket); |
| 89 | 89 | modbus_mapping_free(&mb_mapping); |
| 90 | 90 | modbus_close(&mb_param); |
| 91 | - | |
| 91 | + | |
| 92 | 92 | return 0; |
| 93 | 93 | } |
| 94 | - | ... | ... |