0
\$\begingroup\$

I have the following code for connection to a temperature sensor as my slave using the libmodbus library in C,

#include <stdio.h>
#include <stdlib.h>
#include <modbus.h>
#include <errno.h>
int main()
{
  struct timeval old_response_timeout;
  struct timeval response_timeout;
  modbus_t *ctx = NULL;
  int rc = 0;
  uint16_t tab_reg[64];
  int i = 0;
  int slave = 0;
  int connected = 0;
  int serial = 0;
  ctx = modbus_new_rtu("/dev/ttyS1", 9600, 'E', 8, 1);
  if (ctx == NULL) {
    fprintf(stderr, "Unable to create the libmodbus context\n");
    return -1;
  }
  modbus_set_debug(ctx, 1);
  modbus_get_response_timeout(ctx, &old_response_timeout);
  response_timeout.tv_sec = 10;
  response_timeout.tv_usec = 0;
  modbus_set_response_timeout(ctx, &response_timeout);
  modbus_set_byte_timeout(ctx, &response_timeout);

  slave = modbus_set_slave(ctx,247);

  if(slave == -1)
    printf("Didn't connect to slave/n");

  connected = modbus_connect(ctx);

  if(connected == -1)
    printf("Connection failed\n");
  if(connected == 0)
    printf("connected\n");
  //serial = modbus_rtu_set_serial_mode(ctx, MODBUS_RTU_RS485);
  //if(serial == -1)
  //  printf("Didn't set serial mode/n:%s\n",modbus_strerror(errno));
  rc = modbus_read_registers(ctx,0x27,2,tab_reg);

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

  for(i=0;i<rc;i++)
    printf("degrees %d\n", tab_reg[i]);

  return 0;
}

It fails in the following section of code,

  rc = modbus_read_registers(ctx,0x27,2,tab_reg);

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

with a connection timeout. could this be due to me setting up the connection to the modbus device the wrong way, or are there any libmodbus library problems anyone knows of that could be causing this? I am using this device http://www.epluse.com/en/products/humidity-instruments/humidity-measuring-modules/ee071/ on Redhat Linux 6.5.

\$\endgroup\$
1
\$\begingroup\$

Strange that it uses 9600bps... most of the modbus devices I've seen use 19200 bps. Could you test it with the MODPOLL tool to see if you can communicate with it, and if/when you do, post the parameters that proved to be working.

As you probably know, there's two basic modbus protocols, MODBUS/ASCII and MODBUS/RTU. I didn't find from the datasheet which one it is using, so maybe you're using the other one. If it's using 9600bps, then it might very well be MODBUS/ASCII.

\$\endgroup\$
  • \$\begingroup\$ I used mod poll but I am not sure if it is working right, I tested on a modbus simulator and get different results then what I set the values to. Did you ever have any issues with modpoll? \$\endgroup\$ – jgr208 Oct 31 '14 at 15:59
  • \$\begingroup\$ No, the modpoll has always worked ok for me. But what is confusing about modbus is all the different commands and operating modes; sometimes you use "write coil" and sometimes "write register", with some software you might "set register 1" and in another software the exact same operation might be called "write holding register 30000". Confusing, but if you use the same software to read and write registers, the results should be consistent. \$\endgroup\$ – PkP Oct 31 '14 at 16:06
  • \$\begingroup\$ i did it where i set the bps to 9600 and it showed the bps as 19200. \$\endgroup\$ – jgr208 Oct 31 '14 at 16:11
  • \$\begingroup\$ figured out what the problem is, there was no master connected to the simulator thus the bogus values, so therefore the actual device must be wired up wrong I am thinking. \$\endgroup\$ – jgr208 Oct 31 '14 at 16:47
  • \$\begingroup\$ Actually figured it out finally. The linux kernal was below 3.0 and the library I was using was meant for the 3.0 linux kernal. For why this matters, take a look here stackoverflow.com/questions/25250731/… \$\endgroup\$ – jgr208 Nov 6 '14 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.