Remote Monitoring and Management System of CNG Flow based on Modbus RTU Protocol

—A remote measurement, control and management system of CNG flow is presented based on a Modbus RTU communication protocol and non-contact IC cards. The system realizes parameter settings, CNG cumulative flow collection, history record inquiry, remote data transmission, real-time display, and sound and light alarm with the core chip STC89C516. Both the Coriolis mass flow-meter and the upper computer serially communicate with the microcontroller through the RS485 interface based on Modbus RTU protocol. The non-contact IC card and a password entered on the k eyboard are used to modify the system parameters. A strict password protection system is designed to ensure data security in the IC card. The test results show that the system can smoothly run and safely, can accurately and flexibly acquire data, and can remotely transmit data and display. The system has the advantage of simple operation, high precision, strong anti-interference ability and low circuit cost, which have broad application prospects.


INTRODUCTION
CNG (compressed natural gas) has the advantage of low cost, high efficiency, complete combustion, less pollution, and safe and convenient use, which have strong development potential and have been widely used in many fields of production and life, such as gas power generation, gas appliances, and gas cars. With the increases in oil prices, the price advantage of CNG quickly makes it the ideal alternative energy for vehicles [1][2].
The remote monitoring and management system of CNG flow proposed in this paper collects the output data of the mass flow-meter based on a Modbus RTU (remote terminal unit) protocol, processes data, stores data and remotely transmits data by the RS485 bus to the upper computer. The non-contact IC (integrated circuit) cards are used to modify the parameters. The data acquisition, control, settlement and management are integrated into the system.

II. SYSTEM FUNCTIONS AND FEATURES
Microcontroller STC89C516 is the core of the system with its peripheral circuit of mass flow-meter, barometer, read/write circuit of non-contact IC cards, time chip, analog to digital converter, RS485 transceiver, EEPROM, power module, keyboard, LCD and buzzer; the total structure is shown in Fig. 1.
The system realizes real-time flow data collection, data processing, display, storage, error data alarm, history  The block diagram of the proposed system record query and data uploading by the RS485 bus. The accuracy of the system is decided by the mass flow measurement, so the selection of the mass flow-meter is critical. The dedicated CNG Coriolis mass flow-meter ,from the E+H Company, was chosen because it can measure the medium temperature, density, volume flow and mass flow. The maximum measured temperature is up to 125 �, the maximum measured pressure is up to 350 bars, the maximum measured mass flow rate is 150 kg/min. The maximum mass flow error is ± 0.5%, the maximum measured error caused by the medium temperature is ±0.0003% / �, and the influence of medium pressure on the measured accuracy is negligible. The mass flow-meter supports the Modbus RTU and ASCII transfer modes, and the response time is 5 ms. The flow-meter has the advantage of excellent vibration resistance and electromagnetic compatibility. The measuring range of the barometer is from 0 to 40 Mpa, which is converted into a 1~5V analog voltage signal via the signal conditioning circuit. Then it is transformed into a digital signal by an analog to digital converter and sent to the microcontroller for processing. The non-contact IC cards are utilized to set the system parameters, which are divided into manager cards, employee cards and customer cards. All the IC cards are encrypted to ensure the safety of the system and customer funds. The illegal cards are considered invalid. The revisable parameters include measured pressure range, the upper and lower limits, time, unit price, etc., which make the system more adaptable. The EEPROM chip is used to store the system parameters. When the system works properly, time, gas flow, total price, employee number, custom card number are stored in the EEPROM at the end of unloading gas. If the power is suddenly shut off during the unloading, the power protection circuit plays a role, and the information is stored in the EEPROM. When the power is recovered, the program automatically reads the parameters stored in the EEPROM, and the system enters REGULAR PAPER REMOTE MONITORING AND MANAGEMENT SYSTEM OF CNG FLOW BASED ON MODBUS RTU PROTOCOL its normal working state. The adaptive capacity of the system is improved. The LCD is the human-machine interface, which displays unit price, density, total flow, total price, current time, pressure value, employee number and so on. The system has the following characteristics: (1) Power protection function. When the system suddenly loses power during a work period, a spare battery makes the circuit work continuously, closes the valve and stores the information, such as current total flow and total price, in the EEPROM. The amount is subtracted from the customer card wallet. The process is completed automatically without any manual intervention treatment.
(2) Self-test function. The system monitors the parameter changes during operation. Upon finding abnormal parameters, the system alarms automatically and closes the valves.
(3)The system security is ensured by the use of logic encryption IC cards and passwords entered on the keyboard. The various types of IC cards are set: the manager card, employee card and customer card. Each type of IC card adopts a different password protection system, and an illegal card is considered invalid to ensure the security. An employee cannot read the balance in the customer's card to ensure the fund's safety.

