Commit e48f66e0bab114efdcaaeec3c0e6a0f45e9f4ee0

Authored by Stéphane Raimbault
1 parent 512e857d

Patch from Dirk Reusch to change the port number at init in TCP.

include/modbus/modbus.h
... ... @@ -23,7 +23,7 @@
23 23 #include <termios.h>
24 24 #include <arpa/inet.h>
25 25  
26   -#define MODBUS_TCP_PORT 502
  26 +#define MODBUS_TCP_DEFAULT_PORT 502
27 27  
28 28 #define HEADER_LENGTH_RTU 0
29 29 #define PRESET_QUERY_SIZE_RTU 6
... ... @@ -130,6 +130,8 @@ typedef struct {
130 130 int debug;
131 131 /* IP address */
132 132 char ip[16];
  133 + /* TCP port */
  134 + uint16_t port;
133 135 /* Header length used for offset */
134 136 int header_length;
135 137 /* Checksum size RTU = 2 and TCP = 0 */
... ... @@ -203,10 +205,17 @@ int report_slave_id(modbus_param_t *mb_param, int slave,
203 205 void modbus_init_rtu(modbus_param_t *mb_param, char *device,
204 206 int baud, char *parity, int data_bit,
205 207 int stop_bit);
  208 +
206 209 /* Initialises a parameters structure for TCP
207   - - ip : "192.168.0.5" */
208   -void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address);
  210 + - ip : "192.168.0.5"
  211 + - port : 1099
209 212  
  213 + Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  214 + (502). It's convenient to use a port number greater than or equal
  215 + to 1024 because it's not necessary to be root to use this port
  216 + number.
  217 +*/
  218 +void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port);
210 219  
211 220 /* Sets up a serial port for RTU communications to modbus or a TCP
212 221 connexion */
... ...
src/modbus.c
... ... @@ -1151,11 +1151,18 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device,
1151 1151 mb_param->checksum_size = CHECKSUM_SIZE_RTU;
1152 1152 }
1153 1153  
1154   -/* Initialises the modbus_param_t structure for TCP */
1155   -void modbus_init_tcp(modbus_param_t *mb_param, char *ip)
  1154 +/* Initialises the modbus_param_t structure for TCP.
  1155 +
  1156 + Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one
  1157 + (502). It's convenient to use a port number greater than or equal
  1158 + to 1024 because it's not necessary to be root to use this port
  1159 + number.
  1160 +*/
  1161 +void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port)
1156 1162 {
1157 1163 memset(mb_param, 0, sizeof(modbus_param_t));
1158 1164 strncpy(mb_param->ip, ip, sizeof(char)*16);
  1165 + mb_param->port = port;
1159 1166 mb_param->type_com = TCP;
1160 1167 mb_param->header_length = HEADER_LENGTH_TCP;
1161 1168 mb_param->checksum_size = CHECKSUM_SIZE_TCP;
... ... @@ -1423,7 +1430,7 @@ static int modbus_connect_tcp(modbus_param_t *mb_param)
1423 1430 struct sockaddr_in addr;
1424 1431  
1425 1432 addr.sin_family = AF_INET;
1426   - addr.sin_port = htons(MODBUS_TCP_PORT);
  1433 + addr.sin_port = htons(mb_param->port);
1427 1434 addr.sin_addr.s_addr = inet_addr(mb_param->ip);
1428 1435  
1429 1436 mb_param->fd = socket(AF_INET, SOCK_STREAM, 0);
... ... @@ -1544,9 +1551,8 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param)
1544 1551 socklen_t addrlen;
1545 1552  
1546 1553 addr.sin_family = AF_INET;
1547   - /* The modbus port is < 1024
1548   - This program must be made setuid root. */
1549   - addr.sin_port = htons(MODBUS_TCP_PORT);
  1554 + /* If the modbus port is < to 1024, we need the setuid root. */
  1555 + addr.sin_port = htons(mb_param->port);
1550 1556 addr.sin_addr.s_addr = INADDR_ANY;
1551 1557 memset(&(addr.sin_zero), '\0', 8);
1552 1558  
... ...
src/test-modbus-master.c
... ... @@ -45,7 +45,7 @@ int main(void)
45 45 /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */
46 46  
47 47 /* TCP */
48   - modbus_init_tcp(&mb_param, "169.254.7.104");
  48 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
49 49 modbus_set_debug(&mb_param, TRUE);
50 50  
51 51 modbus_connect(&mb_param);
... ...
src/test-modbus-slave.c
... ... @@ -36,11 +36,12 @@ int main(void)
36 36 int ret;
37 37 int i;
38 38  
39   - modbus_init_tcp(&mb_param, "127.0.0.1");
  39 + modbus_init_tcp(&mb_param, "127.0.0.1", 1502);
40 40 modbus_set_debug(&mb_param, TRUE);
41 41  
42 42 modbus_mapping_new(&mb_mapping,
43   - nb_coil_status, nb_input_status, nb_input_registers, nb_holding_registers);
  43 + nb_coil_status, nb_input_status,
  44 + nb_input_registers, nb_holding_registers);
44 45  
45 46 /* Examples from PI_MODBUS_300.pdf */
46 47  
... ...