[modbus_write_bit]: works, but why waiting for confirmation?

181 views
Skip to first unread message

123and...@gmail.com

unread,
Mar 17, 2015, 8:43:18 PM3/17/15
to libm...@googlegroups.com
Hello 

I am using modbus over TCP and trying to implment a client code. So far, retrieving the data from input registers works. 
But when I do:

modbus_write_bit(ctx,7191,ON)

It return me:
[00][01][00][00][00][06][FF][05][1C][17][FF][00]
Waiting for a confirmation...

and then it stuck forever until time out (which has been set to 5 minutes). 

On the server side, the server successfully received and follow this requested...... 
But the problem is that it looks like
1) either the connectivity between the client and the server has been lost 
2) or server will never reply due to a request like this. 


It is because of this reason, I am wondering if something I can do to ignore the reply to this request? 






Below is my code:


/*
 * Copyright © 2008-2010 Stéphane Raimbault <stephane....@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */



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


#include "unit-test.h"
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>


#if UINT_MAX >= 0xFFFFFFFF
typedef unsigned uint32;
#else
typedef unsigned long uint32;
#endif


#define C_ASSERT(expr) extern char CAssertExtern[(expr)?1:-1]


// Ensure uint32 is exactly 32-bit
C_ASSERT
(sizeof(uint32) * CHAR_BIT == 32);


// Ensure float has the same number of bits as uint32, 32
C_ASSERT
(sizeof(uint32) == sizeof(float));










float to_float(uint32_t a){
 
float f;
 memcpy
(&f, &a,sizeof(float));
 
return f;


}






enum {
    TCP
,
    TCP_PI
,
    RTU
};


int main(int argc, char *argv[])
{
    uint8_t
*tab_rp_bits;
    modbus_t
*ctx;






   
int nb_points;
    uint16_t tab_reg
[640];
   
int rc;


   
int use_backend;
   
struct timeval response_timeout;




   
if (argc > 1) {
       
if (strcmp(argv[1], "tcp") == 0) {
            use_backend
= TCP;
 
} else if (strcmp(argv[1], "tcppi") == 0) {
            use_backend
= TCP_PI;
       
} else if (strcmp(argv[1], "rtu") == 0) {
            use_backend
= RTU;
       
} else {
            printf
("Usage:\n  %s [tcp|tcppi|rtu] - Modbus client for unit testing\n\n", argv[0]);
           
exit(1);
       
}
   
} else {
       
/* By default */
        use_backend
= TCP;
   
}


   
if (use_backend == TCP) {
        ctx
= modbus_new_tcp("173.239.182.26", 502);
   
} else if (use_backend == TCP_PI) {
        ctx
= modbus_new_tcp_pi("::1", "1502");
   
} else {
        ctx
= modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);
   
}
   
if (ctx == NULL) {
        fprintf
(stderr, "Unable to allocate libmodbus context\n");
       
return -1;
   
}
    modbus_set_debug
(ctx, TRUE);
    modbus_set_error_recovery
(ctx,
                              MODBUS_ERROR_RECOVERY_LINK
|
                              MODBUS_ERROR_RECOVERY_PROTOCOL
);


   
if (use_backend == RTU) {
          modbus_set_slave
(ctx, SERVER_ID);
   
}


   
if (modbus_connect(ctx) == -1) {
        fprintf
(stderr, "Connection failed: %s\n",
                modbus_strerror
(errno));
        modbus_free
(ctx);
       
return -1;
   
}
    response_timeout
.tv_sec = 300;
        response_timeout
.tv_usec = 0;
        modbus_set_response_timeout
(ctx, &response_timeout);
   
/* Allocate and initialize the memory to store the bits */
    nb_points
= 16;
    tab_rp_bits
= (uint8_t *) malloc(nb_points * sizeof(uint8_t));
    memset
(tab_rp_bits, 0, nb_points * sizeof(uint8_t));


    printf
("\n\n\n\n*************************Are you ready?????");
    printf
("\n Wating for user's command");
    printf
("** UNIT TESTING **\n");
    printf
("\nTEST WRITE/READ:\n");


   
/** COIL BITS **/
while(1){
   
/* Single */
    rc
= modbus_write_bit(ctx, 7191, ON);
   
if (rc != -1) {

        printf
("Write Sucessful.\n");
   
}
   
else
        printf
("Write unSucessfull.\n");


}
printf
("\n\n\n\n");
   
if (rc == -1) {
        printf
("FAILED (nb points %d)\n", rc);
       
goto close;
   
}


    sleep
(1);
}
    close
:




    free
(tab_rp_bits);


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


   
return 0;
}





Reply all
Reply to author
Forward
0 new messages