A. Mass Flow-meter
The dedicated CNG Coriolis mass flow-meter from the E+H Company was selected to achieve the multiparameter measurements of mass flow, volume flow, density and temperature. Under certain conditions, the temperature, pressure and density of the gas have no effect on the measurement. The flow-meter has the characteristics of high accuracy, simple operation, easy maintenance, anti-wear, anti-corrosion, anti-pollution, and explosion-proof. Two kinds of output interfaces can be chosen: pulse/frequency output and Modbus interface. Data collection based on the Modbus RTU protocol was adopted for this paper.
The Modbus communication protocol was developed by Modicon for open field bus communication protocol in an electronic controller. It has advantages of simplicity, flexible application, heavy data transmission, good realtime performance, powerful detectability, high performance-price ratio and strong compatibility. The Modbus protocol has earned wide support from many of the world's industrial control equipment and smart meter manufacturers and has become one of the main communication protocols in the industry. Through the Modbus protocol, the smart meters and control equipment from the different manufacturers can be connected into an industrial monitoring network to realize centralized control. The master/slave mode is adopted in Modbus protocol. There can be a master and up to 247 slaves in the same network. Each slave is assigned a unique slave address from 1 to 247. The master sends the data request command first. Then all slaves in the same network will receive the command. If the address of the slave is the same as the address receiving the command, the slave will respond and send the corresponding data to the master. The master can send the message to modify the data in the slave directly and can implement the bi-directional read/write function. The master can also communicate with all the slaves through the broadcast, and the address is 0. The slave cannot actively send data and communicate with other slaves.
The Modbus protocol has two kinds of message frame formats: RTU mode and ASCII mode (American standard code for information interchange). The ASCII mode sends a byte by two ASCII characters, while the RTU mode transmits a byte every time. Compared with the ASCII mode, the RTU mode has greater data traffic and more communication efficiency in the same baud rate. Most industrial smart instrumentations utilize RTU mode to transmit data. The master and slaves in the same network must use the same communication mode and transmission rate [3][4][5][6][7]. The microcontroller STC89C516 reads/writes data from/to the flow-meter based on the Modbus RTU communication protocol by the RS-485 interface.
In the RTU mode, the message frame begins to transmit after at least 3.5 character time. The time interval between two bytes must be less than 1.5 character time. After the last character is sent, a pause of at least 3.5 character time indicates the end of a message. A typical message frame is composed of the slave address, function code, data and CRC code. The function code for reading data from the flow-meter is 03, and the command format sent by the microcontroller is:

B. Non-contact IC card and read/write circuit
The non-contact IC card is also known as a radio frequency card or a proximity card. It combines radio frequency identification technology with IC technology to solve the passive and non-contact problem of the IC card with the advantage of rapid and convenient use, high reliability and good security.
The Mifare MF1 S50 non-contact IC card was used in the paper, which meets the ISO/IEC 14443 Type A international standards. The chip in the card is composed of an RF (radio frequency) interface, a digital control unit (anti-collision, authentication, encryption and logic unit) and an EEPROM. A passive non-contact card is made by connecting the chip to the antenna coil and packing it into the plastic. A battery is not required. When the RF card is close to the antenna of the read/write circuit, the highspeed RF interface transfers data at the rate of 106 kB/s. The Mifare MF1 S50 has a 1024!8 bit EEPROM divided into 16 sectors. Each sector is subdivided into four blocks, and each block has 16 bytes. So each sector has 64 bytes, numbered from 0 to 63. The last block of each sector stores two separate keys and read/write control conditions. The remaining three blocks store data (sector 0 has only two data blocks).The data block can be used as an electronic wallet and completes operations such as read, write, increment, decrement, redeposit, restore. To ensure the safety of RF card, each card has a unique 32-bit serial REGULAR PAPER REMOTE MONITORING AND MANAGEMENT SYSTEM OF CNG FLOW BASED ON MODBUS RTU PROTOCOL number and adopts an ISO/IEC DIS9798-2 three pass authentication [8][9][10].
The read/write circuit of the RF card utilizes an MFRC522 chip to recognize the RF card and to read/write data. MFRC522, produced by Philips, is a highly integrated read/write chip for a non-contact IC card (communication frequency is 13.56 MHz) that supports the ISO14443A/MIFARE mode and communicates by an SPI (serial peripheral interface), a serial UART (universal asynchronous receiver/transmitter) and a I2C (inter integrated circuit) interface. This designed circuit works in an SPI mode in this paper, as shown in Fig. 2. MFRC522, as a slave, receives register settings and transmits the communication data by the RF interface. The microcontroller generates the SPI clock, sends data to MFRC522 via the MOSI line, and receives data sent by MFRC522 via the MISO line. The data in the MOSI and the MISO line are high bit first, keep invariant in the rising edge of the clock, and change at the falling edge of the clock.

