modbus server/slave on RS485

104 views
Skip to first unread message

ajay dange

unread,
Nov 26, 2015, 5:58:07 AM11/26/15
to libmodbus
Hello, i am developing a modbus RTU slave on Raspberry pi.I am using libmodbus 3.0.6
Code is given below

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



//#define MODBUS_TIMEOUT_SEC          3
//#define MODBUS_TIMEOUT_USEC         0

#define SERVER_ID                     1
#define UART_PORT                     "/dev/ttyAMA0"
#define BAUD_RATE                     19200
#define PARITY                        'N'
#define BYTESIZE                      8
#define STOPBITS                      1 


int main(void)
{
    struct timeval response_timeout;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
 int i;

  ctx = modbus_new_rtu(UART_PORT, BAUD_RATE, PARITY, BYTESIZE, STOPBITS);
 if (ctx == NULL) {
fprintf(stderr, "Unable to allocate libmodbus context\n");
return -1;
}
        
    /* Save original timeout */
 
response_timeout.tv_sec = MODBUS_TIMEOUT_SEC  ;
response_timeout.tv_usec = MODBUS_TIMEOUT_USEC  ;
modbus_set_response_timeout(ctx, &response_timeout);
modbus_set_debug(ctx, TRUE);

 modbus_set_slave(ctx, SERVER_ID);

 if (modbus_connect(ctx) == -1) {
fprintf(stderr, "Connexion failed: %s\n",modbus_strerror(errno));
modbus_free(ctx);
exit(-1);
}
mb_mapping = modbus_mapping_new(5, 5, 5, 5);
 if (mb_mapping == NULL) {
fprintf(stderr, "Failed to allocate the mapping: %s\n",
   modbus_strerror(errno));
modbus_free(ctx);
return -1;
}

for (;;) {
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
int rc;
   rc = modbus_receive(ctx, query);
if (rc != -1) {

/* rc is the query size */
modbus_reply(ctx, query, rc, mb_mapping);

} else {

/* Connection closed by the client or error */
break;
}
}
printf("Quit the loop: %s\n", modbus_strerror(errno));
modbus_mapping_free(mb_mapping);

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


I have used RS 485 for this connection & connected it with my raspberry pi.
When i run this code i get following error:
ERROR Connection timed out :select
<FF><00>Quit the loop:connection timed out

please help me to resolve this issue.
-regards

Reply all
Reply to author
Forward
0 new messages