前言
本文介绍基于【米尔MYD-YG2LX开发板】在工控领域的应用
,modbus开发环境搭建。开发环境基于WSL2.
下载代码
git clone https://github.com/stephane/libmodbus.git
PC端
编译
sudo su 切换到root用户
cd libmodbus/
./autogen.sh
lhj@lhj:~/libmodbus$ ./autogen.sh
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'build-aux'.
libtoolize: linking file 'build-aux/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
libtoolize: linking file 'm4/libtool.m4'
libtoolize: linking file 'm4/ltoptions.m4'
libtoolize: linking file 'm4/ltsugar.m4'
libtoolize: linking file 'm4/ltversion.m4'
libtoolize: linking file 'm4/lt~obsolete.m4'
configure.ac:33: installing 'build-aux/compile'
configure.ac:56: installing 'build-aux/config.guess'
configure.ac:56: installing 'build-aux/config.sub'
configure.ac:32: installing 'build-aux/install-sh'
configure.ac:32: installing 'build-aux/missing'
src/Makefile.am: installing 'build-aux/depcomp'
parallel-tests: installing 'build-aux/test-driver'
------------------------------------------------------
Initialized build system. You can now run ./configure
------------------------------------------------------
lhj@lhj:~/libmodbus$
./configure && make install
tests目录下是测试Demo
root@lhj:/home/lhj/libmodbus# ls tests/
LICENSE bandwidth-client.o random-test-client unit-test-client unit-test.h.in
Makefile bandwidth-server-many-up random-test-client.c unit-test-client.c unit-tests.sh
Makefile.am bandwidth-server-many-up.c random-test-client.o unit-test-client.o version
Makefile.in bandwidth-server-many-up.o random-test-server unit-test-server version.c
README.md bandwidth-server-one random-test-server.c unit-test-server.c version.o
bandwidth-client bandwidth-server-one.c random-test-server.o unit-test-server.o
bandwidth-client.c bandwidth-server-one.o stamp-h2 unit-test.h
root@lhj:/home/lhj/libmodbus#
测试
make chexk
root@lhj:/home/lhj/libmodbus# make chexk
make: *** No rule to make target 'chexk'. Stop.
root@lhj:/home/lhj/libmodbus# make check
Making check in src
make[1]: Nothing to be done for 'check'.
Making check in tests
make check-TESTS
PASS: unit-tests.sh
============================================================================
Testsuite summary for libmodbus 3.1.10
============================================================================
# TOTAL: 1
# PASS: 1
# SKIP: 0
# XFAIL: 0
# FAIL: 0
# XPASS: 0
# ERROR: 0
============================================================================
root@lhj:/home/lhj/libmodbus#
开发板板端
编译
mkdir output
./configure --host=arm-linux-gnueabihf --prefix=/home/lhj/libmodbus/output --enable-shared --enable-static
make
root@lhj:/home/lhj/libmodbus# make
make --no-print-directory all-recursive
Making all in src
CC modbus.lo
CC modbus-data.lo
CC modbus-rtu.lo
CC modbus-tcp.lo
CCLD libmodbus.la
Making all in tests
make all-am
CC bandwidth-server-one.o
CCLD bandwidth-server-one
CC bandwidth-server-many-up.o
CCLD bandwidth-server-many-up
CC bandwidth-client.o
CCLD bandwidth-client
CC random-test-server.o
CCLD random-test-server
CC random-test-client.o
CCLD random-test-client
CC unit-test-server.o
CCLD unit-test-server
CC unit-test-client.o
CCLD unit-test-client
CC version.o
CCLD version
make[2]: Nothing to be done for 'all-am'.
make install
root@lhj:/home/lhj/libmodbus# make install
Making install in src
/usr/bin/mkdir -p '/home/lhj/libmodbus/output/lib'
/bin/sh ../libtool --mode=install /usr/bin/install -c libmodbus.la '/home/lhj/libmodbus/output/lib'
libtool: install: /usr/bin/install -c .libs/libmodbus.so.5.1.0 /home/lhj/libmodbus/output/lib/libmodbus.so.5.1.0
libtool: install: (cd /home/lhj/libmodbus/output/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/lhj/libmodbus/output/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/lhj/libmodbus/output/lib/libmodbus.la
libtool: install: /usr/bin/install -c .libs/libmodbus.a /home/lhj/libmodbus/output/lib/libmodbus.a
libtool: install: chmod 644 /home/lhj/libmodbus/output/lib/libmodbus.a
libtool: install: arm-linux-gnueabihf-ranlib /home/lhj/libmodbus/output/lib/libmodbus.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/sbin" ldconfig -n /home/lhj/libmodbus/output/lib
----------------------------------------------------------------------
Libraries have been installed in:
/home/lhj/libmodbus/output/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.
----------------------------------------------------------------------
/usr/bin/mkdir -p '/home/lhj/libmodbus/output/include/modbus'
/usr/bin/install -c -m 644 modbus.h modbus-version.h modbus-rtu.h modbus-tcp.h '/home/lhj/libmodbus/output/include/modbus'
Making install in tests
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Nothing to be done for 'install-exec-am'.
/usr/bin/mkdir -p '/home/lhj/libmodbus/output/share/doc/libmodbus'
/usr/bin/install -c -m 644 AUTHORS NEWS README.md '/home/lhj/libmodbus/output/share/doc/libmodbus'
/usr/bin/mkdir -p '/home/lhj/libmodbus/output/lib/pkgconfig'
/usr/bin/install -c -m 644 libmodbus.pc '/home/lhj/libmodbus/output/lib/pkgconfig'
root@lhj:/home/lhj/libmodbus#
部署
查看生成文件如下,将头文件和库文件导入到开发板即可
root@lhj:/home/lhj/libmodbus# tree output/
output/
|-- include
| `-- modbus
| |-- modbus-rtu.h
| |-- modbus-tcp.h
| |-- modbus-version.h
| `-- modbus.h
|-- lib
| |-- libmodbus.a
| |-- libmodbus.la
| |-- libmodbus.so -> libmodbus.so.5.1.0
| |-- libmodbus.so.5 -> libmodbus.so.5.1.0
| |-- libmodbus.so.5.1.0
| `-- pkgconfig
| `-- libmodbus.pc
`-- share
`-- doc
`-- libmodbus
|-- AUTHORS
|-- NEWS
`-- README.md
7 directories, 13 files
root@lhj:/home/lhj/libmodbus#
测试
cp tests/unit-test-server /mnt/d
cp tests/unit-test-client /mnt/d
然后导入到开发板
chmod +x unit-test-server unit-test-client
./unit-test-client
./unit-test-server
总结
本开发板接口资源丰富,性能强,特别适合工控领域的应用,比如实现modbus通讯。