Software design for sending and receiving short messages by using single chip microcomputer to control GSM module

Publisher:创意火花Latest update time:2011-12-29 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The GSM (Global System for Mobile communication) system is a relatively mature, complete and most widely used system in the current mobile communication system based on time division multiple access technology. The GSM digital cellular mobile communication network that has been built to cover the whole country is the main mode of public mobile communication network in my country. The SMS information service based on GSM is a wireless application for transmitting short messages on the mobile network, and is a process of storing and forwarding information on the mobile network. Since the public GSM network has achieved networking and roaming on a global scale, it is recommended that the above system does not need to establish a dedicated communication network, so the SMS application with real-time data transmission function will be rapidly popularized. The temperature data acquisition and wireless transmission system based on the GSM network developed and designed by the author uses this network platform to realize automatic two-way data transmission using short message services. The system model diagram is shown in Figure 1.

The system consists of three modules: data acquisition part, data receiving and sending part, and terminal processing part. The data acquisition module stores the collected temperature data in the memory. The data transceiver module adopts the method of dual single-chip microcomputers sharing E2RPOM. Single-chip microcomputer 2 controls the data to be transferred from the memory to E2PROM; single-chip microcomputer 1 is responsible for reading data from E2PROM and sending the data through GSM module 2 with the help of GSM network. Single-chip microcomputer 1 not only controls the sending of data, but also controls the receiving of data. Here, E2PROM is a transit station for temporary storage and uploading of temperature data. The terminal processing module is responsible for handing over the received data to the computer for processing, and storing the processed results in the database for query. When the terminal processing module needs to send a control command to GSM module 2, the receiving process of GSM module 2 is exactly the opposite of the above process, thereby realizing automatic two-way transmission of data. In the system, the three modules are independent of each other and interdependent on each other to complete the data transmission together. The data transceiver module plays a connecting role in the system and is the core module of the system. This module is based on dual single-chip microcomputers and uses RS232 communication interface to connect with GSM module at physical layer. Due to the limitation of space, this paper mainly introduces the software implementation process of single-chip microcomputer to control the work of this module, aiming to explore how to use single-chip microcomputer to control GSM module to send and receive short messages.

1 GSM module MZ28

MZ28 is a GSM wireless dual-band modem launched by ZTE, which mainly provides wireless interface for voice transmission, SMS sending and data services. MZ28 integrates complete RF circuit and GSM baseband processor, which is particularly suitable for rapid development of wireless application products based on GSM wireless network. The communication between the application product with man-machine interface (MMI) and MZ28 can be carried out through the standard serial interface (RS232). MZ28 uses a simple 20-PIN ZIP socket to connect to the user's own application system. This ZIP connection method provides interface signals such as data communication, audio and power required for development. MZ28 can be used as a wireless engine and embedded in the user's own products. The user can use the UART port of the microcontroller or other CPU and the corresponding AT commands to control the module, so that the product can easily enter the GSM network.

2 Working principle of serial port control SMS

The single-chip microcomputer and the GSM module generally use a serial asynchronous communication interface, and the communication speed can be set, usually 19200bps. When this RSM232 cable method is used for connection, the reliability of data transmission is better. The RS232 interface is connected to the GSM module through a serial interface integrated circuit and a level conversion circuit. The circuit is relatively simple. The chips involved include the single-chip microcomputer 89C52 and the level conversion chip MAX232. It is a very common interface circuit. It should be noted that the interface extends an E2PROM memory chip AT24C64 through the I2C bus. Its main function is to store data, and the information will not be lost when the power is off. These characteristics are necessary for storing data.

GSM's short message service SMS uses signaling channels for transmission, which is unique to the GSM communication network. It does not require dialing to establish a connection. The message to be sent plus the destination data is sent to the short message service center, which stores it before sending it to the final destination. Therefore, the message will not be lost when the destination GSM terminal is not turned on. The amount of information in each SMS is limited to 160 bytes.

Most mobile phones on the market now support the AT command set specified by GSM07.05. This command set was released by ETSI (European Telecommunications Technical Committee), which includes the control of SMS. By using the serial interface of the GSM mobile phone, the microcontroller sends and receives a series of AT commands to the mobile phone, the purpose of controlling the GSM module to send and receive SMS can be achieved. It must be noted that when using a single-chip microcomputer, the programming must pay attention to the fact that the instructions it sends and the responses it receives are all ASCII codes of characters. The AT commands involved in using a single-chip microcomputer to control the GSM module to send and receive short messages are listed in Table 1.

Table 1 AT commands

AT commands

Function Description

AT+OFF Shutdown and restart
AT+CSDH=0 In TEXT mode, detailed header information is not displayed in the return value.
ATE0 Turn off echo
AT+CMGF=1 Select SMS format as TEXT mode
AT+CMGS Send SMS
AT+CMGR Read SMS
AT+CMGD=0 Delete all short messages

