Send Chinese text messages in gsm pdu Mode

Source: Internet
Author: User

There are also a lot of materials on the Internet for sending Chinese text messages in the gsm pdu mode. However, after so many days, I wrote my own experience as a record, it is also convenient for other later users to refer.

Many materials on the Internet do not talk about the development environment, so I still feel confused in some places.

My development environment:

Hardware: the GSM module uses the mc52i of Siemens, and the control chip is stm32. The stm32 and mc52i communicate directly through the serial port.

Development Environment: IAR arm version. I forgot the version number.

Development language: C language, no operating system.

Mobile phone card: the mobile phone card is used by my own mobile phone.

References:

At Chinese text message: http://bbs.chinaunix.net/thread-1945962-1-1.html

Send Chinese: http://www.pc6.com/infoview/Article_405.html with Unicode encoding in SMS

C language for String Conversion in UTF-8, Unicode, gb2312 format: http://www.cnitblog.com/wujian-IT/archive/2007/12/13/37671.html

Unicode encoding: http://www.willar.com/article/article_view.asp? Id = 474

The format of the Short Message PDU in the GSM wireless module is described as follows:
Http://www.cnblogs.com/AlphaDu/articles/1233121.html
Http://www.dreamfabric.com/sms/
Http://read.pudn.com/downloads30/doc/comm/94938/mc35i%E5%8F%8Atc35i%E6%8C%87%E4%BB%A4/qunfa2.pdf

References (with official technical manuals): http://download.csdn.net/detail/dlutxie/4488089

Since the mc55i and mc52i commands are basically the same, I am referring to the mc55i User Manual. The manual contains detailed setup flowchart, which of the following commands are mandatory and optional.

Follow the instructions in this manual to initialize the SMS sending module as follows:

At + CSCs = "GSM"; // set the character set of the terminal device. Set the character set of the GSM module. The default value is the GSM character set. When sending: sentcom ("at + CSCs = \" GSM \ "); pay attention to the reference here.

At + csca = + 8613800411500 // set the number of the information center. This number is definite for a mobile phone card and can be seen in the text message settings of the mobile phone. The setting of this command may also be at + csca = "+ 13800411500", with a quotation mark added, and a parameter can also be followed, at + csca = + 8613800411500,145; 145 indicates that the preceding number is an international number, starts with "+" and carries a national number; or At + csca = 13800411500,129; 129 indicates that you are not sure whether the preceding number is a country or international number. Do not start with "+. I did not perform the test, but I did not view the official documentation. If you want to send this command through the serial port, you can write it as: sentcom ("at + csca = \" + 13800411500 \ "). Note that this section contains a quotation mark for unreferencing.

At + CSMs = 1; // Set message service: GSM 07.05 phase2 +

At + cnmi =,; // sets the prompt mode when the message is received.

At ^ smgo = 1; // use URC to prompt whether the SMS storage space is full. If the value is 0, no prompt is displayed. If the value is 1, a prompt is displayed.

At + cmgf = 0; // set the sending information mode. 0 indicates the PDU mode and 1 indicates the text mode. The default value is generally 0, but the default value of my mobile phone card is 1.

At + SCMP = 17,167, 0, 0; // if it is sent in text format, you need to set this parameter command

At & W; // Save the preceding settings to the user configuration file, so that you do not need to perform these settings the next time you load the file. Is the user configuration file in mc52i or SIM card?

Well, the above is the process required by the official documentation. Let's take a look at my actual situation:

At + CSCs = "GSM"; for this command, I set the character set to ucs2, that is, at + CSCs = "USC2 "; whether it is text mode or pdu mode to send Chinese or English text messages, the message sending fails. Therefore, the default GSM character set is selected.

At + csca = + 13800411500, forget whether it is related to the character set selected earlier. I use at + csca =? For testing, this command returns OK, but executes the READ command: At + csca? But there is always an error. Two returned numbers are a long unreadable string: + csca: "002b0038003600310033003800300030003400310031003500300030, 145". The following 145 indicates that the previous number is an international number, maybe the information center number is encrypted? We can see that the information center number exists in the SIM card instead of in the GSM module. When sending a text message, the GSM module will read the number from the SIM card (Ps, I did not explicitly see it in the official document. Of course, I have not read all of the information ). So when I sent a text message in text mode, I didn't set the information center number. Instead, the text message was sent !!!

In addition to at + cmgf = 0;, you can use the default values for the next six commands! Of course, the default value can also be used for this command. It depends on the form in which you send text messages.

To sum up, if you send text messages in text mode, you only need to execute the following command:

Sentcom ("at + cmgf = 1"); // select the text mode

Delay (100); // The latency is 100 ms. In the official mc52i documents, it is pointed out that it usually takes about ms to execute the next command after each command is executed, some commands require longer latency, such as opening a network connection command.

Sentcom ("at + cmgs = 18608683863"); // set the target mobile phone number

Delay (100); // The latency is 100 ms. If you can send a text message, a ">" prompt is returned, waiting for the text message to be entered.

Print ("please sent me a message back by QQ, THX"); // send text messages. Note that text messages cannot be sent in Chinese, if Chinese or Chinese characters are entered here, an error occurs.

Print ("\ x1a"); // enter Ctrl + Z in hexadecimal format to end the text message

