• 嵌入式开源库之libmodbus学习笔记


    socat

    1. 安装sudo apt-get install socat
    2. 创建终端 socat -d -d pty,b115200 pty,b115200
    3. 查看终端 ls /dev/pts/

    minicom

    1. 安装 sudo apt-get install minicom
    2. 链接虚拟终端 sudo minicom -D /dev/pts/3
    3. 以十六进制显示 minicom -D /dev/pts/1 -H
    4. 设置波特率 minicom -D /dev/pts/1 -b B115200
    5. 设置回显 Ctrl+A->Z->E
    6. 设置自动换行 Ctrl+A->W
    7. 清屏 Ctrl+A->C
    8. 退出 Ctrl+A->X

    交叉编译

    1. 下载https://github.com/stephane/libmodbus/
    2. 安装生成配置的软件sudo apt-get install dh-autoreconf
    3. 生成配置文件./autogen.sh
    4. 配置./configure --host=arm-linux-gnueabihf --prefix=$(pwd)/__install
    5. 编译 make(如果遇到C++版本问题请升级交叉编译器)
    6. 安装 make install
    7. 查看生成的库文件类型file libmodbus.so.5.1.0
      libmodbus.so.5.1.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=d30e9af8a0ccc675f8549abbfa88e8df5574b196, with debug_info, not stripped
    8. 将库文件放到开发板的库文件目录下
      cd /usr/lib
      mv /root/libmodbus.so.5.1.0 ./
    9. 创建软连接
      ln -s libmodbus.so.5.1.0 libmodbus.so.5

    ./libmodbus.exe: /lib/libstdc++.so.6: version `GLIBCXX_3.4.21’ not found (required by ./libmodbus.exe)

    Ubuntu编译

    /usr/bin/ld: 找不到 -lmodbus collect2: error: ld returned 1 exit status

    ./a.out: error while loading shared libraries: libmodbus.so.5: cannot open shared object file: No such file or directory

    库文件的路径添加到 /etc/ld.so.conf 文件中,并运行 sudo ldconfig 命令来更新库缓存。例如,如果库文件位于 /home/lux/OpenSource/libmodbus/__install/lib 目录中,您可以将以下行添加到 /etc/ld.so.conf 文件的末尾:
    /home/lux/OpenSource/libmodbus/__install/lib
    保存文件后,运行 sudo ldconfig 命令。

    测试

    tcp_client

    #include "modbus_tcp.hpp"
    #include 
    
    int main(int argc, char *argv[])
    {
        uint8_t read_buffer[1024];
        uint8_t write_buffer[1024];
        uint16_t read_buffer_16[3];
        uint16_t write_buffer_16[1024];
        uint8_t ret = 0;
        modbus::modbus_tcp modbus("192.168.2.200",60000);  //创建一个modbus_tcp对象
    
        for (int i = 0; i < 10; i++)
        {
    
            
            ret = modbus.read_coil_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01   01 67 89 00 05 
            if (ret > 0)
            {
                printf("read_coil_status:%d\r\n",ret);
                for (int j = 0; j < ret; j++)
                {
                    printf("%02x ",read_buffer[j]);
                }
                printf("\r\n");
            }
            usleep(1000*100);
    
            ret = modbus.read_input_status(0x0000, 5, read_buffer);//00 01 00 00 00 06 01    01 67 89 00 05 
            
            if (ret > 0)
            {
                printf("read_input_status:%d\r\n",ret);
                for (int j = 0; j < ret; j++)
                {
                    printf("%02x ",read_buffer[j]);
                }
                printf("\r\n");
            }
            usleep(1000*100);
    
            ret = modbus.read_holding_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01   01 67 89 00 05
            if (ret > 0)
            {
                printf("read_holding_register:%d\r\n",ret); 
                for (int j = 0; j < ret; j++)
                {
                    printf("%02x ",read_buffer_16[j]);
                }
                printf("\r\n");
            }
            usleep(1000*100);
    
            ret = modbus.read_input_register(0x0000, 5, read_buffer_16);//00 01 00 00 00 06 01    04 67 89 00 05  
            if (ret > 0)
            {
                printf("read_input_register:%d\r\n",ret);
                for (int j = 0; j < ret; j++)
                {
                    printf("%02x ",read_buffer_16[j]);
                }
                printf("\r\n");
            }
            usleep(1000*100);
            
            // printf("write_single_coil:%d\r\n",modbus.write_single_coil(0x0000, 0x1234));
            // usleep(1000*100);
            // printf("write_single_register:%d\r\n",modbus.write_single_register(0x0000, 0x1234));//0x06:写单个保持寄存器: 00 01 00 00 00 06 01    06 67 89 12 34
            // usleep(1000*100);
            // printf("write_multiple_coil:%d\r\n",modbus.write_multiple_coil(0x0000, 5, write_buffer));//0x0F(15):写多个线圈: 00 01 00 00 00 08 01     0F 67 89 00 05 01 1F
            // usleep(1000*100);
            // printf("write_multiple_registers:%d\r\n",modbus.write_multiple_registers(0x0000, 5,  write_buffer_16));//0x10(16):写多个保持寄存器: 00 01 00 00 00 11 01   10 67 89 00 05 0A 00 00 00 00 00 00 00 00 00 05 
            // usleep(1000*100);
        }
        usleep(1000*500);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    modbus_slave

    在这里插入图片描述

  • 相关阅读:
    五种多目标优化算法(MOFA、NSWOA、MOJS、MOAHA、MOPSO)性能对比(提供MATLAB代码)
    Linux操作系统——硬盘的挂载和卸载
    Analyze 菜单分析
    VLC架构及流程分析
    【附源码】计算机毕业设计java支持协作知识建构的Python程序设计课程学习活动平台设计与实现
    【DRAM存储器十六】DDR2介绍-DDR到DDR2的变化、DDR2框图详解、模式寄存器
    手撕 视觉slam14讲 ch7 / pose_estimation_3d2d.cpp (1)
    spring框架Bean的作用域?对需要保持会话状态的bean应使用prototype作用域?为啥?
    docker 服务自动重启
    java排班算法-几班几倒
  • 原文地址:https://blog.csdn.net/weixin_43739167/article/details/133278472