简体   繁体   English

如何使用RS485将数据传输到BeagleBone Black

[英]How to transfer data using RS485 to BeagleBone Black

I need to transfer data to BeagleBone Black via Modbus RTU using Rs485. 我需要使用Rs485通过Modbus RTU将数据传输到BeagleBone Black。 To work with Modbus RTU,but I don't know how to toggle rts in rs-485.HELP 要使用Modbus RTU,但我不知道如何在rs-485.HELP中切换rts

Modbus RTU data transmission code Modbus RTU数据传输代码

how to add RS485 code to this,to use the Modbus library libmodbus 如何添加RS485代码,使用Modbus库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;
}

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. 在那里,您可以获得编译和安装libmodbus所需的所有详细信息,并支持使用软件切换线路。

I'm using this solution in my RPi and CHIP computers but you should be able to use it right away on your BBB. 我在我的RPi和CHIP计算机上使用此解决方案,但您应该可以立即在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 ). 如果GPIO文件名具有3位或4位数字,您可能会发现一个小警告(请查看/sys/class/gpio )。 If that's the case, take a look here , I had to modify the code to correct that problem. 如果是这种情况,请看一下这里 ,我不得不修改代码来纠正这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM