Send Chinese text messages to the gsm Module

Source: Internet
Author: User

Send a Chinese text message using two AT commands

Set to PDU Mode
AT + CMGF = 0 or 1 Enter // set the TEXT message encoding mode to 0: PDU Mode 1: TEXT mode only letters or numbers are supported. To send a Chinese TEXT message, only PDU Mode C can be expressed as follows:
"AT + CMGF = 0/r/n"

Send SMS
"AT + CMGS ="

Pdu encoding format
08 // short message center Length
91 // the short message center has a country code not 81
68 31 08 02 00 05 f0 // Short Message Center
31 // Status Report required // 11 is not required
00 // always 00 // delimiter
0D // The length of the target number is 14 characters
91 // There is a country code // No 81
68 31 36 32 64 49 F0 // target number + 8613632346940
00 // TP-PID. Protocol identifier, generally 00 is a common GSM type, point-to-point method
08 // hands-free or not // It is 18
A7 // validity period sign
04 // 4 bytes of Body
4F 60 59 7D // your short message content

If there is no short message center, the code is:
00 // indicates the default Short Message Center
31 // Status Report required // 11 is not required
00 // always 00 // delimiter
0D // The length of the target number is 14 characters
91 // There is a country code // No 81
68 31 36 32 64 49 F0 // target number + 8613632346940
00 // TP-PID. Protocol identifier, standard GSM type, point-to-point mode
08 // hands-free or not // It is 18
A7 // validity period sign
04 // 4 bytes of Body
4F 60 59 7D // your short message content

The above is written by a ghost blog. In short, the PDU encoding package format of the SMS is as follows:
SMS center number machine configuration flag target phone number machine configuration flag text message content

SMS Center Number:
The length of the SMS Center Number: (the total number of characters divided by the hexadecimal number of 2 together with the Country Code identifier not sent after 91)
Whether to send the country code identifier (91 indicates that the phone number is not sent plus before the phone number)
SMS center number: Fuzhou Unicom's 13010380500 (different regions). If the country code mark is sent before
If it is 91, 86 (China's International Code) should be added before the number) that is, 8613010380500 if the number of characters is an odd number, then the number should be supplemented with F to make it an even number, that is, 8613010380500F. Finally, the parity-adjusted SMS center number will be changed to 683110300805F0.
Is it dizzy ?? Fortunately, generally, when the SIM card leaves the factory, it will pre-store the SMS center number with the command at + csca? Press enter to get the SMS center number and you will see it. If you see the returned result as follows + csca: "+ 8613010380500", 145 already exists. If not, you can also use the command at + csca = "+ 8613010380500" to press enter to set the SMS center number. Now you can use 00 to replace the above troublesome string.
Machine configuration flag: 3100 or 1100
Target phone number:
Target phone number length: Target phone number (excluding 91 !) ASCII value of the hexadecimal number of characters
Whether to send the country code identifier: 91
Target phone number: The packaging method is the same as that in the SMS center above.
For example, if my phone number 13860619634 is packaged, it becomes the 0d91684258609163f4 target phone number. Of course, if you want to send it to yourself, you can also use 00 to replace the above troublesome string, haha.
Machine configuration ID: 000800
Text message content:
SMS length: Unicode code of the SMS content (big-Endian) the length of ASCII characters in hexadecimal notation is divided by 2 (that is, the number of characters in the text message multiplied by 2 (that is, the number of bytes occupied by the text message). Unicode encoding occupies two bytes for one character) hexadecimal value
Text message content: the Unicode-encoded hexadecimal ASCII character. For example, I Want You To 621160f34f60.

The process of sending a text message is roughly as follows:
AT command text message byte line break/r text message PDU encoding package hexadecimal number 0x1A (that is, the value of Ctrl + Z)
Specifically, the sending process is as follows:
1. Send the AT command AT + CMGS = string (that is, the ASCII code value)
2. Text message PDU encoding package length (number of characters except the SMS center in the PDU encoding package divided by 2) the ASCII value of the BCD code
3. Send '/R'
4. Send the above packaged sms pdu encoding package
5. Issue 0x1A
Now you can try it. Connect your phone and open the serial port debugging assistant. Select the serial port and then send the following
AT + CMGF = 0
AT + CMGS = 21
0011000d9168da-8609136f400080006621160f34f60
Send it to me, content I miss you
Ah! I will be so flattered...

 

Below is the C program sent by SMS
// Package the target phone number
Void GSM_PackPhone (uchar * str)
{
Uchar count = 0, I = 0, j = 0, tmp;

Strcpy_f (gsm. set, Gsm_Phpre );
If (* str = '1') // Add "86" to the phone number"
{
Gsm. phone [0] = '8 ';
Gsm. phone [1] = '6 ';
Count = 2;
}
While (* str)
{
Gsm. phone [count] = * str ++;
Count ++;
}

Gsm. phonelen [0] = pgm_read_byte (ASC_NUM + (count> 4); // ASC code value of the phone number length
Gsm. phonelen [1] = pgm_read_byte (ASC_NUM + (count & 0x0F ));

If (count % 2) // The number is an odd complement of 'F'
Gsm. phone [count] = 'F ';
For (I = 0; I <count; I + = 2) // parity
{
Tmp = gsm. phone [I];
Gsm. phone [I] = gsm. phone [I + 1];
Gsm. phone [I + 1] = tmp;
}
Gsm. phone [count + 1] = 0;
}
Void GSM_PackMess (uchar * mess)
{

// Because the project sends an SMS alarm and the content is relatively fixed, the text message content has been converted and packaged as a constant variable.

}
// Set it to PDU Mode
Void GSM_SetPDUmode (void)
{
Uchar atcmd [] = {"AT + CMGF = 0/r/n "};
U1_TxOpen (0 );
U1_SendStr (atcmd );
}
// Determine whether OK is returned
Uchar GSM_IsOK (void)
{
Uchar I = 0;
While (umo-buf [I])
{
If (umo-buf [I] = 'O' & umo-buf [I + 1] = 'k ')
{
Cldata (uw.buf, sizeof (uw.buf ));
Return OK;
}
I ++;
If (I> sizeof (uw.buf ))
Break;
}
Return ERROR;
}

// Send a message
Void GSM_SendMess (uchar * phone, uchar * mess)
{
Uchar strl;
Uchar atcmd [] = {"AT + CMGS = "};
Uchar mspre [] = {"000800 "};
Uchar * str = "AT + CMGS = 15 ";
GSM_PackPhone (phone );
Strl = (strlen (gsm. set) + strlen_f (Gsm_Mspre) + strlen (mess)-2 );
// Strl = (strlen (gsm. set) + strlen_f (Gsm_Mspre) + strlen_f (mess)-2); // The number of SMS messages except the SMS center in hexadecimal notation
Strl/= 2;
Strl = tobcd (strl); // convert to bcd code 10 hexadecimal number
Gsm. messlen [0] = pgm_read_byte (ASC_NUM + (strl> 4 ));
Gsm. messlen [1] = pgm_read_byte (ASC_NUM + (strl & 0x0F ));
U1_TxOpen (0 );
U1_SendStr (atcmd); // sends the CMGS command
U1_SendData_C (gsm. messlen, sizeof (gsm. messlen); // The total length of the text message.
U1_SendByte ('/R'); // issue 0x0D
U1_SendStr (gsm. set); // The SMS center and target phone number.
U1_SendStr (mspre); // sending information prefix
U1_SendStr (mess); // text message content
U1_SendByte (0x1A); // press Ctrl + Z

GsmState. GSM_SED = 1; // The SMS has been sent.
}

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.