RT-Thread 应用笔记 - libmodbus RTU 编译问题的解决

前言

  • 更新最新的RT-Thread 标准版后,发现libmodbus软件包,无法正常编译通过
  • 使用RT-Thread Studio编译,gcc的无法编译通过
  • 使用Keil MDK5编译,依旧是编译不通过

Keil MDK5 解决方法

  • 这里使用RT-Thread中的随意一个BSP:如rt-thread\bsp\stm32\stm32l475-atk-pandora

  • menuconfig配置,默认配置皆可

  • scons --target=mdk5构建工程,确保默认工程可以正常的编译
    RT-Thread 应用笔记 - libmodbus RTU 编译问题的解决

  • 开启libmodbus软件包,这里使用RTU模式,手动把软件包的例程copy到工程中
    RT-Thread 应用笔记 - libmodbus RTU 编译问题的解决

  • pkgs --update下载libmodbus软件包到本地

  • 重新构建工程,编译发现无法通过

Build started: Project: project
*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'rt-thread'
compiling modbus.c...
packages\libmodbus-latest\inc\modbus-private.h(84): error:  #20: identifier "fd_set" is undefined
      int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
packages\libmodbus-latest\src\modbus.c(150): warning:  #68-D: integer conversion resulted in a change of sign
          return MSG_LENGTH_UNDEFINED;
