preset_multiple_registers=-12 (timeout)

Asked by wawan wiratno

Hi All,

I tried writing to Deep Sea 5220 register using preset_multiple_register.
according to its documentation the value I've sent is correct but always return timeout
==============================================================================
gmbulz@gmbulz-fay:/var/www/libmodbus$ sudo ./modget /dev/ttyS0 9600 none 8 2 10 4104 35701
modget port baud parity databit stopbit slave address value
Opening /dev/ttyS0 at 9600 bauds (none)
connection success
parameters:
 address=10
 register=4104
 value=35701
[0A][10][10][08][00][01][02][8B][75][62][FE]
Waiting for a message (8 bytes)...

ERROR Communication time out (-12)
preset_multiple_registers=-12
===============================================================================

here is my code:
/***********************************************************************************************************************/
int main (int argc, char *argv[]){

 int count, ret;
 printf ("modget port baud parity databit stopbit slave address value\n");
 if (argc > 8){
   //for (count = 1; count < argc; count++)
     //printf("argv[%d] = %s\n", count, argv[count]);

  modbus_param_t mb_param;
  modbus_init_rtu(&mb_param, argv[1], atoi(argv[2]), argv[3], atoi(argv[4]), atoi(argv[5]));
  modbus_set_debug(&mb_param, TRUE);
  if (modbus_connect(&mb_param) == -1) {
          printf("ERROR Connection failed\n");
          exit(1);
  }else printf("connection success\n");
  //nulis di register 4105
  sleep(2);
  uint16_t val;
  val = atoi(argv[8]);
  printf("parameters:\n address=%d\n register=%d\n value=%i)\n", atoi(argv[6]),atoi(argv[7]),val);
   ret = preset_multiple_registers(&mb_param, atoi(argv[6]), atoi(argv[7]), 1, &val);
                printf("preset_multiple_registers=%d \n", ret);

  modbus_close(&mb_param);
 }else printf("parameter error\n");
  return 0;
}
/******************************************************************************************************************/

any help whould be much appreciated, thanks...

Question information

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

Your code is really hard to read (argv mess) and it's strange to see libmodbus in /var/www, anyway 8 bits and 2 stop bits is strange, are you're sure about that?

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#2

Hi Stephane,

I'm newbee in C i'm php programmer before, sorry that my code are messup.
but I have compile it and its run.

yes the specification from Deep sea said that the parameter databits = 8 and stopbits=2
I have use modpoll also for reading and its working.

I though that modpoll also using libmodbus as a library right?

thank you

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

2010/10/28 wawan wiratno <email address hidden>:
> Question #130642 on libmodbus changed:
> https://answers.launchpad.net/libmodbus/+question/130642
>
> wawan wiratno posted a new comment:
> Hi Stephane,
>
> I'm newbee in C i'm php programmer before, sorry that my code are messup.
> but I have compile it and its run.
>
> yes the specification from Deep sea said that the parameter databits = 8 and stopbits=2
> I have use modpoll also for reading and its working.
>
> I though that modpoll also using libmodbus as a library right?