A. Flow Data Collection
The STC89C516 microcontroller communicates with the mass flow-meter based on a Modbus RTU protocol, which sends commands to the meter and receives returned data by serial port interrupt. The information transmission is asynchronous. The band rate is 9600, and the transferred 10-bit data are composed of one start bit, 8 data bits and 1 stop bit. The microcontroller sends the read command to collect the cumulative flow data every 1s. After the command has been completely sent, the RS485 transceiver is switched to the receive mode for obtaining the data from the flowmeter, and the timer starts to calculate the time interval between the characters. The microcontroller generates a serial port interrupt upon receiving the data. If the time interval is greater than 3.5 character time, new data are received. If the received data are same as the slave address, the microcontroller continues to receive data. The time interval between the two continuous data is computed during the data receiving process. If the time is less than the 1.5 character interval, the microcontroller continues to receive data. If the time is greater than 1.5 but less than 3.5 character interval, the data are abnormal. The CRC code is calculated and compared with the received CRC code. If the two codes are uniform, the subsequent data processing is performed. The data collection flowchart is shown in Fig. 3.

B. Communication with the upper computer
Modbus RTU protocol is also used to communicate between the system and the upper computer. The data are exchanged by the question/answer mode. The upper computer acts as the master, while the system is a slave. Data communication with the upper computer and the flow-meter share a serial port because STC89C516 has only one serial port. When the system is idle, the microcontroller monitors the signal in the serial port by the serial interrupt mode. When receiving the data that agree with the local address, the microcontroller starts to receive the command sent by the upper computer. After Read/write circuit of RF card the CRC codes are validated, the data stored in the EEPROM will be uploaded to the upper computer. The flowchart of communication with the upper computer is shown in Fig. 4.

C. RF card
The RF cards in the system can be divided into three categories: manager cards, employee cards and customer cards. The manager and employee cards are used to modify the system parameters according to their privileges. The manager cards can modify the unit price with the help of the password entered on the keyboard. The employee cards can modify the current time, the upper and lower limits of the gas pressure of the system. The customer cards can complete expense payment combined with the user password entered by the keyboard. To ensure the safety of data stored in the cards, triple password protection was adopted. When the RF cards are near the read/write circuit, the microcontroller begins to request the card, anti-collide the information, select the card and authenticate it. The flowchart is shown in Fig. 5.

D. Data Management
Data management includes the parameter settings by the manager and employee card, fee payment by the customer card, data query and so on. When the user presses the "manager" key, the system contacts the manager card.
Then the system reads the card number, verifies the validity and asks for a password. If the password is correct, the manager can modify the unit price value. The modification time, the manager card number and the parameter value are stored into the EEPROM. When pressing the "employee" key, the user can modify the time, maximum and minimum gas pressure and so on after brushing the employee card and entering the password. The correlative information is stored into the EEPROM. When the user presses the "start" key, the system sends reminders to brush the custom card and enter the password. If the password is correct, the system reads the card number and balance and opens the valve to unload gas. After unloading is completed, the employee presses the "stop" key to close the valve, modify the balance in the custom card and store the data, such as current time, unloading gas flow, total price, customer card number, balance, employee number and so on into EEPROM. Once the balance is insufficient during the unloading process, the system immediately closes the valve and clears the customer card balance. When the user presses the "query" key, the system sends reminders to brush the card and enter the password to query the data stored in the EEPROM based on the user's permissions.

E. Main program process
The total flowchart of the software system is shown in Fig. 6. The initializations are first performed, such as serial port initialization, time chip initialization, reading the initial parameters, RF card initialization, LCD (Liquid Crystal Display) initialization and so on. The system starts to check when a key is pressed. The corresponding data processing is executed according to the key value. Then the system reads the gas pressure inside the pipe and alarms when the pressure overruns. When it is unloading, the system reads the flow value, and calculates the total price and time displayed by the LCD. The system monitors the command sent by the upper computer when it is idle. The data stored in the EEPROM are uploaded when the system receives the command.

V. EXPERIMENTS AND RESULTS
The PCB (Printed Circuit Board) is fabricated according to Fig. 1; the chips and devices are soldered. The flow meter, barometer, circuit board, LCD and keyboard matrix are assembled. The flow-meter is connected to the personal computer by the service interface FXA291. The communication parameters of the serial port are set by the fieldcare software, as shown in Fig. 7. By pressing the "start" key to open the valves, the system starts to read the flow value, calculate the total price and display. The flow value displayed by the LCD is compared with the value read by the fieldcare online software to verify the correctness of the system, as shown in Fig. 8. Pressing the "stop" key will turn off the valve. Pressing the "query" key will search the results. Pressing the "up" and "down" key will query the historical results.

VI. CONCLUSIONS
CNG flow collection, pressure monitoring, data management and record uploading based on Modbus RTU protocol and RS485 communication are proposed in this paper. The STC89C516 microcontroller is the core of the hardware systems. The soft programs are written using C in a keil uVision4 environment to realize the Modbus protocol. Reliable communication occurs through the flow-meter and upper computer is achieved through the RS485 bus. The system parameters are modified by the dual protection methods of non-contact IC cards andpasswords entered on the keyboard. The strict password protection mechanism is designed to ensure the data security of the IC card. The parameters are stored in the EEPROM to improve the adaptive capacity of the system. The test results show the system is easy to operate; is stable, safe, reliable, precise, and convenient to manage; and has comprehensive function and strong antiinterface capability. It possesses high practical value and broad application prospects.