diff --git a/include/modbus/modbus.h b/include/modbus/modbus.h index d5c8d99..59ae7bc 100644 --- a/include/modbus/modbus.h +++ b/include/modbus/modbus.h @@ -23,7 +23,7 @@ #include #include -#define MODBUS_TCP_PORT 502 +#define MODBUS_TCP_DEFAULT_PORT 502 #define HEADER_LENGTH_RTU 0 #define PRESET_QUERY_SIZE_RTU 6 @@ -130,6 +130,8 @@ typedef struct { int debug; /* IP address */ char ip[16]; + /* TCP port */ + uint16_t port; /* Header length used for offset */ int header_length; /* Checksum size RTU = 2 and TCP = 0 */ @@ -203,10 +205,17 @@ int report_slave_id(modbus_param_t *mb_param, int slave, void modbus_init_rtu(modbus_param_t *mb_param, char *device, int baud, char *parity, int data_bit, int stop_bit); + /* Initialises a parameters structure for TCP - - ip : "192.168.0.5" */ -void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address); + - ip : "192.168.0.5" + - port : 1099 + Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one + (502). It's convenient to use a port number greater than or equal + to 1024 because it's not necessary to be root to use this port + number. +*/ +void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port); /* Sets up a serial port for RTU communications to modbus or a TCP connexion */ diff --git a/src/modbus.c b/src/modbus.c index 19eccfa..dda3ee2 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -1151,11 +1151,18 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, mb_param->checksum_size = CHECKSUM_SIZE_RTU; } -/* Initialises the modbus_param_t structure for TCP */ -void modbus_init_tcp(modbus_param_t *mb_param, char *ip) +/* Initialises the modbus_param_t structure for TCP. + + Set the port to MODBUS_TCP_DEFAULT_PORT to use the default one + (502). It's convenient to use a port number greater than or equal + to 1024 because it's not necessary to be root to use this port + number. +*/ +void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port) { memset(mb_param, 0, sizeof(modbus_param_t)); strncpy(mb_param->ip, ip, sizeof(char)*16); + mb_param->port = port; mb_param->type_com = TCP; mb_param->header_length = HEADER_LENGTH_TCP; mb_param->checksum_size = CHECKSUM_SIZE_TCP; @@ -1423,7 +1430,7 @@ static int modbus_connect_tcp(modbus_param_t *mb_param) struct sockaddr_in addr; addr.sin_family = AF_INET; - addr.sin_port = htons(MODBUS_TCP_PORT); + addr.sin_port = htons(mb_param->port); addr.sin_addr.s_addr = inet_addr(mb_param->ip); mb_param->fd = socket(AF_INET, SOCK_STREAM, 0); @@ -1544,9 +1551,8 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param) socklen_t addrlen; addr.sin_family = AF_INET; - /* The modbus port is < 1024 - This program must be made setuid root. */ - addr.sin_port = htons(MODBUS_TCP_PORT); + /* If the modbus port is < to 1024, we need the setuid root. */ + addr.sin_port = htons(mb_param->port); addr.sin_addr.s_addr = INADDR_ANY; memset(&(addr.sin_zero), '\0', 8); diff --git a/src/test-modbus-master.c b/src/test-modbus-master.c index 864dcd5..8c8091e 100644 --- a/src/test-modbus-master.c +++ b/src/test-modbus-master.c @@ -45,7 +45,7 @@ int main(void) /* modbus_init_rtu(&mb_param, "/dev/ttyS0", 19200, "none", 8, 1); */ /* TCP */ - modbus_init_tcp(&mb_param, "169.254.7.104"); + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); modbus_set_debug(&mb_param, TRUE); modbus_connect(&mb_param); diff --git a/src/test-modbus-slave.c b/src/test-modbus-slave.c index 4cdc053..bc6e348 100644 --- a/src/test-modbus-slave.c +++ b/src/test-modbus-slave.c @@ -36,11 +36,12 @@ int main(void) int ret; int i; - modbus_init_tcp(&mb_param, "127.0.0.1"); + modbus_init_tcp(&mb_param, "127.0.0.1", 1502); modbus_set_debug(&mb_param, TRUE); modbus_mapping_new(&mb_mapping, - nb_coil_status, nb_input_status, nb_input_registers, nb_holding_registers); + nb_coil_status, nb_input_status, + nb_input_registers, nb_holding_registers); /* Examples from PI_MODBUS_300.pdf */