# Application of Modbus Protocol Based on µC /TCPIP in Water Saving Irrigation in Facility Agricultural

Jin-Lei Li, Wen-Gang Zheng, Chang-Jun Shen, Ke-Wu Wang

► To cite this version:

Jin-Lei Li, Wen-Gang Zheng, Chang-Jun Shen, Ke-Wu Wang. Application of Modbus Protocol Based on µC /TCPIP in Water Saving Irrigation in Facility Agricultural. 7th International Conference on Computer and Computing Technologies in Agriculture (CCTA), Sep 2013, Beijing, China. pp.281-288, 10.1007/978-3-642-54344-9_34 . hal-01220929

HAL Id: hal-01220929

https://inria.hal.science/hal-01220929

Submitted on 27 Oct 2015

**HAL** is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>
# Application of Modbus Protocol based on µ C/TCPIP in
Water Saving Irrigation in Facility Agricultural

Jin-lei LI¹, Wen-gang ZHENG¹, Chang-jun SHEN¹, Ke-wu WANG¹

¹Beijing Research Center of Intelligent Equipment for Agriculture, Beijing 100097, China;

**Abstract.** As long as the deepen application of the Intelligent equipment technology in the Water Saving Irrigation, a hot topics in research is coming out, that is how to increase the proportion of the efficiency and output for the high tech facility in manufacture. The Modbus protocol based on u C/TCPIP will play an important role in the Water Saving Irrigation application system, comparing to the others, it has such advantages of the Reliability of communication technology and real-time performance. After analyzing components of Modbus/TCP protocol, we applied it on real-time u C/OS-II kernel and ARM7 software and hardware and hardware environment with Modbus frame embedded in TCP frame. Test results show that Modbus/TCP protocol can achieve industrial standards. The protocol realizes the interconnection of control network and information network and has a broad application prospect in the agricultural water saving irrigation.

**Key words:** Modbus/TCP; u C/OS-II; ARM7; real-time; water saving irrigation

## 1 Introduction

In recent years, the industrial communication technology has been widely used in the field of agricultural water-saving irrigation. Current direction of development of agric ultural water-saving irrigation is that transmitting irrigation data through the network, scientific and reasonable control of irrigation, and improve the comprehensive utilization of agricultural water resources. The specific application process has the following problems: The first one is that the ways of irrigation data transmittion mode is single, so standardized task is difficult to realize. The second problem is the
real-time and reliability performance of information transmission is not good,unnecessary water waste exist in irrigation areas due to the poor efficiency and low reliability of information transmission [1,2].

Communication technology plays an important role in agricultural irrigation. In recent years, a few methods of network communication are widely used in agricultural water-saving irrigation applications. For instance, set an external serial server irrigation controller, or simply via Ethernet TCP/IP protocol transmission, etc. Although these methods have lots of advantages, such as interoperability, anti-interference ability and good real-time, but the defects of these ways can not be ignored. For example, communicate through an external serial server not merely engenders large power consumption, poor stability, low reliability but also generates additional costs. However, transmission through TCP/IP protocol also leads to network insecurity occasionally [6,7].

Since 1970s, the Modbus protocol has been widely used in different areas, it's host/slave-machine communication way perfectly meet the requirement of deterministic. In our system, we combine the Modbus protocol with the TCP/IP protocol by inserting Modbus frame into the TCP frame. Since Modbus protocol has a large number of node installing, it's very suitable for irrigation controllers that require a lot of control valves. That is one of the reasons why Modbus possesses a high performance and low cost in the field of agricultural water-saving irrigation. Modbus is an truly open and ideal solution for agricultural water-saving irrigation. The effectiveness can be verified by experiment and its value can be proved in practical applications.

In this paper, aiming at the actual situation shortcomings of the current domestic agricultural water-saving irrigation, applied the industrial Ethernet technology to the agricultural water saving irrigation, and embedded the Modbus protocol into uC/TCPIP, and a communication which is not only low power consumption and low-cost but also high compatibility has been realized. The problems of multiplicity and incompatibly of Field-bus standard, less accuracy and limited real time have been solved.
## 2 System structure and function

According to specific application, the system is based on the embedded Modbus/TCPIP system structure. Essentially, the system structure is usually shown as that in Figure 1.

