Abstract: SMS short message service, as a basic service of GSM network, has been paid more and more attention by system operators and system developers. With GSM network as data wireless transmission network, various applications with extremely optimistic prospects can be developed. This paper gives the design of a small embedded SMS Chinese/English short message interface, and discusses in detail the short message format of PDU mode and the design of Chinese short message software decoding.
Keywords: GSM SMS UTF-8 GB-2312 wireless communication wireless data transmission remote control Chinese encoding
Introduction:
SMS (Short Message Service) is an application service provided by the GSM (Global System for Mobile Communication) system for sending and receiving text messages between GSM terminals (mobile phones) through a service center, where the service center completes the storage and forwarding functions of the information. As a basic service of the GSM network, the short message service has been paid more and more attention by system operators and system developers, and various applications based on this service have also flourished. Using the GSM network as a wireless data transmission network, a variety of applications with extremely optimistic prospects can be developed, such as two-way transmission of wireless data, wireless remote detection and control, etc. Typical applications include: wireless transmission and wireless automatic alarm of monitoring data of substations, electric meters, water towers, reservoirs or environmental monitoring points; remote wireless control of the start and stop of high-voltage line circuit breakers, heating systems, flood control systems or other electromechanical systems; fleet traffic management and control command system; control and monitoring of the operating status and inventory level of cigarette, food and beverage vending machines, etc.
Since the GSM network has achieved networking and roaming throughout the country and has the characteristics of strong network capabilities, users do not need to form a separate network, which greatly improves the network coverage and saves customers expensive network construction and maintenance costs. At the same time, it has no restrictions on the number of users, overcoming the defects of traditional private network communication systems with high investment costs, high maintenance costs, and limited coverage and number of users for network monitoring. Compared with traditional cluster systems, it has incomparable advantages in wireless network coverage, and the data transmission function of GSM SMS itself has made these applications rapidly popularized. Wireless communication using the GSM short message system also has a two-way data transmission function and stable performance, providing a powerful support platform for remote data transmission and communication of monitoring equipment.
1 SMS short message wireless data transmission monitoring and control
Normally, the wireless data transmission monitoring and control system based on SMS short message service is a point-to-multipoint remote wireless two-way data communication and control system, as shown in Figure 1. The center of the system is the data or monitoring command center, which is composed of a computer network, database, electronic map and GSM communication interface. The monitoring center is mainly responsible for receiving and sending and sorting various information and data: on the one hand, it receives the information and data uploaded by each monitoring point, puts them into the corresponding database and distributes them to the corresponding monitoring computer to realize the monitoring and management of each monitoring point; on the other hand, the monitoring center responds to the control information of each monitoring point sent by the monitoring computer, and sends this information to the corresponding monitoring point, so as to achieve the purpose of controlling the monitoring point equipment.
2 GSM wireless communication control terminal
The GSM wireless communication control terminal is an embedded system of MC3, which has the measurement, data acquisition, control and wireless communication functions as shown in Figure 2. The GSM wireless communication control terminal performs two-way information transmission with the monitoring center through the GSM network. It transmits the measured and collected data information to the monitoring center, and receives the control data from the monitoring center to implement corresponding control on the equipment.
3 Hardware Implementation of GSM Communication Interface
The communication interface of the GSM wireless communication control terminal generally adopts the standard GSM modules currently available on the market for secondary development, such as TC35, FALCOM, WISMO3, etc. These communication modules have all the functions of GSM wireless communication, and provide standard UART serial interface, supporting the instructions of the AT command set defined by GSM 07.05. Therefore, the MCU can be very conveniently connected to the GSM module through the UART interface, and the sending, receiving, searching and management of short messages can be conveniently and concisely realized by directly using AT commands.
In the GSM wireless communication control terminal we designed, the GSM communication module used is FALCOM A2D, and the MCU uses Atmel's high-speed 8-bit microcontroller ATmega128. The ATmega128 chip has 64 pins and integrates 4K bytes of RAM, 4K bytes of EEPROM, 128K bytes of Flash, and 2 UART serial interfaces. Due to the use of a high-performance MCU, a large number of peripheral devices, such as external RAM and ROM memory, are saved, which greatly simplifies the hardware structure and improves the reliability of the system. The hardware block diagram of the system is shown in Figure 3.
4 Software interface based on GSM communication
The standard GSM modules available for secondary development on the market all support the AT command set defined by GSM 07.05. Therefore, the MCU can send AT commands directly to the GSM module through the UART serial interface, so as to conveniently and concisely implement the sending, receiving, searching and management of short messages based on GSM. Table 1 lists some commonly used GSM AT commands related to SMS.
According to the definition of GSM 07.05, there are three modes for sending and receiving SMS short messages: Block mode, Text mode based on AT commands and PDU mode based on AT commands. The use of Block mode requires the module manufacturer to provide driver support; the PDU mode should be used for sending Chinese and English short messages, and the Text mode should be used for sending only English short messages (plain text format). The sending and receiving of short messages in Text mode is relatively simple. If Chinese does not need to be sent, it is best to use Text mode to send and receive short messages.
The steps of sending and receiving text messages in Text mode are divided into initializing the GSM module, sending text messages and receiving text messages. The following simple program gives an example:
PRINT "AT+CSCA=";Chr(34);"+8613800210500";Chr(34) 'Set the short message service center numberPRINT
"AT+CMGF=1" 'Set to Text modePRINT
"AT+CMGS=";Chr(34);"13500123456";Chr(34) 'Send a short messagePRINT
"This is a test SMS";Chr(26)
PRINT "AT+CMGR=1" 'Read the received
short message
.
.
.
This program is written in BASCOM-AVR high-level language. The function of the PRINT statement is to send a string through the URAT serial port. Chr(34) represents ASCII characters (") and Chr(26) represents ASCII characters (Ctrl-Z). The first line of statements sets the short message service center number, that is, sends the following string through the serial port: AT+CSCA= "+8613800210500", where +8613800210500 is the short message service center number of China Mobile in Shanghai. The second line of statements sets the short message sending format: AT+CMGF=1, where 1 indicates the use of Text mode. The third and fourth lines of statements send a short message to the control center. The format of the AT command is: AT+CMGS= "13500123456"<cr>This is a test SMS<^Z>, where the control center number is 13500123456, the short message content is "This is a test SMS", and the confirmation control character is Ctrl-Z. The fifth line of statements is to read the content of a received short message: AT+CMGR=1, where 1 indicates the first short message received (in the SIM card). After this command is issued, if there is a received short message in the GSM module, the following information will be returned from the UART interface (please refer to GSM 07.05 for the specific AT command format):
+CMGR: "REC UNREAD","+8613508485560",,"02/10/16,15:37:28+32"
xxxxxxxxxxxxxxxxxx (text content of received short message)
5 Chinese SMS Reception Based on PDU Mode
The PDU mode must be used to send and receive short messages in Chinese or mixed Chinese/English. According to the definition of GSM 07.05, as long as the controller sends AT commands to the GSM module through the UART interface, the received short messages in PDU mode can be directly read:
PRINT "AT+CMGF=0" 'Set to PDU mode
PRINT "AT+CMGR=1" 'Read a received short message
'
In PDU mode, the answer format of the GSM communication module is:
+CMGR: <stat>,,<length><CR><LF><pdu>
Among them: stat indicates the status of the short message, usually 0 (unread, new short message) or 1 (read);
Length is a decimal number, indicating the pdu data length minus the short message service center address length, in bytes;
<CR><LF> is a carriage return and line feed;
pdu is a data packet in PDU mode.
Then, the PDU data packet needs to be analyzed and the received Chinese information and other related information need to be parsed out according to the PDU data format.
5.1 PDU Data Packet Format
The PDU data packet consists of two parts, the short message service center address (SMSC address) and the TPDU. Table 2 shows the format of the PDU data packet, where a data unit is one byte.
The short message service center number, source number, date and time are represented by compressed BCD code, with the low bit first and the high bit last. A short message in PDU mode read from the GSM module by the AT command (AT+CMGR=1) for reading short messages is given, and common values and explanations are given.
5.2 Decoding of English short messages in PDU mode
English short messages in PDU mode are encoded using the 7-bit encoding of the GSM character set. At this time, the value of TP-DCS is 00. For example, if the content of the short message is the English character "Hi", first convert each character into a 7-bit standard binary ASCII code, and then adjust the low bits of the following characters to the front bit by bit to make up the difference. For example, the binary ASCII code of "H" is 1001000, and the binary ASCII code of "i" is 1101001; obviously, the binary code of H is less than eight bits, so the last bit of the following character i is added to the front of H, which becomes 11001000 (C8), and i has six bits left, 110100, and two 0s are added to the front, which becomes 00110100 (34), so "Hi" becomes two octal numbers C834. Since the English short messages in PDU mode are encoded in 7 bits, decoding is inconvenient. Therefore, for applications that only need to send and receive English characters and numeric symbols, it is best to use Text mode to send and receive short messages.
5.3 Decoding of Chinese short messages
Since Chinese characters are encoded using 2 bytes, sending and receiving short messages in Chinese or mixed Chinese and English can only use PDU mode. However, in the GSM standard, Chinese characters are encoded using UTF-8, not GB-2312, which is currently commonly used in China. Therefore, Chinese encoding conversion is required to match the GB-2313 Chinese character library to display Chinese characters.
Since there is no one-to-one linear relationship between UTF-8 and GB-2312 encoding, conversion can only be performed using a table lookup method. The following is an algorithm for converting from UTF-8 to GB-2312 encoding.
① Establish two Chinese encoding tables, UTF-8 and GB-2312. The data items in the tables are hexadecimal numbers with a length of 2 bytes, representing a Chinese code. The length of the two tables is 14,890 bytes (2×7445) respectively. The UTF-8 encoding table is sorted from small to large by data item value, while in the GB-2312 encoding table, the GB-2312 codewords of the same Chinese characters are at the same position as UTF-8, as shown in Figure 4.
② The software decoding process is: take two bytes from the TP-UD of the PDU mode SMS data packet each time, use the binary fast data search algorithm to find its location in the UTF-8 encoding table, and then read the corresponding GB-2312 code at the same position in the GB-2312 table. The decoding process is shown in Figure 5.
③ The worst search times of binary search is lg2(n+1), the length of UTF-8 table n is 7445, so the worst search times is 13 times, which means that the conversion of a Chinese code from UTF-8 to GB-2312 can be completed after 13 comparison searches at most. This is much faster than using a simple sequential table lookup, greatly reducing the time spent on table lookup, and is very suitable for use in general embedded systems.
④ Although the two Chinese character encoding tables take up nearly 30 KB (if GB-2312 to UTF-8 conversion is required, two more tables will be added, totaling 60 KB), the ATmega128 microcontroller is used, which has 128KB of program memory on the chip, so the program and the table can be placed together. After removing the 60 KB table, there is still nearly 70 KB of capacity for system programs, so there is no need for external expansion memory (except for the Chinese character library), which greatly simplifies the hardware structure and design.