packages\libmodbus-latest\src\modbus.c(184): error:  #20: identifier "EBADF" is undefined
                  if ((errno == EBADF || errno == ECONNRESET || errno == EPIPE)) {
packages\libmodbus-latest\src\modbus.c(184): error:  #20: identifier "ECONNRESET" is undefined
                  if ((errno == EBADF || errno == ECONNRESET || errno == EPIPE)) {
packages\libmodbus-latest\src\modbus.c(184): error:  #20: identifier "EPIPE" is undefined
                  if ((errno == EBADF || errno == ECONNRESET || errno == EPIPE)) {
packages\libmodbus-latest\src\modbus.c(339): error:  #20: identifier "fd_set" is undefined
      fd_set rset;
packages\libmodbus-latest\src\modbus.c(355): warning:  #223-D: function "FD_ZERO" declared implicitly
      FD_ZERO(&rset);
packages\libmodbus-latest\src\modbus.c(356): warning:  #223-D: function "FD_SET" declared implicitly
      FD_SET(ctx->s, &rset);
packages\libmodbus-latest\src\modbus.c(381): error:  #20: identifier "ETIMEDOUT" is undefined
                  if (errno == ETIMEDOUT) {
packages\libmodbus-latest\src\modbus.c(384): error:  #20: identifier "EBADF" is undefined
                  } else if (errno == EBADF) {
packages\libmodbus-latest\src\modbus.c(395): error:  #20: identifier "ECONNRESET" is undefined
              errno = ECONNRESET;
packages\libmodbus-latest\src\modbus.c(402): error:  #20: identifier "ECONNRESET" is undefined
                  (errno == ECONNRESET || errno == ECONNREFUSED ||
packages\libmodbus-latest\src\modbus.c(402): error:  #20: identifier "ECONNREFUSED" is undefined
                  (errno == ECONNRESET || errno == ECONNREFUSED ||
packages\libmodbus-latest\src\modbus.c(403): error:  #20: identifier "EBADF" is undefined
                   errno == EBADF)) {
packages\libmodbus-latest\src\modbus.c(910): error:  #20: identifier "ENOPROTOOPT" is undefined
          errno = ENOPROTOOPT;
packages\libmodbus-latest\src\modbus.c(912): warning:  #111-D: statement is unreachable
          break;
packages\libmodbus-latest\src\modbus.c: 4 warnings, 12 errors
compiling modbus-rtu.c...
packages\libmodbus-latest\src\modbus-rtu.c(18): error:  #5: cannot open source input file "dfs_select.h": No such file or directory
  #include <dfs_select.h>
packages\libmodbus-latest\src\modbus-rtu.c: 0 warnings, 1 error
compiling serial.c...
..\..\..\components\drivers\serial\serial.c(890): error:  #20: identifier "speed_t" is undefined
      speed_t speed;
..\..\..\components\drivers\serial\serial.c(896): error:  #20: identifier "B2400" is undefined
      {B2400, BAUD_RATE_2400},
..\..\..\components\drivers\serial\serial.c(897): error:  #20: identifier "B4800" is undefined
      {B4800, BAUD_RATE_4800},
..\..\..\components\drivers\serial\serial.c(898): error:  #20: identifier "B9600" is undefined
      {B9600, BAUD_RATE_9600},
..\..\..\components\drivers\serial\serial.c(899): error:  #20: identifier "B19200" is undefined
      {B19200, BAUD_RATE_19200},
..\..\..\components\drivers\serial\serial.c(900): error:  #20: identifier "B38400" is undefined
      {B38400, BAUD_RATE_38400},
..\..\..\components\drivers\serial\serial.c(901): error:  #20: identifier "B57600" is undefined
      {B57600, BAUD_RATE_57600},
..\..\..\components\drivers\serial\serial.c(902): error:  #20: identifier "B115200" is undefined
      {B115200, BAUD_RATE_115200},
..\..\..\components\drivers\serial\serial.c(903): error:  #20: identifier "B230400" is undefined
      {B230400, BAUD_RATE_230400},
..\..\..\components\drivers\serial\serial.c(904): error:  #20: identifier "B460800" is undefined
      {B460800, BAUD_RATE_460800},
..\..\..\components\drivers\serial\serial.c(905): error:  #20: identifier "B921600" is undefined
      {B921600, BAUD_RATE_921600},
..\..\..\components\drivers\serial\serial.c(906): error:  #20: identifier "B2000000" is undefined
      {B2000000, BAUD_RATE_2000000},
..\..\..\components\drivers\serial\serial.c(907): error:  #20: identifier "B3000000" is undefined
      {B3000000, BAUD_RATE_3000000},
..\..\..\components\drivers\serial\serial.c(910): error:  #20: identifier "speed_t" is undefined
  static speed_t _get_speed(int baudrate)
..\..\..\components\drivers\serial\serial.c(920): error:  #20: identifier "B0" is undefined
      return B0;
..\..\..\components\drivers\serial\serial.c(923): error:  #20: identifier "speed_t" is undefined
  static int _get_baudrate(speed_t speed)
..\..\..\components\drivers\serial\serial.c(950): error:  #20: identifier "TCIFLUSH" is undefined
          case TCIFLUSH:
..\..\..\components\drivers\serial\serial.c(951): error:  #20: identifier "TCIOFLUSH" is undefined
          case TCIOFLUSH:
..\..\..\components\drivers\serial\serial.c(974): error:  #20: identifier "TCOFLUSH" is undefined
           case TCOFLUSH:
..\..\..\components\drivers\serial\serial.c(910): warning:  #177-D: function "_get_speed"  was declared but never referenced
  static speed_t _get_speed(int baudrate)
..\..\..\components\drivers\serial\serial.c(923): warning:  #177-D: function "_get_baudrate"  was declared but never referenced
  static int _get_baudrate(speed_t speed)
..\..\..\components\drivers\serial\serial.c(936): warning:  #177-D: function "_tc_flush"  was declared but never referenced
  static void _tc_flush(struct rt_serial_device *serial, int queue)
..\..\..\components\drivers\serial\serial.c: 3 warnings, 19 errors
".\build\keil\Obj\rt-thread.axf" - 32 Error(s), 7 Warning(s).
Target not created.
Build Time Elapsed:  00:00:01
  • 定位编译不通过的问题,确认为libmodbus的头文件包含问题
  • 工程配置:select
    RT-Thread 应用笔记 - libmodbus RTU 编译问题的解决

RT-Thread 应用笔记 - libmodbus RTU 编译问题的解决

libmodbus软件包修改如下:

  • modbus.c 中:#include <errno.h> -> #include <sys/errno.h>
  • modbus-rtu.c中:#include <dfs_select.h> -> #include <sys/select.h>
  • modbus-private.h 中:增加#include <sys/select.h>
  • 编译问题解决

小结

  • 可能最近内核规范化,修改了部分路径、头文件的包含需要更新
  • 可以通过VS Code,全局搜索无法识别的函数、定义等,然后包含相应的头文件即可
  • RT-Thread Studio的libmodubs软件包,编译不通过,可以参考上面的修改方法

参考例程

https://gitee.com/zhangsz0516/nucleo_l476rg_pm/tree/master/libmodbus_master

上一篇:s5pv210之路(3) --- 编译环境


下一篇:使用AFL对libmodbus进行fuzz测试