Delay (5000); // The latency is 5 S. Note that the latency here is very important. At that time, I delayed the delay of 2 S and did not receive a reply from the mc52i module, so I don't know whether the command is successfully executed, and I finally find that the delay is not enough. This delay may take me some time and I don't know what the problem is !! The command delay in mc52i is very important, but it is rarely emphasized in some documents on the Internet.

To send text messages in PDU mode, run the following command:

Sentcom ("at + cmgf = 0"); // select the PDU Mode

Delay (100); // latency: 100 ms

Sentcom ("at + cmgs = 25"); // if you can send a text message, that is, if the command is successfully executed, a ">" prompt is returned, waiting for the text message to be entered, when writing a program, you can use an if statement.

Print ("0011000d91685178722902f00008000a5de54f5c61095febff01"); // The text message content is "have a good time !" Add an exclamation point to the four words,

Print ("\ x1a"); // enter Ctrl + Z in hexadecimal format to end the text message

Delay (5000); // The latency is 5 S. Note that the latency here is very important. At that time, I delayed the delay of 2 S and did not receive a reply from the mc52i module, so I don't know whether the command is successfully executed, and I finally find that the delay is not enough. This delay may take me some time and I don't know what the problem is !! The command delay in mc52i is very important, but it is rarely emphasized in some documents on the Internet.

PS: sentcom ("at + cmgs = 25"); 25 in this command starts with pdutype (that is, starting with the next byte of csca) the number of bytes until the maximum number of all characters, that is, the number of characters/2. The above information refers to the number of bytes occupied by 11000d91685178722902f00008000a5de54f5c61095febff01. Note that the number of bytes is not the number of characters !! If you want to be lazy, you can repeat the string to word07 or 10, select it, click the number of characters in the status bar to view the number of characters, and divide it by 2, that is, the number of bytes. Some documents say that the length here refers to the maximum number of bytes of data to be sent. For example, here 25 refers to the maximum number of 25 bytes to be sent (excluding csca ), the actual number of bytes sent can be less than this value. However, in my test, this value can only be the number of bytes actually sent. Otherwise, an error will occur!

0011000d91685178722902f00008000a5de54f5c61095febff01 the first byte 00 of the message indicates the information center number set in at + csca = + 8613800411500. Therefore, this information center number is removed from the PDU, however, I didn't execute the at + csca = + 8613800411500 command in my program, and the information was sent out. It should be the information of the information center number in the SIM card.

This PDU can also be changed to: 0891683369401105f011000d91685178722902f00008000a5de54f5c61095febff01. The text message content is "happy work !" Four words plus an exclamation point, the number of bytes sent is still 25, and the previous PDU is the same, just with the information center number!

Here we will explain the PDU: 0891683369401105f011000d91685178722902f00008000a5de54f5c61095febff01:

Segment the PDU: 0891683369401105f0 1100 0d91685178722902f0 000800 0a5de54f5c61095febff01

The first section is csca, that is, the information center number 08 91 683104401105f0: 08 is the byte length (number of characters/2) of "916830000401105f0" 91 represents the international number, therefore, a country sign is added to the number. 86 represents China. The actual number of the information center is 8613800411500. each two-digit number is regarded as a group, and the front and back are exchanged to get the previous representation. For more details, refer to the information I provide for details.

Section 2: 1100, 11: This byte is pdutype, which indicates sending SMS instead of receiving SMS. Of course, this Byte also contains other meanings. For details, see relevant documents. 00: For TP-MR, information reference, each sent a text message, Information Center will automatically add 1 to the value, generally set to 00.

The third section is the destination mobile phone number: 0d 91 685178722902f0. The OD is the hexadecimal representation of the character length of the destination mobile phone number (8615872792200), and the decimal value is 13. Note that this refers to the character length, instead of the number of bytes, this calculation is different from the calculation of the length of the information center number !! Therefore, the length does not include 91 or the supplemented F. 91 indicates the international number. The actual number is 8615872792200.

The fourth section is the data encoding: 000800 08 indicates that ucs2 encoding is used instead of the 8-bit encoding described in some online materials. The 8-bit encoding can use 04 or F6, for more information, see. If this is changed to 04, 8 encoding is used, and the text message content of my PDU is still encoded in UNICODE, that is, 0a5de54f5c61095febff01. In this way, the message sending command returns OK, but the recipient did not receive the text message !!! The other two bytes are not described in detail here (the previous 00 is the PID, and the later 00 is the information center retention time ).

The fifth part is the Information Content: 0a 5de54f5c61095febff01. 0a indicates the length of the information, that is, the byte length of "5de54f5c61095febff01". The Chinese character uses Unicode and is converted to ucs2 encoding.

Therefore, the PDU indicates the following information:

Information Center number: + 8613800411500

Target mobile phone number: + 8615872792200

The text message is: Happy work!

Finally, the sentcom () and print functions are attached:

Void sentcom (unsigned char * PD)
{
Rxcounter = 0; // reset the receiving buffer counter
Print (PD );
Print ("\ r \ n"); // the flag for sending the command. You can also enter only: \ r

}

Void print (unsigned char * PD)
{
While (* PD )! = '\ 0 ')
{
While (usart_getflagstatus (usart1, usart_flag_txe) = reset); // stm32 Serial Port 1
Usart_senddata (usart1, * PD );
PD ++;
}
} PS: At + cpin? // Check whether the SIM card works normally. If ready is returned, it indicates that the signal quality is normal at + CSQ. + CSQ: 99,0 indicates not known or not detectable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.