Xunwei iTOP-development board-modbus porting and usage documentation

This document describes how the modbus library is ported to the ARM development board and how to use the test program.

Applicable to Xunwei iTOP-4412 development board丨4418 development board丨6818 development board丨IMX6 development board

1. What is modbus

There are many articles about modbus introduction on the Internet, and there are books dedicated to modbus. These materials can be used by everyone, but in this section, the author hopes to help everyone understand the nature of modbus and what modbus is used for. What problems are used to solve and the value of existence.

The first important thing to understand is that modbus is a "pure data protocol".

Speaking of the modbus protocol, it is often associated with RS232 and RS485 and Ethernet. So what is the relationship between modbus and RS485 protocol? modbus is a pure data protocol, and has nothing to do with protocols such as RS232, RS485 and network. If the modbus protocol is used on the serial port RS232, modbus only processes the data before and after the serial port is sent and received. The serial port only uses modbus before sending data. protocol to convert the data, after receiving the data, convert the data through the modbus protocol.

The second important thing to understand is: modbus is a "universal pure data protocol".

How to understand the general data protocol? When the author was studying, he just started learning about embedded, using 51 single-chip microcomputer. At that time, when debugging a program, he usually lights a light or pulls up an IO at any step of the execution. Later, he felt that the program could be debugged by "lighting on". It's too slow, so I defined the "serial port data protocol", for example: when a certain piece of code is executed, I will print 0x01, and if there is an error in this code, I will print 0x02... Now everyone has learned linux, and they all know linux control For programmers, the linux console is mainly used to debug programs. Adding the print function to the code can realize the console print string.

Then the "serial port data protocol" defined by the author back then is not a general protocol and can only be used by oneself, but the linux console, no matter which linux engineer, operates in a similar way. Then the linux console is common to all linux devices.

modbus can be compared to Mandarin, as follows:

Hokkien → Hokkien → Mandarin

Cantonese→Cantonese→Mandarin

Northeasterners→Northeastern Chinese→Mandarin

Sichuanese→Sichuan dialect→Mandarin

"Fujian" is compared to "RS232", "Cantonese" and "Northeastern" are compared to RS485, and "Sichuan" is compared to "Ethernet"... Then "Mandarin" is the modbus protocol. The modbus protocol is the same as "people". The hardware has nothing to do with the way "people" speak. Even as long as the robot's voice is "Mandarin", this Mandarin is also a modbus protocol.

There is a lot of information about modbus on the Internet. There are many materials that bind modbus and RS232. This is completely wrong. Please pay attention to that, modbus is just a "universal pure data protocol".

modbus is a bus protocol used in industrial fields. It can be used not only on PLC or single-chip microcomputer, but also on any electronic device that can run code, because modbus has no definition for hardware. Of course, the scope of application of modbus is mainly in industrial control. It would be terrifying if every industrial control device had a custom protocol. The modbus protocol can be used on all control devices, and all industrial control devices are compatible with the modbus protocol.

The essence of modbus is a general-purpose pure data protocol. modbus is a communication protocol used to standardize various control devices in the industrial field. Through the modbus protocol, the control device can communicate with other devices through the network (RS485 and Ethernet, etc.). The equipment produced by the manufacturer's equipment can be easily connected to an industrial network.

2.Modbus ported to linux

2.1 modbus source code download

You can download version 3.1.4 from the libmodbus official website http://libmodbus.org/download/, or you can use the downloaded source code provided by Xunwei.

The author downloaded "libmodbus-3.1.4.tar.gz" and packaged it in "topeet_modbus.tar.gz", which also included the transplanted files.

2.2 modbus porting and compiling on arm-linux

The compiler used for cross-compilation is "arm-2009q3", as shown in the following figure.

Copy the downloaded source code package "libmodbus-3.1.4.tar.gz" to Ubuntu through ssh, decompress it with the command "tar -vxf libmodbus-3.1.4.tar.gz", and generate the directory "libmodbus- 3.1.4”, as shown in the figure below.