3 Software Implementation

3.1 Implementation of half-duplex communication protocol between host and slave modules

3.1.1 Acknowledgement and retransmission

The communication between the upper computer module and the lower computer module is carried out in half-duplex communication mode, that is, data transmission is bidirectional. However, at any time, only one party can send data and the other party can receive data, because the reading and writing of E2PROM cannot be carried out at the same time. In order to avoid that when one party sends information frames (the information frames here refer to the data frames sent by the lower computer module and the command frames sent by the upper computer module, the same below), the other party will also send data, the channel must be changed to half-duplex mode. Although this may not be as efficient as full-duplex mode, the efficiency can be sacrificed in exchange for the stability of the module's working performance. The order adopted by both parties is: send → receive a response → send again.

According to the design idea of ​​the whole system, the frames sent by the upper computer module (i.e. GSM module 1 in Figure 1, the same below) include command frames, confirmation frames and non-confirmation frames; the frames sent by the lower computer module (i.e. GSM module 2 in Figure 1, the same below) include data frames, confirmation frames and non-confirmation frames. Confirmation frames and non-confirmation frames are response frames waiting for the other party to send after sending data, which are used as the basis for continuing to send the next frame and resending the previous frame. Command frames and data frames are information frames. When one party sends the information frame first, if the receiving party receives the other party's information frame and has no information frame to send, then the situation is relatively simple. The receiving party will decide whether to send a confirmation frame or a non-confirmation frame based on the correctness of the information frame, so that the other party can decide whether to continue sending or resend; if the receiving party also has an information frame to send at this moment, then the receiving party will not send a response frame immediately, but will immediately send its own information frame to the other party and wait for the other party's response frame to this frame. After receiving the other party's response frame, the receiving party will decide whether to continue sending the next information frame or resend the original information frame based on the content of the response frame (i.e., confirmation frame or non-confirmation frame, the same below). If the response frame is lost due to factors such as the unreliability of the link itself, the receiving party will delay resending the original information frame within a certain period of time because it has not received the response frame. After receiving the other party's response frame, the receiving party will continue to send the next information frame and wait for the other party's response frame, and repeat this process until the receiving party has sent all the information frames. After receiving the last response frame from the other party, it indicates that all the information frames of the receiving party have been sent. The receiving party will then send the response frame that the other party is still waiting for, notifying the other party whether the received information frame is correct or not.



3.1.2 Delayed retransmission

During the communication between the two parties, there are two times t1 and t2, which respectively represent the maximum delay of resending the information frame. t1 represents the time from when one party sends the information frame to when it receives the response frame from the other party. If the waiting time for the response frame exceeds t1, the sender will resend the original information frame; when the receiver receives the information frame sent by the other party, if the receiver has an information frame that needs to be sent at this time, the receiver will not send a response frame, but send the information frame to the other party. In other words, during the time t1 when the other party is waiting for the receiver's response frame, the receiver inserts the information frame sent in this article, and there is also a delay retransmission problem in the sending of this party. Within the specified time, if the other party's response frame is not received, the receiver also needs to resend the original information frame. This specified time is t2. Obviously, since the receiver uses the gap time to send its own information frame, t2

FIG2 takes the lower computer module sending the data frame first as an example to illustrate the specific implementation process of the communication between the two parties.

It should be noted that due to the limitation of the layout, the communication process shown in FIG2 does not involve the case of sending non-confirmation frames. If the receiving party sends a non-confirmation frame, the sending process of the sending party is the same as sending a data frame, except that in this case, the data frame with the same frame number needs to be resent. If the upper computer module sends a command frame first, the implementation process of the communication between the two parties is similar to FIG2, except that the data frame becomes a command frame at this time, and the command frame becomes a data frame. In terms of the delay time, whether the lower computer sends a data frame or the upper computer sends a command frame, the size of t2 should be the same. Both use the time interval t2 to send the receiving party's information frame, and the delay time is the same. However, for t1, the situation is different. Because when the lower computer module sends the data frame first, the command frame sent by the upper computer module using the interval time t1 is less reliable, so the t1 defined when the lower computer module sends the data frame first should be less than the t1 defined when the upper computer module sends the command frame first. This is because when the upper computer module sends the command frame first, the lower computer module may send more data frames using the interval time t1.

3.2 Frame Format

The GSM module controls SMS through an asynchronous communication interface. There are three access protocols: Block Mode; Text Mode based on AT commands; PDU Mode based on AT commands. The data sent and received by this system are all based on digital temperature data and command words. In order to ensure the applicability of the system, SMS transmission and reception adopts TEXT mode. TEXT mode is based on characters, more specifically, a structural mode based on ASCII code. In this mode, the information frame format sent and received by the module is as follows:

Frame Header Frame number data Checksum

Information frames include data frames and command frames.

The frame header indicates the mark of the data frame and is composed of fixed characters "WQ".