<figure><img src="image_2.png" /><figcaption>Fig. 1. The structure of MODBUS /TCP</figcaption></figure>

The typical ways of the system are as follows: Irrigation control station Based on the Modbus/TCP as a server, a computer or embedded devices with the Modbus/TCP stack as a client. The client and the server are connected through the gateway, network interconnection equipment. Users can use the PC Modbus/TCP software on the PC client to exchange data with irrigation control station.

## 3 Hardware design of Network Interface

The system structure is shown as that in Figure 2, the Ethernet Controller mainly composed of 5 parts. There are the microprocessor MCU, Ethernet physical layer
interface chip DP83848, the RJ45 module used for cable connecting, 25MHz clock source, and the status LED lights which indicates the status of the connection. In our system, we use LPC2387 as the microprocessor, whose RAM memorizer of 64K and nominal operating frequency of 72MHz can provide abundant resources for µC / TCPIP transplantation [11]. In addition, the LPC2387 is capable of providing 10M/100Mbps Ethernet access in both half-duplex and full-duplex mode, with the built-in Ethernet MAC controller. Which can support Reduced Media Independent Interface, RMII and Buffered DMA Interface, BDI. We chose DP83848C which is made by National Semiconductor as the Ethernet physical layer interface chip. It can easily connect with the MCU(LPC2387) with three kinds of interface mode(MII/RMII/SNI),and we chose RMII.

<figure><img src="image_3.png" /><figcaption>Fig. 2. Overall block diagram of system.</figcaption></figure>

Ethernet controller control the network DP83848C by a simplified and independent Media Interface, RMII interface. MAC chip outputs differential signal to the RJ45 port. When the cable access network mouth, the green LED light indicates connection, and the yellow LED light indicates data interchange. The circuit structure of this module is shown as that in Figure 3.

Channel 13: the channel consists RMII interface and MII interface. RMII Interface:CRS(P1.8),RX_ER(P1.14),RXD0(P1.9),RXD1(P1.10),TX_EN(P1.4),TXD0 (P1.0),TXD1(P1.1),REF_CLK(P1.15).MII Interface:MDC(P1.16),MDIO(P1.17).

Channel 14:The channel includes TX+,TX-,RX+,RX-,a total of two of the differential signal.
<figure><img src="image_4.png" /><figcaption>**Fig. 3.** U800 is the MAC module(DP83848). The microprocessor (LPC2387) connect with DP83848 with the RMII interface (P1.8,P1.14,P1.9,P1.10,P1.4,P1.0,P1.1); JP800 is the RJ45 module. The DP83848 connect with RJ45 with RX+,RX-,TX+,TX-.</figcaption></figure>

# 4 The Software Design

## 4.1 The transplant system of µC / OS-II

The transplant system of µC / OS-II operating system use µC / OS-II as the ROTS. So transplant µC / OS-II to hardware platform is the first thing to go before using µC / TCPIP protocol stack, and generally speaking, it is related to four documents. There are OS_CPU_A.ASM,OS_CPU.H,OS_CPU_C.C which associated with the processor, and application-related OS_CFG.H. In our system, we use V2.84, the latest version of µC / OS-II.

## 4.2 The µC / TCPIP transplantation

The uC/TCPIP is to achieve the most important and centralized protocols of TCP/IP. Including IP, ARP, ICMP, UDP, TCP and BSD Socket invocation interface. uC/TCPIP include the following protocol module,NIC, ARP, IP, ICMP, UDP and TCP. At present, uC/TCPIP can only support Interface Controller in ethernet, referred
to NIC(Network Interface Controllers). And in our system it is DP83848.uC/TCPIP operate as two tasks of uC/OS-II, Rx Task and Tmr Task. Rxhere in charge of process the data packet received by NIC, if the data packet is needed by the present application layer, Rxhere will put the data packet from network layer to application layer through TCP-IP protocol; and Tmrhere is to manage all of the time delays related to TCP-IP protocol. During the uC/OS-II transplantation process, the user needs to realize the following functions:

(1) interrupt service function NetNIC ISR Handler(), initialization and increase the interrupt nesting in IRQ.S

