一、源碼下載
libmodbus官網(wǎng)是libmodbus,首頁(yè)就是個(gè)很霸氣的標(biāo)語(yǔ),我必須copy過(guò)來(lái)“The most popular Open Source library to communicate with Modbus devices.”
點(diǎn)擊紅色標(biāo)識(shí)處進(jìn)入下載頁(yè)面:
最新版的是v3.1.10,把gz源碼文件下載下來(lái)。
二、源碼編譯
將下載的libmodbus-3.1.10.tar.gz源碼傳輸?shù)絏86 PC編譯環(huán)境(具體環(huán)境搭建在上文已經(jīng)介紹),解壓源碼,可以順便看看相關(guān)文件樹(shù):
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
進(jìn)入libmodbus-3.1.10文件夾執(zhí)行 makedir -p install創(chuàng)建install目錄。
然后執(zhí)行./configure ac_cv_func_malloc_0_nonnull=yes --host=arm-linux-gnueabi --enable-static --prefix=$(pwd)/install/ 進(jìn)行配置,host選擇arm-linux-gnueabi適合T113的Cortex-A7內(nèi)核。
配置執(zhí)行完畢后顯示如下:
后面也沒(méi)啥,直接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)”文件沒(méi)錯(cuò):
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開(kāi)發(fā)板的/lib目錄下。
三、測(cè)試
libmodbus.so已經(jīng)編成,接下來(lái)就是測(cè)試是否能正常工作。
(一)選擇串口
MYC-YT113i 核心板處理器擁有高達(dá) 6 路串口。由于芯片的管腳復(fù)用關(guān)系,默認(rèn)配置了 2 路串口,UART4 和 UART5。
可以從米爾-全志T113-i開(kāi)發(fā)板的系統(tǒng)設(shè)備上印證。
那就直接選擇UART4吧,找到UART4的針腳即可。在米爾-全志T113-i開(kāi)發(fā)板的硬件手冊(cè)中找到。
紅框處的3個(gè)pin,反咋GND也要接。
具體連接如圖。
(二)測(cè)試demo
具體代碼如下,在X86 PC上編譯好,SCP到米爾-全志T113-i開(kāi)發(fā)板。
#include #include #include #include #include #include // clang-format off #ifdef _WIN32 # include #else # include #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] []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; }
(三)運(yùn)行
UART4接USB-TTL模塊連PC,然后PC運(yùn)行modbus-pull。
在米爾-全志T113-i開(kāi)發(fā)板上打開(kāi)測(cè)試程序。
root@myd-yt113-i:~# ./mod-server rtu /dev/ttyAS4 Opening /dev/ttyAS4 at 115200 bauds (N, 8, 1) Waiting for an indication...
modbus-pull讀寄存器,設(shè)備id是demo里面寫死的。
米爾-全志T113-i開(kāi)發(fā)板側(cè)的log顯示已經(jīng)收到modbus信息,不支持00地址的read,不管了,反正只測(cè)試libmodbus是否移植成功,業(yè)務(wù)上的錯(cuò)誤先不管:
Waiting for an indication... 11?>03?>00?>00?>00?>0A?>5D?> Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... 11?>03?>00?>00?>00?>0A?>5D?> Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... 11?>03?>00?>00?>00?>0A?>5D?> Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... 11?>03?>00?>00?>00?>0A?>5D?> Illegal data address 0x0 in read_registers [11][83][02][C1][34] Waiting for an indication... 11?>03?>00?>00?>00?>0A?>5D?> Illegal data address 0x0 in read_registers [11][83][02][C1][34]
在modbus-pull側(cè)的監(jiān)視消息,對(duì)的上:
(四)結(jié)論
經(jīng)過(guò)測(cè)試demo,證明本次移植libmodbus庫(kù)到米爾-全志T113-i開(kāi)發(fā)板是成功的。
審核編輯 黃宇
-
測(cè)試
+關(guān)注
關(guān)注
8文章
5303瀏覽量
126642 -
開(kāi)發(fā)板
+關(guān)注
關(guān)注
25文章
5050瀏覽量
97456 -
LibModbus
+關(guān)注
關(guān)注
0文章
8瀏覽量
7258 -
全志T113
+關(guān)注
關(guān)注
0文章
54瀏覽量
264
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論