源码:git clone https://github.com/stephane/libmodbus.git
或者直接用下载好的附件:
*附件:libmodbus-master.zip
这里想直接在开发板编译,结果发现系统中缺少autoreconf 工具。无法正常编译
最终还是在提供的虚拟机开发环境中编译:
解压缩源码压缩包:unzip libmodbus-master.zip
进入源码目录 cd libbodbus-master
完整打印如下:
./configure --host=loongarch64-linux-gnu --enable-static --prefix=$(pwd)/install/
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for loongarch64-linux-gnu-strip... loongarch64-linux-gnu-strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking how to create a pax tar archive... gnutar
checking for loongarch64-linux-gnu-gcc... loongarch64-linux-gnu-gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... yes
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether loongarch64-linux-gnu-gcc accepts -g... yes
checking for loongarch64-linux-gnu-gcc option to accept ISO C89... none needed
checking whether loongarch64-linux-gnu-gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of loongarch64-linux-gnu-gcc... gcc3
checking how to run the C preprocessor... loongarch64-linux-gnu-gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... no
checking whether make supports nested variables... (cached) yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... loongarch64-unknown-linux-gnu
checking how to print strings... printf
checking for a sed that does not truncate output... /bin/sed
checking for fgrep... /bin/grep -F
checking for ld used by loongarch64-linux-gnu-gcc... /opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld
checking if the linker (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/bin/loongarch64-linux-gnu-nm -B
checking the name lister (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/bin/loongarch64-linux-gnu-nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking how to convert x86_64-pc-linux-gnu file names to loongarch64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-pc-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld option to reload object files... -r
checking for loongarch64-linux-gnu-objdump... loongarch64-linux-gnu-objdump
checking how to recognize dependent libraries... pass_all
checking for loongarch64-linux-gnu-dlltool... no
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\\n
checking for loongarch64-linux-gnu-ar... loongarch64-linux-gnu-ar
checking for archiver @FILE support... @
checking for loongarch64-linux-gnu-strip... (cached) loongarch64-linux-gnu-strip
checking for loongarch64-linux-gnu-ranlib... loongarch64-linux-gnu-ranlib
checking command to parse /opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/bin/loongarch64-linux-gnu-nm -B output from loongarch64-linux-gnu-gcc object... ok
checking for sysroot... no
checking for a working dd... /bin/dd
checking how to truncate binary pipes... /bin/dd bs=4096 count=1
checking for loongarch64-linux-gnu-mt... no
checking for mt... mt
configure: WARNING: using cross tools not prefixed with host triplet
checking if mt is a manifest tool... no
checking for dlfcn.h... yes
checking for objdir... .libs
checking if loongarch64-linux-gnu-gcc supports -fno-rtti -fno-exceptions... no
checking for loongarch64-linux-gnu-gcc option to produce PIC... -fPIC -DPIC
checking if loongarch64-linux-gnu-gcc PIC flag -fPIC -DPIC works... yes
checking if loongarch64-linux-gnu-gcc static flag -static works... yes
checking if loongarch64-linux-gnu-gcc supports -c -o file.o... yes
checking if loongarch64-linux-gnu-gcc supports -c -o file.o... (cached) yes
checking whether the loongarch64-linux-gnu-gcc linker (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking arpa/inet.h usability... yes
checking arpa/inet.h presence... yes
checking for arpa/inet.h... yes
checking byteswap.h usability... yes
checking byteswap.h presence... yes
checking for byteswap.h... yes
checking errno.h usability... yes
checking errno.h presence... yes
checking for errno.h... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking limits.h usability... yes
checking limits.h presence... yes
checking for limits.h... yes
checking linux/serial.h usability... yes
checking linux/serial.h presence... yes
checking for linux/serial.h... yes
checking netdb.h usability... yes
checking netdb.h presence... yes
checking for netdb.h... yes
checking netinet/in.h usability... yes
checking netinet/in.h presence... yes
checking for netinet/in.h... yes
checking netinet/tcp.h usability... yes
checking netinet/tcp.h presence... yes
checking for netinet/tcp.h... yes
checking sys/ioctl.h usability... yes
checking sys/ioctl.h presence... yes
checking for sys/ioctl.h... yes
checking sys/params.h usability... no
checking sys/params.h presence... no
checking for sys/params.h... no
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for sys/types.h... (cached) yes
checking termios.h usability... yes
checking termios.h presence... yes
checking for termios.h... yes
checking time.h usability... yes
checking time.h presence... yes
checking for time.h... yes
checking for unistd.h... (cached) yes
checking whether __CYGWIN__ is declared... no
checking for library containing accept... none required
checking for accept4... yes
checking for getaddrinfo... yes
checking for gettimeofday... yes
checking for inet_pton... yes
checking for inet_ntop... yes
checking for select... yes
checking for socket... yes
checking for strerror... yes
checking for strlcpy... no
checking for inline... inline
checking for loongarch64-linux-gnu-g++... loongarch64-linux-gnu-g++
checking whether we are using the GNU C++ compiler... yes
checking whether loongarch64-linux-gnu-g++ accepts -g... yes
checking dependency style of loongarch64-linux-gnu-g++... gcc3
checking how to run the C++ preprocessor... loongarch64-linux-gnu-g++ -E
checking for ld used by loongarch64-linux-gnu-g++... /opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld
checking if the linker (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld) is GNU ld... yes
checking whether the loongarch64-linux-gnu-g++ linker (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld) supports shared libraries... yes
checking for loongarch64-linux-gnu-g++ option to produce PIC... -fPIC -DPIC
checking if loongarch64-linux-gnu-g++ PIC flag -fPIC -DPIC works... yes
checking if loongarch64-linux-gnu-g++ static flag -static works... yes
checking if loongarch64-linux-gnu-g++ supports -c -o file.o... yes
checking if loongarch64-linux-gnu-g++ supports -c -o file.o... (cached) yes
checking whether the loongarch64-linux-gnu-g++ linker (/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/loongarch64-linux-gnu/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... (cached) GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking for int64_t... yes
checking for size_t... yes
checking for ssize_t... yes
checking for uint16_t... yes
checking for uint32_t... yes
checking for uint8_t... yes
checking winsock2.h usability... no
checking winsock2.h presence... no
checking for winsock2.h... no
checking whether TIOCSRS485 is declared... yes
checking whether TIOCM_RTS is declared... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/modbus-version.h
config.status: creating src/win32/modbus.dll.manifest
config.status: creating tests/Makefile
config.status: creating libmodbus.pc
config.status: creating config.h
config.status: creating tests/unit-test.h
config.status: executing depfiles commands
config.status: executing libtool commands
libmodbus 3.1.10
===============
prefix: /home/loongson/Desktop/libmodbus-master/install
sysconfdir: ${prefix}/etc
libdir: ${exec_prefix}/lib
includedir: ${prefix}/include
compiler: loongarch64-linux-gnu-gcc
cflags: -g -O2
ldflags:
tests: yes
loongson@loongson-virtual-machine:~/Desktop/libmodbus-master$
要确保使用的是指定的编译器才可以。
loongson@loongson-virtual-machine:~/Desktop/libmodbus-master$ make -j4
make --no-print-directory all-recursive
Making all in src
CC modbus-rtu.lo
CC modbus.lo
CC modbus-data.lo
CC modbus-tcp.lo
CCLD libmodbus.la
loongarch64-linux-gnu-ar: `u' modifier ignored since `D' is the default (see `U')
Making all in tests
make all-am
CC bandwidth-server-one.o
CC bandwidth-server-many-up.o
CC bandwidth-client.o
CC random-test-server.o
CC random-test-client.o
CC unit-test-server.o
CC unit-test-client.o
CC version.o
CCLD bandwidth-server-one
CCLD bandwidth-server-many-up
CCLD bandwidth-client
CCLD random-test-server
CCLD random-test-client
CCLD unit-test-server
CCLD version
CCLD unit-test-client
loongson@loongson-virtual-machine:~/Desktop/libmodbus-master$
loongson@loongson-virtual-machine:~/Desktop/libmodbus-master$ make install
Making install in src
/bin/mkdir -p '/home/loongson/Desktop/libmodbus-master/install/lib'
/bin/bash ../libtool --mode=install /usr/bin/install -c libmodbus.la '/home/loongson/Desktop/libmodbus-master/install/lib'
libtool: install: /usr/bin/install -c .libs/libmodbus.so.5.1.0 /home/loongson/Desktop/libmodbus-master/install/lib/libmodbus.so.5.1.0
libtool: install: (cd /home/loongson/Desktop/libmodbus-master/install/lib && { ln -s -f libmodbus.so.5.1.0 libmodbus.so.5 || { rm -f libmodbus.so.5 && ln -s libmodbus.so.5.1.0 libmodbus.so.5; }; })
libtool: install: (cd /home/loongson/Desktop/libmodbus-master/install/lib && { ln -s -f libmodbus.so.5.1.0 libmodbus.so || { rm -f libmodbus.so && ln -s libmodbus.so.5.1.0 libmodbus.so; }; })
libtool: install: /usr/bin/install -c .libs/libmodbus.lai /home/loongson/Desktop/libmodbus-master/install/lib/libmodbus.la
libtool: install: /usr/bin/install -c .libs/libmodbus.a /home/loongson/Desktop/libmodbus-master/install/lib/libmodbus.a
libtool: install: chmod 644 /home/loongson/Desktop/libmodbus-master/install/lib/libmodbus.a
libtool: install: loongarch64-linux-gnu-ranlib /home/loongson/Desktop/libmodbus-master/install/lib/libmodbus.a
libtool: finish: PATH="/opt/loongson-gnu-toolchain-8.3-x86_64-loongarch64-linux-gnu-rc1.3-1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/sbin" ldconfig -n /home/loongson/Desktop/libmodbus-master/install/lib
----------------------------------------------------------------------
Libraries have been installed in:
/home/loongson/Desktop/libmodbus-master/install/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/bin/mkdir -p '/home/loongson/Desktop/libmodbus-master/install/include/modbus'
/usr/bin/install -c -m 644 modbus.h modbus-version.h modbus-rtu.h modbus-tcp.h '/home/loongson/Desktop/libmodbus-master/install/include/modbus'
Making install in tests
make[2]: 对“install-exec-am”无需做任何事。
make[2]: 对“install-data-am”无需做任何事。
make[2]: 对“install-exec-am”无需做任何事。
/bin/mkdir -p '/home/loongson/Desktop/libmodbus-master/install/share/doc/libmodbus'
/usr/bin/install -c -m 644 AUTHORS NEWS README.md '/home/loongson/Desktop/libmodbus-master/install/share/doc/libmodbus'
/bin/mkdir -p '/home/loongson/Desktop/libmodbus-master/install/lib/pkgconfig'
/usr/bin/install -c -m 644 libmodbus.pc '/home/loongson/Desktop/libmodbus-master/install/lib/pkgconfig'
安装完成后,在刚刚创建的install文件夹中会有lib库及头文件
进入lib库目录,可以看到生成的静态库和动态库文件,将libmodbus.a静态库文件复制到开发板的lib/目录下即可。
在开发虚拟上编写好测试程序,生成可执行文件,然后上传到开发板上即可开启测试。
使用方法:
下载程序附件到虚拟机开发环境中,然后make即可生成可执行程序。或者直接解压缩后将其中的可执行程序上传到开发板即可。
*附件:modbus-test.zip
1、程序包含了RTU协议的主机和从机模式,可以启动程序后自由选择。
2、串口号通过参数带入。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include "modbus.h" //modbus动态库文件
int main(int argc, char *argv[])
{
// 1.read ttyS number
if (argc < 2)
{
fprintf(stderr, "Usage: %s <ttyS_device>\\n", argv[0]);
return -1;
}
char tty_device[20];
snprintf(tty_device, sizeof(tty_device), "/dev/%s", argv[1]);
// 创建modbus对象
modbus_t *modbusContext = nullptr;
// 初始化主站的相关寄存器
modbus_mapping_t *mb_mapping = NULL;
mb_mapping = modbus_mapping_new(10, 10, 10, 10);
if (mb_mapping == NULL)
{
fprintf(stderr, "Failed to allocate the mapping: %s\\n", modbus_strerror(errno));
modbus_free(modbusContext);
return -1;
}
// 初始化 - 设备号、波特率、校验位、数据位、停止位
modbusContext = modbus_new_rtu(tty_device, 115200, 'N', 8, 1);
if (modbusContext == nullptr)
{
std::cerr << "Failed to create Modbus context" << std::endl;
return 1;
}
// 从机 ID
const int slaveId = 1;
modbus_set_debug(modbusContext, 1); // 设置1可看到调试信息
modbus_set_slave(modbusContext, slaveId); // 设置slave ID
// 打开 Modbus 连接
if (modbus_connect(modbusContext) == -1)
{
std::cerr << "Modbus connection failed: " << modbus_strerror(errno) << std::endl;
modbus_free(modbusContext);
return 1;
}
std::cout << "Choose Modbus mode:" << std::endl;
std::cout << "1. Modbus-RTU 主机模式" << std::endl;
std::cout << "2. Modbus-RTU 从机模式" << std::endl;
int choice;
std::cin >> choice;
uint16_t data = 0;
int rc = -1;
int64_t slave_ok = 0, slave_erro = 0;
int64_t poll_ok = 0, poll_erro = 0;
int64_t cyc_cont = 0;
while (1)
{
cyc_cont++;
switch (choice)
{
case 1:
{
std::cout << "-----------主机模式----------" << std::endl;
uint16_t read_buffer_16[1024]; // 定义read_buffer_16
// 0x03:读保持寄存器: [01][03][00][00][00][0A][C5][CD]
// recive:<01><03><14><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><00><A3><67>
rc = modbus_read_registers(modbusContext, 0, 10, read_buffer_16);
if (rc == -1)
{
slave_erro ++;
std::cerr << "erro: " << modbus_strerror(errno) << std::endl;
}
else
{
slave_ok ++;
for (int i = 0; i < 10; i++)
{
printf("red16[%d] = %d(0x%x)\\n", i, read_buffer_16[i], read_buffer_16[i]);
}
}
std::cout <<"send:"<<cyc_cont*8<<" read_ok:" << slave_ok*25 << " erro:" << slave_erro << std::endl;
break;
}
case 2:
{
std::cout << "-----------从机模式----------" << std::endl;
for (int i = 0; i < 10; i++)
{
mb_mapping->tab_registers[i] = data++;
}
if (data > 0XEEEE)
data = 0;
uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
if (modbusContext)
{
rc = modbus_receive(modbusContext, query);
if (rc > 0)
{
poll_ok ++;
modbus_reply(modbusContext, query, rc, mb_mapping);
}
else if (rc == -1)
{
poll_erro ++;
std::cerr << "erro: " << modbus_strerror(errno) << std::endl;
}
}
std::cout <<"send:"<<cyc_cont*25<< " read_ok:" << poll_ok*8 << " erro:" << poll_erro << std::endl;
break;
}
default:
break;
}
}
modbus_mapping_free(mb_mapping);
// 关闭 Modbus 连接
modbus_close(modbusContext);
modbus_free(modbusContext);
return 0;
}
测试方法:
1、选择待测试的两个串口,用杜邦线连接,串口1的Rx,Tx连接串口2的Tx,Rx;
2、开两个终端,启动程序./modus_test ttyS2 和./modus_test ttyS3,(程序后面带的参数是设备号,可选ttyS1,ttyS2,ttyS3)
3、程序启动后需要选择运行模式:主机、从机,一个程序选主机、另一个选从机即可。
4、两个串口开始自动的收发数据,并记录各自的收发量及rtu协议错误的数量。
经过30分钟的测试,结果如下:
串口1:发送:2960784 Byte 接收:9252450 Byte 协议错误:0
串口2:发送:9252450 Byte 接收:2960784 Byte 协议错误:0
串口收发稳定、数量一致,未出现丢包等现象。
更多回帖