ERROR Connection timed out: select

Asked by ahmed

I am coding a small application that use modbus, I'm using libmodbus
I have a pci card (2 serial port + 1 parallel port) + RS232 <-> RS485 converter
I wired the PC with the slave, tested on windows with modbusPoll : OK
But when using any modbus tools on my ubuntu, it doesn't work

With libmodbus I have "ERROR Connection timed out: select"
I tried to monitor the serial with "statserial" I have
Before the test :
RTS = 1
CTS = 0
DSR = 1
DTR = 1

after test :
RTS = 1
CTS = 1
DSR = 1
DTR = 1

and no activity for TX or RX

Can any one help me, please

Greeting from Tunisia :)

Here is the code of my app

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <modbus.h>

int main(void)
{
 int rc;
 modbus_t *ctx;
 uint16_t tab_reg[10];
int i;
struct timeval byte_timeout;
byte_timeout.tv_sec = 0;
byte_timeout.tv_usec = 30;

struct timeval response_timeout;
response_timeout.tv_sec = 0;
response_timeout.tv_usec = 300;

 ctx = modbus_new_rtu("/dev/ttyS5", 9600, 'N', 8, 1);
 if (ctx == NULL) {
     fprintf(stderr, "Unable to create the libmodbus context\n");
     return -1;
 }

modbus_get_byte_timeout(ctx, &byte_timeout);
modbus_set_response_timeout(ctx, &response_timeout);

 modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
 modbus_set_slave(ctx, 1);
 modbus_set_debug(ctx, TRUE);
 modbus_set_error_recovery(ctx, TRUE);
 modbus_rtu_set_rts(ctx, MODBUS_RTU_RTS_UP);

 if (modbus_connect(ctx) == -1) {
     fprintf(stderr, "Connexion failed: %s\n", modbus_strerror(errno));
      modbus_free(ctx);
      return -1;
 }

 rc = modbus_read_input_registers(ctx, 1, 1, tab_reg);
 if (rc == -1) {
     fprintf(stderr, "Read : %s\n", modbus_strerror(errno));
     return -1;
 }

   modbus_close(ctx);
    modbus_free(ctx);
 return 0;
}

Question information

Language:
English Edit question
Status:
Solved
For:
libmodbus Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Stéphane Raimbault (sra) said :
#1

set_byte_timeout and set_response_timeout to decent values!

Revision history for this message
ahmed (ahmed-jerbi) said :
#2

What do you mean by decent value ?

struct timeval byte_timeout;
byte_timeout.tv_sec = ;
byte_timeout.tv_usec = 300;

struct timeval response_timeout;
response_timeout.tv_sec = 3;
response_timeout.tv_usec = 0;

is it ok?

thank your for replying

Revision history for this message
ahmed (ahmed-jerbi) said :
#3

I tried different timeout but no result
How can I test my com port (I have ttyS4 & ttyS5)

thank you

Revision history for this message
ahmed (ahmed-jerbi) said :
#4

Even with longer timeout it's not working
any suggestions ?

Revision history for this message
Karthik.K.M. (karthikkm1987) said :
#5

hey give the value in sec and not in usec.it might work

Revision history for this message
ahmed (ahmed-jerbi) said :
#6

I tried

byte_timeout.tv_sec = 1;
byte_timeout.tv_usec = 300;

response_timeout.tv_sec = 3;
response_timeout.tv_usec = 0;

not working, how to be sure that my serial port is ok and well configured ?

Revision history for this message
ahmed (ahmed-jerbi) said :
#7

I solved my problem with com port, modpoll work fine with my device
but I still have the same error with libmodbus

"ERROR Connection timed out: select"

Revision history for this message
Launchpad Janitor (janitor) said :
#8

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Stéphane Raimbault (sra) said :
#9

Solved