(2) interrupt handler NetNIC ISR Handler(), remove the CPU interrupt flag, send a semaphore and call the related function

(3) operating system function: It's achieved in net_os.c, which involves a large number of the µC / OS-II-related operations function. This document provides a interface from TCP-IP protocol to the uC/OS-II operating system. If you want to migrate to a different operating system, the user can change net_os.h / c file. In RTOS Layer, tasks should be completed in three aspects: the establishment of Rxhere and Tmhere, establish time control service functions, the establishment of 10 semaphores, as shown in Table 1.

<table><caption>**Table 1** The semaphores created to transplant uC/TCPIP</caption><thead><tr><td>serial number</td><td>semaphore</td></tr></thead><tbody><tr><td>1</td><td>NetOS-InitSignalPtr</td></tr><tr><td>2</td><td>NetOS-LockPtr</td></tr><tr><td>3</td><td>NetOS-NIC-TxRdySignalPtr</td></tr><tr><td>4</td><td>NetOS-IF-RxQ-SignalPtr</td></tr><tr><td>5</td><td>NetOS-Sock-ConnReqSignalPtr</td></tr><tr><td>6</td><td>NetOS-Sock-ConnAcceptQ_Signal</td></tr><tr><td>7</td><td>NetOS-Sock-ConnCloseSignalPtr</td></tr><tr><td>8</td><td>NetOS-Sock-RxQ-SignalPtr</td></tr><tr><td>9</td><td>NetOS-IP-RxQ-SignalPtr</td></tr><tr><td>10</td><td>NetOS-TCP-RxQ-SignalPtr</td></tr></tbody></table>
4.3 Driver programming in NIC

(1) Initialization. The whole intialization is consist of processor-related MCU initialization and PHY initialization. And the process includes:

static void EMAC_Init (NET_ERR *perr)
{
    NetBSP_Phy_HW_Init();
    /*hardware reset initialization, configure IO, we use RMII.*/
    MAC1 = MAC1_RESET_TX|
    MAC1_RESET_MCS_TX|
    MAC1_RESET_RX|
    MAC1_RESET_MCS_RX|
    MAC1_RESET Sim |
    MAC1_RESET_SOFT;
    /*confugure MCU ethernet register */
    NetNIC_PhyInit(perr);
    if (*perr==NET PHY_ERR_RESET_TIMEOUT)
    {
        return ;
    }

/*initialize PHY (DP83848) ,First, reset PHY, and then read the ID of PHY to
make sure that it has been reset successfully, and finally to the PHY control register
assignment, set the data transfer rate, the default adaptive mode */
MAXF = 0x600;

/*configure register of TCP frame length,reset value is 0x600,the maximum
length is 1536 bits */
for (i = 0; i < 7; i++)
{
    if ((clk_freq / MII_Dividers[i][0]) <= 25)
    {
        MCFG = MII_Dividers[i][1];
        break;
    }
    SA0 = (NetIF MAC_Addr[5] << 8) |
        (NetIF MAC_Addr[4]);
SA1 = (NetIF MAC Addr[3] << 8) | (NetIF MAC Addr[2]);
SA2 = (NetIF MAC Addr[1] << 8) | (NetIF MAC Addr[0]);
EMAC TxEn();
EMAC RxEn();
}

## 4.4 Modbus/TCP protocol implementation

Modbus protocol defines a simple Protocol Data Unit (PDU), which has nothing to do with the basic communication. It consists a one byte function code and a variable length date. Unlike the standard Modbus frame, the Modbus/TCP frame embeds Modbus ADU into TCP for further transmission. Addressing and verification by the TCP/IP protocol, there is no necessity to recheck. What we need is to add a MBAP composed by seven bytes in order to identify the Modbus ADU. The MBAP can be divided into four parts, the detailed description as shown in table 2.

<table><caption>**Table 2.** Message header description of MBAP</caption><thead><tr><td>region</td><td>Length</td><td>Description</td></tr></thead><tbody><tr><td>Business unit ID</td><td>2bits</td><td>Modbus protocol request/response the ID of transaction process</td></tr><tr><td>Protocol ID</td><td>2bits</td><td>0=Modbus</td></tr><tr><td>Length</td><td>2bits</td><td>Uint ID and data length</td></tr><tr><td>Uint ID</td><td>1bits</td><td>The identification number of serial link or other bus to connect from the station</td></tr></tbody></table>

Function code that must carry on the operation, which is divided into a 8-bit word operation, such as 03 represent for maintaining a register operation, and 16 means multiple registers has to be written. The maximum data in Data domain is 248 bytes, and the specific format associated with functional code. In practice, the client sends the request data which shows the starting address of the register and the register number, meanwhile, the server respond with the number of operating registers and the status value of the register in a certain number.
The overall process in our system is as follows: The irrigation controller as a server waiting for the PC client to connect. The user can manage the irrigation controller with the software on computer when connected to them with reticle.

## 4.5 Modbus/TCP protocol communication test and results analysis

Test results show that the first frame delay basically maintain within 1.3 ms, the last frame delay basically keep within 1.2 ms. The Result Indicates that 300 frames/second is normal processing speed of Modbus/TCP.

<table><caption>**Table 3.** The test results</caption><thead><tr><th>sending rate<br>(frame/second)</th><th>PC to irrigation controller<br>The first frame delay(ms)</th><th>The last frame delay(ms)</th></tr></thead><tbody><tr><td>10</td><td>1.205</td><td>1.084</td></tr><tr><td>50</td><td>1.253</td><td>1.091</td></tr><tr><td>100</td><td>1.204</td><td>1.196</td></tr><tr><td>150</td><td>1.194</td><td>1.200</td></tr><tr><td>200</td><td>1.202</td><td>1.033</td></tr><tr><td>300</td><td>Frame lost</td><td>Frame lost</td></tr></tbody></table>

# 5 Summarization

Through a wholly analysis to the actual demand of current water-saving irrigation agriculture, then get a research of the Modbus/TCP protocol in facilities construction and Embedded System. The design of NIC and Modbus/TCP here have three advantages as follows: 1. Combining the Modbus/TCP technology and embedded system makes the Ethernet controller realize the real-time efficiency 2. The Modbus/TCP protocol here solve the multiplicity and incompatibility of field-bus standard 3. Successfully remove the uC/OS-II format software, making the system high throughput data transferring, also solving the problems during operations to the complex control system, then low down the cost, more practically.
References

1. Proakis J G, Salehi M. Communication systems engineering[M]. UpperSaddle River: PrenticeHal, 2001

2. Diane J Cook.Sajal.K Ds.Smart environments: technologies ,protocols, and applications[J].Hoboken:John Wileyand Sons ,2005,11(5): 13-46

3. Jia Xiao, Wang Cheng, Qiao Xiaojun. Drive of wireless smart terminal and touch-screen human-machine interface for field irrigation[J] 2008(9):8-12

4. Can Ayday, Saban Safak: Application of Wireless Sensor Networks with GIS on the Soil Moisture Distribution Mapping[M] 25. - 28.1.2009, Ostrava[8] PU ZH B. Photo electricity testing technology [M]. Beijing: Machinery Industry Press, 2005.

5. Sun Zhongfu, Zhao Wei, Liang Junbao, Du Keming, Zhang Yuliang. Design and application of greenhouse remote automatic control system based on GPRS and WEB[J]. Control & Automation,2010.

6. Texas Modbus-IDA.Modbus Messaging on TCP/IP implamentation Guide V1.0b.
http://www.modbus.org.October 24,2006

7. W. Richard Stevens. TCP/IP Illustrated Chapter 1 : Protocol [M].

8. Tong Wei Ming, Xu Rong, Lin Jing Bo. Design of Ethernet Experimental System Based on LPC2478 [J]. Low Voltage Apparatus :2011 NO.22

9. Sun Jing. Implement of Network Interface Communication Apply µ C/TCP-IP Based on Ethernet Controller LAN91C111 [J]. 2010,12(6)

10. Yang Zhi Yi, Zhang Yong, Zhang Kai long, Sun Zhong Hao. Software Adaptive Fault-tolerance Method in Embedded Multiprocessor Environment [J]. Computer Measurement &Control: 2012,20(6)

11. Sun Yu He. Research on Embedded Protocol Modbus/TCPIP Conversion Module [J]. 2008,5(6)