The frame number indicates the sequence number of the data frame, which consists of two bytes. The frame number indicates the incremental data frame number sent by the lower computer module and the command frame number sent by the upper computer module. In order to simplify the frame structure, the command frame number is unified as 00H.

The length of the data field is 154 bytes, and a maximum of 77 characters can be sent (in TEXT mode, Chinese characters cannot be sent).

The check subcode is the initial code of the cumulative sum of all bytes in the data field (the original code is inverted and 1 is added), consisting of one byte.

In addition to information frames, there are also response frames transmitted in both directions, which include confirmation frames and non-confirmation frames. Confirmation frames are response frames fed back to the sender by both parties, indicating that the receiver has correctly received the information frame sent by the sender. The confirmation frame format only includes two fields, and the contents of the two fields are fixed, namely the frame header "WQ" and the data field "ACK". The confirmation frame format is as follows.

WQ ACK

A non-confirmation frame is a response frame from the receiver to the sender, indicating that the receiver has received an invalid information frame. Its format is similar to the response frame format. The frame format is as follows.

WQ NACK

3.3 Allocation of E2PROM Space

An 8KB E2PROM is used, which is divided into blocks of 77 bytes each, for a total of 106 blocks, as shown in FIG3 .

Blocks 00 and 01 are reserved for system use, and blocks 02 to 105 are data blocks for storing data.

3.4 Handshake protocol between the transmitter and receiver and the collector

The transceiver and the acquisition end share a memory, that is, the two CPUs operate the same E2PROM. The implementation scheme is to connect one I/O pin of each microprocessor, and the two CPUs use query mode to query this I/O end. If the transceiver query finds that the local I/O end is high at a certain time, then the single-chip computer 1 has the right to operate this memory and can read and write to the E2PROM. If the acquisition end queries that the local I/O end is high, then the single-chip computer 2 has the right to operate this memory and can write to it. After one party completes the operation, it sets the I2C bus to a high level, indicating that the local end has released the I2C bus and the E2PROM is currently in an available state.

3.5 Program Design

3.5.1 Design ideas of main function

After powering on, the program runs in the main function, and the MCU and GSM module are initialized respectively. The MCU initialization includes setting the serial port working mode, baud rate, and initializing variable parameters and flags. The GSM module initialization includes restarting, turning off echo, setting the return value in TEXT mode not to display detailed header information, selecting the SMS format as TEXT mode, and developing the serial port interrupt to prepare for receiving data.

3.5.2 GSM return parameter processing - SHELL function

The SHELL function is called when entering the clock interrupt program. This function processes the parameters returned by the GSM module. According to the requirements of the system design, the following operations need to be performed on the GSM module: calling the other module number, sending data, reading text messages, and deleting text messages. Based on the above operation instructions, if the operation is successful, the GSM module will return different parameters: >, +CMGS, +CMGR, OK. According to the different parameters received, the lower computer module will turn to different operation steps to judge and change the value of the flag bit. For example, if > is received at a certain moment, it means that the call to the other module number is successful, and data needs to be sent next. At this time, the SHELL function will check the flag bits f_sending, f_ack, and f_nack represented by sending different data, so as to determine what type of data needs to be sent.

3.5.3 SMS data processing—ExecData function

When entering the clock interrupt to call the SHELL function, if the returned parameter +CMTI is received, it means that the upper computer module has sent SMS data to the lower computer module, which may be a command frame, a confirmation frame or a non-confirmation frame. In this case, the SHELL function needs to analyze the content of the SMS and perform different processing according to the content of the SMS. The ExecData function is responsible for completing the above functions. It is called by the SHELL function to analyze and process the SMS data.

Conclusion

Through the above analysis, it is not difficult to find that the whole program is complicated and the functions are intertwined. The flag bit plays a very important role in the implementation of the program. It is relying on these flag bits that the program can well implement the switching between various functions, and the value of the flag bit is modified by the parameters returned by the OSM module. Therefore, the implementation process of the program should be reading parameters → modifying flag bits → sending instructions.

The main function, clock interrupt and serial port interrupt program, SHELL function, and ExecData function are the main line and core part of the whole program. The analysis of them can help us understand the main idea of ​​the program, which is why the author focuses on introducing them. However, the implementation of these functions and interrupt programs also requires the cooperation of other functions, such as E2PROM operation functions based on I2C bus, string operation functions, and serial port sending functions, etc. Due to space limitations, they will not be introduced here. The GSM network itself is not completely reliable, and frame transmission errors and frame losses may occur. However, due to the existence of retransmission and delayed retransmission mechanisms, the program can avoid the occurrence of the above situations to the greatest extent. In the actual application process, the module runs normally, with stable performance and good real-time performance.

Reference address:Software design for sending and receiving short messages by using single chip microcomputer to control GSM module

Previous article:Common methods and countermeasures for MCU cracking
Next article:Developing MCU Programs without an Emulator

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号