Use the command "mkdir install" to create a new install path to install the generated files, as shown in the following figure.

Enter libmodbus-3.1.4 and execute the command ./configure ac_cv_func_malloc_0_nonnull=yes -- host=arm-none-linux-gnueabi --enable-static --prefix=/home/minilinux/install/ . Host= is followed by the compiler prefix, --prefix= followed by the installation directory, which can be determined according to your own environment, as shown in the following figure.

As shown in the figure below, the execution is completed.

Execute the compilation command "make", as shown in the following figure.

The compilation is complete, as shown in the following figure.

Execute the installation command "make install", then install to the directory specified by ./configure. The author here is the "/home/minilinux/install/" directory, as shown in the following figure.

The installation is complete, as shown in the figure below.

Check the installation directory install, and generate three directories in install: include, lib, share, as shown in the following figure.

As shown in the figure below, use the file command to view the library files in the lib directory, and you can find that the files are of the arm architecture.

Copy all the files in the compiled lib path to the "/lib" directory of the development board.

So far, the arm-linux library file of modbus has been compiled.

2.3 Test program for arm

Enter the directory /home/minilinux/libmodbus-3.1.4/tests/, check the test program random-test-server.c, as shown in the figure below, it has been compiled, but you still need to modify several parameters in the source code and re- compile.

Enter the command vim random-test-server.c to open the test program, in line 23 ctx =modbus_new_tcp("127.0.0.1", 1502); Change it to ctx = modbus_new_tcp(NULL, 1502);, that is, the server monitors all ip addresses, The port is 1502.

Then execute the command cross compile command arm-none-linux-gnueabi-gcc -o random-test-server random-test-server.c - L/home/minilinux/install/lib -lmodbus -I/home/minilinux/install/ include/modbus generates the executable random-test-server, as shown in the following figure.

As shown in the figure below, the executable file is of the arm architecture.

So far, the test routine "random-test-server" of the arm server has been compiled.

2.3 x86-linux modbus porting and compiling

Create another folder in the minilinux directory for the x86 files of modbus, as shown in the figure below, use the command "tar -vxf libmodbus-3.1.4.tar.gz -C x86-modbus/" to extract the source code to in this directory.

Enter the decompressed "/home/minilinux/x86-modbus/libmodbus-3.1.4" directory, as shown in the figure below, execute the command "./configure" without adding any parameters, the default is GCC-x86 configuration.

The configuration is complete, as shown in the following figure.

Execute the "make" compilation command, and then execute the installation command "make install", the installation is complete, as shown in the following figure.

Enter the test directory, the test program under arm we compiled earlier is server, then the client client should be running under Ubuntu, as shown in the following figure.

The client needs the IP address of the server. As shown in the figure below, the author's server (that is, the development board) IP is "192.168.2.20".

Next, modify the "random-test-client.c" file in the x86-modbus directory, use the vim command to open the random-test-client.c file, and set the

ctx = modbus_new_tcp("127.0.0.1", 1502);

change to

ctx = modbus_new_tcp(development board IP, 1502);

As shown below, then save and exit.

Execute the command: gcc -o random-test-client random-test-client.c `pkg-config --libs --cflags libmodbus`

Compile and generate random-test-client executable file, as shown in the figure below, the compiled executable file is x86.

At this point, the test program on x86 is compiled.

3. Test

First, make sure that the development board and the virtual machine Ubuntu can be pinged. As shown in the figure below, the IP of the author's virtual machine is "192.168.2.200", and Ubuntu can be pinged through the development board.

Secondly, pay attention to copy the arm library file compiled earlier to the development board, as shown in the figure below.

Run the server program on the development board, as shown in the following figure.

Run the client program "./random-test-client" on the virtual machine Ubuntu, as shown in the figure below

The server is running, as shown in the following figure.

So far, the porting and testing of modbus is completed.

Related Posts