/********************************************************************************* * mbpoll Test FreeModbus TCP Demo * 说明: * 使用mbpoll工具对FreeModbus进行测试,本文仅仅涉及编译以及简单测试。FreeModbus * 默认没有打开网络传输功能,需要手动打开。 * * 2018-1-2 深圳 南山平山村 曾剑锋 ********************************************************************************/ 一、参考文档: epsilonrt/mbpoll https://github.com/epsilonrt/mbpoll 二、Compile/Install: root@localhost:/home/zengjf/freemodbus# git clone https://github.com/epsilonrt/mbpoll.git Cloning into 'mbpoll'... remote: Counting objects: 263, done. remote: Total 263 (delta 0), reused 0 (delta 0), pack-reused 263 Receiving objects: 100% (263/263), 6.23 MiB | 0 bytes/s, done. Resolving deltas: 100% (135/135), done. Checking connectivity... done. root@localhost:/home/zengjf/freemodbus# cd mbpoll root@localhost:/home/zengjf/freemodbus/mbpoll# ls 3rdparty CMakeLists.txt LICENCE NEWS README_FR.md mbpoll.pro mbpo AUTHORS INSTALL LICENSE README.md TODO mbpoll.project pack root@localhost:/home/zengjf/freemodbus/mbpoll# mkdir build root@localhost:/home/zengjf/freemodbus/mbpoll# cd build/ root@localhost:/home/zengjf/freemodbus/mbpoll/build# cmake .. -- The C compiler identification is GNU 4.9.3 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/zengjf/freemodbus/mbpoll/build root@localhost:/home/zengjf/freemodbus/mbpoll/build# ls CMakeCache.txt CPackConfig.cmake Makefile CMakeFiles CPackSourceConfig.cmake cmake_install.cmake root@localhost:/home/zengjf/freemodbus/mbpoll/build# make Scanning dependencies of target mbpoll [ 12%] Building C object CMakeFiles/mbpoll.dir/src/mbpoll.c.o [ 25%] Building C object CMakeFiles/mbpoll.dir/3rdparty/modbus/modbus.c.o [ 37%] Building C object CMakeFiles/mbpoll.dir/3rdparty/modbus/modbus-data.c.o [ 50%] Building C object CMakeFiles/mbpoll.dir/3rdparty/modbus/modbus-tcp.c.o [ 62%] Building C object CMakeFiles/mbpoll.dir/3rdparty/modbus/modbus-rtu.c.o [ 75%] Building C object CMakeFiles/mbpoll.dir/3rdparty/sysio/delay.c.o [ 87%] Building C object CMakeFiles/mbpoll.dir/3rdparty/sysio/serial.c.o [100%] Linking C executable mbpoll [100%] Built target mbpoll root@localhost:/home/zengjf/freemodbus/mbpoll/build# make install [100%] Built target mbpoll Install the project... -- Install configuration: "" -- Installing: /usr/local/bin/mbpoll root@localhost:/home/zengjf/freemodbus/mbpoll/build# mbpoll -h usage : mbpoll [ options ] device|host [ writevalues... ] [ options ] ModBus Master Simulator. It allows to read and write in ModBus slave registers connected by serial (RTU only) or TCP. Arguments : device Serial port when using ModBus RTU protocol COM1, COM2 ... on Windows /dev/ttyS0, /dev/ttyS1 ... on Linux /dev/ser1, /dev/ser2 ... on QNX host Host name or dotted IP address when using ModBus/TCP protocol writevalues List of values to be written. If none specified (default) mbpoll reads data. If negative numbers are provided, it will precede the list of data to be written by two dashes ('--'). for example : mbpoll -t4:int /dev/ttyUSB0 -- 123 -1568 8974 -12 General options : -m # mode (rtu or tcp, TCP is default) -a # Slave address (1-255 for rtu, 0-255 for tcp, 1 is default) for reading, it is possible to give an address list separated by commas or colons, for example : -a 32,33,34,36:40 read [32,33,34,36,37,38,39,40] -r # Start reference (1 is default) -c # Number of values to read (1-125, 1 is default) -u Read the description of the type, the current status, and other information specific to a remote device (RTU only) -t 0 Discrete output (coil) data type (binary 0 or 1) -t 1 Discrete input data type (binary 0 or 1) -t 3 16-bit input register data type -t 3:hex 16-bit input register data type with hex display -t 3:int 32-bit integer data type in input register table -t 3:float 32-bit float data type in input register table -t 4 16-bit output (holding) register data type (default) -t 4:hex 16-bit output (holding) register data type with hex display -t 4:int 32-bit integer data type in output (holding) register table -t 4:float 32-bit float data type in output (holding) register table -0 First reference is 0 (PDU addressing) instead 1 -B Big endian word order for 32-bit integer and float -1 Poll only once only, otherwise every poll rate interval -l # Poll rate in ms, ( > 100, 1000 is default) -o # Time-out in seconds (0.01 - 10.00, 1.00 s is default) Options for ModBus / TCP : -p # TCP port number (502 is default) Options for ModBus RTU : -b # Baudrate (1200-921600, 19200 is default) -d # Databits (7 or 8, 8 for RTU) -s # Stopbits (1 or 2, 1 is default) -P # Parity (none, even, odd, even is default) -4 RS-485 mode (/RTS on after sending) -5 RS-485 mode (/RTS on when sending) -h Print this help summary page -V Print version and exit -v Verbose mode. Causes mbpoll to print debugging messages about its progress. This is helpful in debugging connection... root@localhost:/home/zengjf/freemodbus/mbpoll/build# 三、Test Example: root@localhost:/home/zengjf/freemodbus/mbpoll/build# ./mbpoll -m TCP -t 3 -r 1000 -c 1 localhost mbpoll 0.1-23 - FieldTalk(tm) Modbus(R) Master Simulator Copyright (c) 2015 epsilonRT, All rights reserved. This software is governed by the CeCILL license <http://www.cecill.info> Protocol configuration: Modbus TCP Slave configuration...: address = [1] start reference = 1000, count = 1 Communication.........: localhost, port 502, t/o 1.00 s, poll rate 1000 ms Data type.............: 16-bit register, input register table -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 -- Polling slave 1... Ctrl-C to stop) [1000]: 0 ^C--- localhost poll statistics --- 12 frames transmitted, 12 received, 0 errors, 0.0% frame loss everything was closed. Have a nice day ! root@localhost:/home/zengjf/freemodbus/mbpoll/build#