一个modbus协议库移植——libmodbus-master

modbus协议是工业上常用的数据传输协议,在linux系统上,大家一般自己编写或者找别人的代码复用,代码质量无法保障,调试起来也比较费劲,最近发现github上有个非常好的modbus协议类库,经过测试发现很好用,支持串口和ip两种方式,下面记录下来往arm版移植(其实类库移植大同小异)的过程供大家参考,希望给大家节省一点时间。

1、代码下载

去github下载源代码:https://github.com/stephane/libmodbus

代码解压并且拷贝到交叉编译用户的目录下



2、编译

首先运行./autogen.sh 生成 configure脚本;

然后运行./configure --help

从帮助信息可以看到如何设置编译生成目录和交叉编译项设置。

mkdir limodbus-release ---生成编译生成文件存放目录

./configure --host=交叉编译前缀 --prefix=编译生成文件放置目录

我自己的开发环境相关参数是:

./configure --host=arm-linux --prefix=/home/ccsl/libmodbus-master-linux/libmodbus-master-linux/limodbus-release

配置最后提示

       libmodbus 3.1.1
        ===============


        prefix:                 /home/ccsl/libmodbus-master-linux/libmodbus-master-linux/limodbus-release
        sysconfdir:             ${prefix}/etc
        libdir:                 ${exec_prefix}/lib
        includedir:             ${prefix}/include


        compiler:               arm-linux-gcc -std=gnu99
        cflags:                 -g -O2
        ldflags:                


可以查看生成目录和编译器是否设置正确,如果不正确的话请重新设置

再然后执行

make & make install

中间或提示生成的文件安装目录,以及如何使用生成的lib文件,如下所示:

-------------------------------------------------------------------------------------------------------------

Libraries have been installed in:
   /home/ccsl/libmodbus-master-linux/libmodbus-master-linux/limodbus-release/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.

-------------------------------------------------------------------------------------------------------------


完成后到你的目录下查看生成的目录和文件:


lib文件下面就是动态库了,压缩生成的库

tar czf libmodbus-linux.tar.gz lib/libmodbus.*

通过nfs或者什么方法把动态库放到arm的/lib目录下,就可以开始使用了


3、测试

libmodbus-master提供了测试程序在tests下,

首先把random-test-server.c里面23行  ctx = modbus_new_tcp("127.0.0.1", 1502); 改成 ctx = modbus_new_tcp(NULL, 1502);,意思是说server监控所有的ip地址,端口是1502,然后编译

arm-linux-gcc random-test-server.c -o random-test-server '-I/home/ccsl/libmodbus-master-linux/libmodbus-master-linux/limodbus-release/include/modbus  -L/home/ccsl/libmodbus-master-linux/libmodbus-master-linux/limodbus-release/lib -lmodbus'

把random-test-server放到板子上运行


好了,arm版上的环境就搭好了,为了测试一下,我们在编译机上作为client

首先在你的编译机上新建一个目录(最好是换个用户下面),按照上面的过程再来一遍,但是生成makefile文件的时候什么都不要加

./configure 

然后make & make install

到tests目录下面

random-test-client.c文件 第60行:ctx = modbus_new_tcp("127.0.0.1", 1502); 里面的地址改成你的arm板子的地址,编译一下:

gcc random-test-client.c -o random-test-client `pkg-config --libs --cflags libmodbus`

这样就生成了:random-test-client

运行./random-test-client

就可以看到输出了


到这里就介绍完了,希望对大家有帮助!



你可能感兴趣的:(Linux)