libmodbus"ERROR communication time out -12)

Asked by jeffrey

Hi,

Im running a C program for capturing a modbus data from a charge controller. The program works when I connected it to a local network but when I connected it through Internet I got this error message "ERROR Communication time out (-12)" and the value of results is much more different from the result when it was connected to a local network. Im wondering if there's a way to change the communication time out from modbus.h that will solve this problem? Here's the part of the program:

#include <stdio.h>
#include <stdlib.h>
#include <modbus/modbus.h>

#define SUNSAVERMPPT 0x01 /* MODBUS Address of the SunSaver MPPT */

int main(void)
{
 modbus_param_t mb_param;
 int ret;
 float adc_vb_f,adc_va_f,adc_vl_f,adc_ic_f,adc_il_f, Vb_f, Vb_ref, Ahc_r, Ahc_t, kWhc;
 float V_lvd, Ahl_r, Ahl_t, Power_out, Sweep_Vmp, Sweep_Pmax, Sweep_Voc, Vb_min_daily;
 float Vb_max_daily, Ahc_daily, Ahl_daily, vb_min, vb_max;
 short T_hs, T_batt, T_amb, T_rts;
 unsigned short charge_state, load_state, led_state;
 unsigned int hourmeter;
 unsigned short array_fault, load_fault, dip_switch, array_fault_daily, load_fault_daily;
 unsigned int alarm, alarm_daily;
 uint16_t data[50];

 /* Setup the serial port parameters */
 modbus_init_tcp(&mb_param, "192.168.1.1", 502); /* Add the appropriate path to your serial port */

 /* Open the MODBUS connection */
 if (modbus_connect(&mb_param) == -1) {
  printf("ERROR Connection failed\n");
  exit(1);
 }

 /* Read the RAM Registers */
 ret = read_input_registers(&mb_param, SUNSAVERMPPT, 0x0008, 45, data);

 /* Close the MODBUS connection */
 modbus_close(&mb_param);

 /* Convert the results to their proper values and print them out */
 printf("RAM Registers\n\n");

 adc_vb_f=data[0]*100.0/32768.0;
 printf("adc_vb_f = %.2f V\n",adc_vb_f);

 adc_va_f=data[1]*100.0/32768.0;
 printf("adc_va_f = %.2f V\n",adc_va_f);

 adc_vl_f=data[2]*100.0/32768.0;
 printf("adc_vl_f = %.2f V\n",adc_vl_f);

 adc_ic_f=data[3]*79.16/32768.0;
 printf("adc_ic_f = %.2f A\n",adc_ic_f);

 adc_il_f=data[4]*79.16/32768.0;
 printf("adc_il_f = %.2f A\n",adc_il_f);

 T_hs=data[5];
 printf("T_hs = %d °C\n",T_hs);

 T_batt=data[6];
 printf("T_batt = %d °C\n",T_batt);

 T_amb=data[7];
 printf("T_amb = %d °C\n",T_amb);

 T_rts=data[8];
 printf("T_rts = %d °C\n",T_rts);

Question information

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

Hi,

I haven't solve the connection time out problem, I always got this error

Connecting to 192.168.1.1
[00][01][00][00][00][06][FF][04][00][08][00][2D]
Waiting for a confirmation...
ERROR Connection timed out: select

I read the manual from the link you gave me and came up to insert the modbus_get_response_timeout in the program. Please help know if there's something wrong from program? Im trying to increase the timeout so that there's enough time for establishing a stable network connection. Thanks in advance.

int main(void)
{
 modbus_t *ctx;
 int ret;

 uint16_t data[50];
 struct timeval old_response_timeout;
 struct timeval response_timeout;

 ctx = modbus_new_tcp("192.168.1.1", 502);
 modbus_set_debug(ctx, TRUE);

/* Save original timeout */
 modbus_get_response_timeout(ctx, &old_response_timeout);

 /* Define a new and too short timeout! */
 response_timeout.tv_sec = 20;
 response_timeout.tv_usec = 20;
 modbus_set_response_timeout(ctx, &response_timeout);

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

 }

 /* Read the RAM Registers */
 ret = modbus_read_input_registers(ctx, 0x0008, 45, data);

 /* Close the MODBUS connection */
 modbus_close(ctx);
 modbus_free(ctx);

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

> I read the manual from the link you gave me and came up to insert the modbus_get_response_timeout in the program.

get is getter if you want to change you must use set !

Revision history for this message
jeffrey (jep0815) said :
#4

Need to put modbus_set_slave..

Revision history for this message
jeffrey (jep0815) said :
#5

Need to put modbus_set_slave..