0

I need to transfer data to BeagleBone Black via Modbus RTU using Rs485. To work with Modbus RTU,but I don't know how to toggle rts in rs-485.HELP

Modbus RTU data transmission code

how to add RS485 code to this,to use the Modbus library libmodbus

#include "modbus-rtu.h"
#include <stdio.h>
#include <errno.h>

int main(){
int connected;
    modbus_t *ctx;     
    uint16_t tab_reg[64];    
    int rc;     
    int i;
    ctx = modbus_new_rtu("/dev/ttyS4", 9600, 'N', 8, 1);     
    if(ctx == NULL) {   
        fprintf(stderr, "Unable to create the libmodbus context\n");

    }
    else {
        modbus_set_slave(ctx, 1);     
        modbus_set_debug(ctx, TRUE);     
        connected = modbus_connect(ctx);    
        printf("modbus_set_slave return: %d\n", rc);
        if (rc != 0)
        {
            printf("modbus_set_slave: %s \n"modbus_strerror(errno));        
        }
            rc = modbus_read_registers(ctx, 0, 3, tab_reg);      
        for (i = 0; i < rc; i++) {
            printf("reg[%d]=%d (0x%X)\n", i, tab_reg[i], tab_reg[i]);
        }
            if(rc == -1)
            {
                fprintf(stderr, "%s\n", modbus_strerror(errno));
            }               
        modbus_close(ctx);     
        modbus_free(ctx);     
    }
return 0;
}

1 Answers1

0

First I would check if your hardware support automatic (hardware) half-duplex direction control.

If not, then you can take a look at this answer. In there you have all the details you need to compile and install libmodbus with support for toggling the line with software.

I'm using this solution in my RPi and CHIP computers but you should be able to use it right away on your BBB. You might find a small caveat if the GPIO file name has a 3 or 4-digit number (take a look in /sys/class/gpio). If that's the case, take a look here, I had to modify the code to correct that problem.

Marcos G.
  • 3,371
  • 2
  • 8
  • 16