No, modpoll is based on proprietary library, could check the result
with libmodbus 2.9.1 (see http://libmodbus.org) and raise the timeout
if required. If it doesn't work are you able to see the query sent by
modpoll (just to be sure)?

With the new library, the code will be something like that:
modbus_t *mb;
uint16_t tab_reg[32];

/* Note, the last argument will be removed in libmodbus 2.9.2 ... */
mb = modbus_new_rtu("/dev/ttyS0", 9600, 'N', 8, 2, 10);
modbus_set_debug(mb, TRUE);
modbus_connect(mb);

modbus_set_slave(mb, 10);
tab_reg[0] = 35701;
rc = modbus_write_registers(mb, 4104, 1, tab_reg); /* or
modbus_write_register for single register */
/* Check result */

modbus_close(mb);
modbus_free(mb);

See these functions if necessary:
void modbus_get_timeout_begin(modbus_t *ctx, struct timeval *timeout);
void modbus_set_timeout_begin(modbus_t *ctx, const struct timeval *timeout);

void modbus_get_timeout_end(modbus_t *ctx, struct timeval *timeout);
void modbus_set_timeout_end(modbus_t *ctx, const struct timeval *timeout);

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#4

Hi Stephane

I have tried with new library 2.9.1
here is my code:
        modbus_t *mb;
 uint16_t tab_reg[32];
 int rc;

 /* Note, the last argument will be removed in libmodbus 2.9.2 ... */
 mb = modbus_new_rtu("/dev/ttyS0", 19200, 'N', 8, 2, 10);
 modbus_set_debug(mb, TRUE);
 modbus_connect(mb);

 modbus_set_slave(mb, 10);
 tab_reg[0] = 35700;
 tab_reg[1] = 29835;
 rc = modbus_write_registers(mb, 4104, 2, tab_reg);
 //rc = modbus_read_registers(mb, 1030, 1, tab_reg);
 /* or
 modbus_write_register for single register */
 /* Check result */
 printf("result=%d \n", rc);
 //printf("read batery=%d \n",tab_reg);
 modbus_close(mb);
 modbus_free(mb);

and the result is :

gmbulz@gmbulz-fay:/var/www/libmodbus2/tests$ ./modb
Opening /dev/ttyS0 at 19200 bauds (N, 8, 2)
[0A][10][10][08][00][02][04][8B][74][74][8B][16][74]
Waiting for a confirmation (8 bytes)...
ERROR Slave device or server is busy
<0A><90><06><BD><C0>result=-1

but when I used modpoll app it works:

gmbulz@gmbulz-fay:/var/www/modbus/linux$ ./modpoll -b 19200 -p none -d 8 -s 2 -m rtu -a 10 -r 1030 -c 1 -t 4 /dev/ttyS0
Polling slave (Ctrl-C to stop) ...
[1030]: 120

did I missed something?

Thanks

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

Your code seems OK and the slave answers so the serial line settings are good but the query sent by libmodbus isn't the same as modpoll because the line is commented in the source code.

What's the result with the same query?

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#6

Hi Stepane

return same result:

gmbulz@gmbulz-fay:/var/www/libmodbus2/tests$ ./modb
Opening /dev/ttyS0 at 19200 bauds (N, 8, 2)
[0A][03][04][06][00][01][64][40]
Waiting for a confirmation (7 bytes)...
ERROR Slave device or server is busy
<0A><83><06><B0><F0>result=-1
read batery=-1079061160

is it about timeout? where I should change the timeout setting?

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#7

I have also tried with different library "libyam" but also get same result:

gmbulz@gmbulz-fay:/var/www/libyam$ ./test --device=/dev/ttyS0,19200 --debug --timeout=5000 --readregister=1030[1]
Generic send packet to 0A: CRC = 6440, ADU: 8 bytes
[0A][03][04][06][00][01][64][40]
<0A><83><06>
adu_len = 3
Error reading registers: Slave Busy

Only modpoll works correctly.
according to Deepsea support I already sent the right message.
so I believe that its about timing, right?

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#8

Hi Stephane,

blessing in the sky...
I just create loop for 1 second delay to execute the write/read command.
the second,third,... command just work like a charm... :)

I dont have any reason why this is working. I just inspired by modpoll that have recursive pooling by 1 second

Thanks for your support

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

It makes sense to add a delay before reading after a writing if your device is slow.

I don't understand what do you mean by write/read, is it a write and read or a write or read? Have you try to add a delay (around 5 seconds) after the connect?

In every case, your slave device is just dead slow!

Revision history for this message
wawan wiratno (wawan-wiratno) said :
#10

I do write then read,
not yet, just put 1 second after connect, will try then..

I agree its slow anyway

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

LinkedIn
------------

I'd like to add you to my professional network on LinkedIn.

- Stéphane Raimbault

Stéphane Raimbault
Open Source Project Leader and Software developer at Makina Corpus
Nantes Area, France

Confirm that you know Stéphane Raimbault
https://www.linkedin.com/e/-woxsxq-gl2146qh-5v/isd/2469859655/Mo5AuRwR/

--
(c) 2011, LinkedIn Corporation

Revision history for this message
Abid Qureshi (ajqureshi) said :
#12

Hi wawan wiratno,

I am new to modbus too. I also have a php background. I started playing with DSE controller lately. I need to know where can I get the documetation for the DSE contorller for modbus parameters?