一、源码下载
libmodbus官网是libmodbus,首页就是个很霸气的标语,我必须copy过来“The most popular Open Source library to communicate with Modbus devices.”
点击红色标识处进入下载页面:
最新版的是v3.1.10,把gz源码文件下载下来。
二、源码编译
将下载的libmodbus-3.1.10.tar.gz源码传输到X86 PC编译环境(具体环境搭建在上文已经介绍),解压源码,可以顺便看看相关文件树:
root@EliteDesk800:~$ tar -xzvf libmodbus-3.1.10.tar.gz libmodbus-3.1.10/configure.ac libmodbus-3.1.10/libmodbus.pc.in libmodbus-3.1.10/configure libmodbus-3.1.10/AUTHORS libmodbus-3.1.10/tests/ libmodbus-3.1.10/config.h.in libmodbus-3.1.10/COPYING.LESSER libmodbus-3.1.10/Makefile.am libmodbus-3.1.10/README.md libmodbus-3.1.10/NEWS libmodbus-3.1.10/m4/ libmodbus-3.1.10/Makefile.in libmodbus-3.1.10/aclocal.m4 libmodbus-3.1.10/src/ libmodbus-3.1.10/src/modbus-private.h libmodbus-3.1.10/src/modbus-data.c libmodbus-3.1.10/src/modbus.c libmodbus-3.1.10/src/modbus-tcp.c libmodbus-3.1.10/src/modbus-rtu.h libmodbus-3.1.10/src/modbus-version.h libmodbus-3.1.10/src/Makefile.am libmodbus-3.1.10/src/modbus-version.h.in libmodbus-3.1.10/src/modbus-rtu.c libmodbus-3.1.10/src/modbus-tcp.h libmodbus-3.1.10/src/win32/ libmodbus-3.1.10/src/modbus.h libmodbus-3.1.10/src/modbus-tcp-private.h libmodbus-3.1.10/src/Makefile.in libmodbus-3.1.10/src/modbus-rtu-private.h libmodbus-3.1.10/src/win32/modbus.dll.manifest.in libmodbus-3.1.10/m4/ltversion.m4 libmodbus-3.1.10/m4/libtool.m4 libmodbus-3.1.10/m4/ltoptions.m4 libmodbus-3.1.10/m4/ltsugar.m4 libmodbus-3.1.10/m4/lt~obsolete.m4 libmodbus-3.1.10/tests/random-test-client.c libmodbus-3.1.10/tests/LICENSE libmodbus-3.1.10/tests/bandwidth-server-one.c libmodbus-3.1.10/tests/unit-test.h libmodbus-3.1.10/tests/unit-test-client.c libmodbus-3.1.10/tests/bandwidth-server-many-up.c libmodbus-3.1.10/tests/Makefile.am libmodbus-3.1.10/tests/README.md libmodbus-3.1.10/tests/unit-test-server.c libmodbus-3.1.10/tests/unit-tests.sh libmodbus-3.1.10/tests/version.c libmodbus-3.1.10/tests/bandwidth-client.c libmodbus-3.1.10/tests/Makefile.in libmodbus-3.1.10/tests/random-test-server.c libmodbus-3.1.10/build-aux/install-sh libmodbus-3.1.10/build-aux/ltmain.sh libmodbus-3.1.10/build-aux/config.guess libmodbus-3.1.10/build-aux/depcomp libmodbus-3.1.10/build-aux/missing libmodbus-3.1.10/build-aux/config.sub libmodbus-3.1.10/build-aux/compile libmodbus-3.1.10/build-aux/test-driver
进入libmodbus-3.1.10文件夹执行 makedir -p install创建install目录。
然后执行./configure ac_cv_func_malloc_0_nonnull=yes --host=arm-linux-gnueabi --enable-static --prefix=$(pwd)/install/ 进行配置,host选择arm-linux-gnueabi适合T113的Cortex-A7内核。
配置执行完毕后显示如下:
后面也没啥,直接make & make install即可。
root@EliteDesk800:~/libmodbus-3.1.10$ 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 root@EliteDesk800:~/libmodbus-3.1.10$ make install
最后生成lib都在,具体如下:
root@EliteDesk800:~/libmodbus-3.1.10$ file install/lib/ libmodbus.a libmodbus.la libmodbus.so libmodbus.so.5 libmodbus.so.5.1.0 pkgconfig/
看看文件,“ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV)”文件没错:
root@EliteDesk800:~/libmodbus-3.1.10$ file install/lib/libmodbus.* install/lib/libmodbus.a: current ar archive install/lib/libmodbus.la: libtool library file, ASCII text install/lib/libmodbus.so: symbolic link to libmodbus.so.5.1.0 install/lib/libmodbus.so.5: symbolic link to libmodbus.so.5.1.0 install/lib/libmodbus.so.5.1.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=34c254066cb77dc6565cc41963f872b90e5dbefc, with debug_info, not stripped
然后把所有的libmodbus文件拷贝到米尔-全志T113-i开发板的/lib目录下。
三、测试
libmodbus.so已经编成,接下来就是测试是否能正常工作。
(一)选择串口
MYC-YT113i 核心板处理器拥有高达 6 路串口。由于芯片的管脚复用关系,默认配置了 2 路串口,UART4 和 UART5。
可以从米尔-全志T113-i开发板的系统设备上印证。
那就直接选择UART4吧,找到UART4的针脚即可。在米尔-全志T113-i开发板的硬件手册中找到。
红框处的3个pin,反咋GND也要接。
具体连接如图。
(二)测试demo
具体代码如下,在X86 PC上编译好,SCP到米尔-全志T113-i开发板。
#include < errno.h > #include < modbus.h > #include < stdio.h > #include < stdlib.h > #include < string.h > #include < unistd.h > // clang-format off #ifdef _WIN32 # include < winsock2.h > #else # include < sys/socket.h > #endif /* For MinGW */ #ifndef MSG_NOSIGNAL # define MSG_NOSIGNAL 0 #endif // clang-format on #include "unit-test.h" enum { TCP, TCP_PI, RTU }; int main(int argc, char *argv[]) { int s = -1; modbus_t *ctx; modbus_mapping_t *mb_mapping; int rc; int i; int use_backend; uint8_t *query; int header_length; char *ip_or_device; if (argc > 1) { if (strcmp(argv[1], "tcp") == 0) { use_backend = TCP; } else if (strcmp(argv[1], "tcppi") == 0) { use_backend = TCP_PI; } else if (strcmp(argv[1], "rtu") == 0) { use_backend = RTU; } else { printf("Modbus server for unit testing.n"); printf("Usage:n %s [tcp|tcppi|rtu] [< ip or device >]n", argv[0]); printf("Eg. tcp 127.0.0.1 or rtu /dev/ttyUSB0nn"); return -1; } } else { /* By default */ use_backend = TCP; } if (argc > 2) { ip_or_device = argv[2]; } else { switch (use_backend) { case TCP: ip_or_device = "127.0.0.1"; break; case TCP_PI: ip_or_device = "::1"; break; case RTU: ip_or_device = "/dev/ttyUSB0"; break; default: break; } } if (use_backend == TCP) { ctx = modbus_new_tcp(ip_or_device, 1502); query = malloc(MODBUS_TCP_MAX_ADU_LENGTH); } else if (use_backend == TCP_PI) { ctx = modbus_new_tcp_pi(ip_or_device, "1502"); query = malloc(MODBUS_TCP_MAX_ADU_LENGTH); } else { ctx = modbus_new_rtu(ip_or_device, 115200, 'N', 8, 1); modbus_set_slave(ctx, SERVER_ID); query = malloc(MODBUS_RTU_MAX_ADU_LENGTH); } header_length = modbus_get_header_length(ctx); modbus_set_debug(ctx, TRUE); mb_mapping = modbus_mapping_new_start_address(UT_BITS_ADDRESS, UT_BITS_NB, UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB, UT_REGISTERS_ADDRESS, UT_REGISTERS_NB_MAX, UT_INPUT_REGISTERS_ADDRESS, UT_INPUT_REGISTERS_NB); if (mb_mapping == NULL) { fprintf(stderr, "Failed to allocate the mapping: %sn", modbus_strerror(errno)); modbus_free(ctx); return -1; } /* Examples from PI_MODBUS_300.pdf. Only the read-only input values are assigned. */ /* Initialize input values that's can be only done server side. */ modbus_set_bits_from_bytes( mb_mapping- >tab_input_bits, 0, UT_INPUT_BITS_NB, UT_INPUT_BITS_TAB); /* Initialize values of INPUT REGISTERS */ for (i = 0; i < UT_INPUT_REGISTERS_NB; i++) { mb_mapping- >tab_input_registers[i] = UT_INPUT_REGISTERS_TAB[i]; } if (use_backend == TCP) { s = modbus_tcp_listen(ctx, 1); modbus_tcp_accept(ctx, &s); } else if (use_backend == TCP_PI) { s = modbus_tcp_pi_listen(ctx, 1); modbus_tcp_pi_accept(ctx, &s); } else { rc = modbus_connect(ctx); if (rc == -1) { fprintf(stderr, "Unable to connect %sn", modbus_strerror(errno)); modbus_free(ctx); return -1; } } for (;;) { do { rc = modbus_receive(ctx, query); /* Filtered queries return 0 */ } while (rc == 0); /* The connection is not closed on errors which require on reply such as bad CRC in RTU. */ if (rc == -1 && errno != EMBBADCRC) { /* Quit */ break; } /* Special server behavior to test client */ if (query[header_length] == 0x03) { /* Read holding registers */ if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3) == UT_REGISTERS_NB_SPECIAL) { printf("Set an incorrect number of valuesn"); MODBUS_SET_INT16_TO_INT8( query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1); } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) == UT_REGISTERS_ADDRESS_SPECIAL) { printf("Reply to this special register address by an exceptionn"); modbus_reply_exception(ctx, query, MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY); continue; } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) { const int RAW_REQ_LENGTH = 5; uint8_t raw_req[] = {(use_backend == RTU) ? INVALID_SERVER_ID : 0xFF, 0x03, 0x02, 0x00, 0x00}; printf("Reply with an invalid TID or slaven"); modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t)); continue; } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) == UT_REGISTERS_ADDRESS_SLEEP_500_MS) { printf("Sleep 0.5 s before replyingn"); usleep(500000); } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1) == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) { /* Test low level only available in TCP mode */ /* Catch the reply and send reply byte a byte */ uint8_t req[] = "x00x1Cx00x00x00x05xFFx03x02x00x00"; int req_length = 11; int w_s = modbus_get_socket(ctx); if (w_s == -1) { fprintf(stderr, "Unable to get a valid socket in special testn"); continue; } /* Copy TID */ req[1] = query[1]; for (i = 0; i < req_length; i++) { printf("(%.2X)", req[i]); usleep(5000); rc = send(w_s, (const char *) (req + i), 1, MSG_NOSIGNAL); if (rc == -1) { break; } } continue; } } rc = modbus_reply(ctx, query, rc, mb_mapping); if (rc == -1) { break; } } printf("Quit the loop: %sn", modbus_strerror(errno)); if (use_backend == TCP) { if (s != -1) { close(s); } } modbus_mapping_free(mb_mapping); free(query); /* For RTU */ modbus_close(ctx); modbus_free(ctx); return 0; }
(三)运行
UART4接USB-TTL模块连PC,然后PC运行modbus-pull。
在米尔-全志T113-i开发板上打开测试程序。
root@myd-yt113-i:~# ./mod-server rtu /dev/ttyAS4 Opening /dev/ttyAS4 at 115200 bauds (N, 8, 1) Waiting for an indication...
modbus-pull读寄存器,设备id是demo里面写死的。
米尔-全志T113-i开发板侧的log显示已经收到modbus信息,不支持00地址的read,不管了,反正只测试libmodbus是否移植成功,业务上的错误先不管:
Waiting for an indication... < 11 >< 03 >< 00 >< 00 >< 00 >< 0A >< C7 >< 5D > Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... < 11 >< 03 >< 00 >< 00 >< 00 >< 0A >< C7 >< 5D > Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... < 11 >< 03 >< 00 >< 00 >< 00 >< 0A >< C7 >< 5D > Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... < 11 >< 03 >< 00 >< 00 >< 00 >< 0A >< C7 >< 5D > Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... < 11 >< 03 >< 00 >< 00 >< 00 >< 0A >< C7 >< 5D > Illegal data address 0x0 in read_registers [11][83][02][C1][34]
在modbus-pull侧的监视消息,对的上:
(四)结论
经过测试demo,证明本次移植libmodbus库到米尔-全志T113-i开发板是成功的。
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !