Send and receive short messages through serial port (up)

Source: Internet
Author: User
Tags arabic numbers printable characters
QHow can I program the implementation in applications to send and receive short messages through a serial port connected to a GSM mobile phone?

QWe plan to develop a GPS system based on GSM Short Message. How can we use SMS for data communication?

AFirst, we need to understand the SMS specification developed by ESTI. The rules related to short message sending and receiving discussed mainly include GSM 03.38, GSM 03.40, and GSM 07.05. The first two focuses on the technical implementation of SMS (including the encoding method), the latter sets out the DTE-DCE interface standard of SMS (AT command set ).
There are three methods to send and receive SMS messages: block mode, text mode, and PDU mode. Block Mode is a waste of time and is rarely used at present. Text mode is a text-only method. It can use different character sets and can be used technically to send short messages in Chinese. However, Chinese mobile phones are basically not supported in China and are mainly used in Europe and America. PDU mode is supported by all mobile phones and can use any character set. This is also the default encoding method for mobile phones. Text mode is simple and not suitable for custom data transmission. The following describes how to send and receive short messages in PDU mode.
A pdu string is an ascii code consisting of numbers and letters '0'-'9' and 'A'-'F. They are 8-bit hexadecimal numbers or BCD decimal numbers. The PDU string contains not only the displayed message, but also many other information, such as the SMS service center number, target number, reply number, encoding method, and service time. The structure of the PDU string sent and received is different. We first use two actual examples to describe the structure and arrangement of the PDU string.

Example 1 sending: the SMSC number is + 8613800250500, the peer number is 13851872468, and the message content is "Hello !". The PDU string sent from the mobile phone can be
08 91 68 31 08 20 05 05 F0 11 00 0d 91 68 31 58 81 27 64 F8 00 00 00 06 C8 32 9B FD 0e 01
For detailed analysis, refer to the following specifications:

Segmentation Description Description
08 Length of SMSC address information A total of 8 8 bits (including 91)
91 SMSC address format (ton/NPI) Number in international format (Add '+' in front ')
68 31 08 20 05 05 F0 SMSC address 8613800250500, fill in 'F' into an even number
11 Basic parameters (TP-MTI/VFP) Sent, TP-VP in relative format
00 Message Reference value (TP-MR) 0
0d Number of target addresses A total of 13 decimal numbers (excluding 91 and 'F ')
91 Target address format (ton/NPI) Number in international format (Add '+' in front ')
68 31 58 81 27 64 F8 Destination Address (TP-DA) 8613851872468, fill in 'F' into an even number
00 Protocol identifier (TP-PID) Common GSM type, point-to-point mode
00 User Information encoding method (TP-DCS) 7-bit encoding
00 Validity Period (TP-VP) 5 minutes
06 User Information length (TP-UDL) The actual length is 6 bytes.
C8 32 9B FD 0e 01 User Information (TP-UD) "Hello !"

Example 2 receive: the SMSC number is + 8613800250500, the peer number is 13851872468, and the message content is "Hello !". The PDU string received by the mobile phone can be
08 91 68 31 08 20 05 05 F0 84 0d 91 68 31 58 81 27 64 F8 00 08 30 21 80 63 54 80 06 4f 60 59 7d 00 21
For detailed analysis, refer to the following specifications:

Segmentation Description Description
08 Length of address information 8-bit bytes (including 91)
91 SMSC address format (ton/NPI) Number in international format (Add '+' in front ')
68 31 08 20 05 05 F0 SMSC address 8613800250500, fill in 'F' into an even number
84 Basic parameters (TP-MTI/MMS/RP) Receive, no more messages, reply address
0d Number of reply addresses A total of 13 decimal numbers (excluding 91 and 'F ')
91 Reply address format (ton/NPI) Number in international format (Add '+' in front ')
68 31 58 81 27 64 F8 Reply address (TP-RA) 8613851872468, fill in 'F' into an even number
00 Protocol identifier (TP-PID) Common GSM type, point-to-point mode
08 User Information encoding method (TP-DCS) Ucs2 Encoding
30 30 21 80 63 54 80 Timestamp (TP-SCTS) 2003-3-12 08:36:45 + 8 Time Zone
06 User Information length (TP-UDL) The actual length is 6 bytes.
4f 60 59 7d 00 21 User Information (TP-UD) "Hello !"

If the maximum bit (TP-RP) of the basic parameter is 0, there are no three segments that reply to the address. This is often the case for short messages sent from the Internet.
Note that the expression of the number and time does not follow the normal order, and the odd number must be supplemented into an even number with 'F.

QIn the above two examples, the 7-bit and ucs2 encoding methods have appeared. Please introduce these encoding methods in detail?

AIn PDU mode, three encoding methods can be used to encode the sent content: 7-bit, 8-bit, and ucs2. 7-bit encoding is used to send Common ASCII characters. It encodes a string of 7-bit characters (the highest bit is 0) into 8-bit data, each 8 characters can be "COMPRESSED" into 7; 8-bit encoding is usually used to send data messages, such as slices and ringtones; while ucs2 encoding is used to send Unicode characters. The maximum size of the user information (TP-UD) segment of the PDU string is 140 bytes, so the maximum number of characters of short messages that can be sent in these three encoding modes is 160, 140, and 70, respectively. Here, an English letter, a Chinese character, and a Data byte are regarded as a character.
It should be noted that the user information length (TP-UDL) of the PDU string has different meanings in various encoding methods. 7-bit encoding refers to the number of characters of the original Short Message, rather than the number of bytes After encoding. 8-bit encoding is the number of bytes. Ucs2 encoding is also the number of bytes, which is twice the number of characters of the original short message. If a header exists in user information (TP-UD for basic parameters is 1), the length of user information (TP-UDHI) in all encoding modes) both are equal to the sum of the header length and the number of encoded bytes. If the compression algorithm recommended by GSM 03.42 is adopted (the High 3-bit TP-DCS is 001), the length is also the sum of the number of bytes after compression or the length of the header and the number of bytes after compression.

The following example describes the 7-bit encoding process. We sent an English text message "Hello !" Encoding:

The source string is encoded into a group of 8 characters (less than 8 characters in this example), and the characters in the group are compressed, but there is no connection between each group.

The 7-bit encoding and decoding algorithm using C is as follows:

// 7-bit encoding // psrc: Source string pointer // pdst: Destination encoded string pointer // nsclength: Source String Length // return: int gsmencode7bit (const char * psrc, unsigned char * pdst, int nsclength) {int NSRC; // The Count value of the source string int ndst; // int nchar; // The number of characters in the group currently being processed. The value range is 0-7 unsigned char nleft; // The residual data of the previous byte // The Count value is initialized NSRC = 0; ndst = 0; // The Source string is divided into one group of every eight bytes, compress into 7 bytes // This processing process is cyclically until the source string is processed // if the group is less than 8 bytes, while (NSRC <nsclength) can also be correctly processed) {// minimum 3-bit nchar = NSRC & 7; // process each byte of the source string if (nchar = 0) {// The first byte in the group is saved. When the next byte is to be processed, use nleft = * psrc;} else {// other byte in the Group to add the right part of the byte to the residual data, get a target encoded byte * pdst = (* psrc <(8-nchar) | nleft; // returns the left part of the byte, save as residual data nleft = * psrc> nchar; // modify the pointer and calculated value of the target string pdst ++; ndst ++ ;} // modify the pointer and counter value of the source string psrc ++; NSRC ++;} // return the length of the target string return ndst;} // 7-bit decoding // psrc: source encoding string pointer // pdst: Destination string pointer // nsclength: source encoding String Length // return: Destination String Length int gsmdecode7bit (const unsigned char * psrc, char * pdst, int nsclength) {int NSRC; // The Count value of the source string int ndst; // The Count value of the target decoding string int nbyte; // the sequence number of the byte in the group currently being processed, the value range is 0-6 unsigned char nleft; // The residual data of the last byte // The Count value is initialized NSRC = 0; ndst = 0; // initialize the byte sequence number and residual data in the group. nbyte = 0; nleft = 0; // divide the source data into one group for every seven bytes, decompress the package to 8 bytes // repeat the processing process until the source data is processed // if the group is less than 7 bytes, The while (NSRC <nsclength) can also be correctly processed) {// Add the right part of the Source byte to the residual data, remove the highest bit, and get a target decoded byte * pdst = (* psrc <nbyte) | nleft) & 0x7f; // Save the left part of the byte as the residual data nleft = * psrc> (7-nbyte); // modify the pointer and calculated value of the target string pdst ++; ndst ++; // modify the Byte Count value nbyte ++; // If (nbyte = 7) to the last byte in a group) {// obtain an additional target decoding byte * pdst = nleft; // modify the pointer and calculated value of the target string pdst ++; ndst ++; // initialize the byte sequence number and residual data in the group. nbyte = 0; nleft = 0;} // modify the source string pointer and calculated value psrc ++; NSRC ++ ;} * pdst = 0; // return the length of the target string return ndst ;}

It should be noted that the 7-bit character set is inconsistent with the ANSI standard character set, and some printable characters are also arranged below 0x20, however, the English letters, Arabic numbers, and common symbols have the same positions. Use the algorithm described above to send and receive short messages in English only. Generally, it is enough. If it is French, German, or Spanish, it contains the characters "é" and "é", you need to look up the table according to the output of the above encoding. See the regulations of GSM 03.38.

8-bit encoding does not specify specific algorithms.

Ucs2 encoding converts each character (1-2 bytes) to a 16-bit Unicode wide character according to ISO/iec000046. In Windows systems, especially in 2000/XP, you can simply call API functions for encoding and decoding. If there is no system support, for example, using a single-chip microcomputer to control the mobile phone module to send and receive short messages, you have to use the look-up table method to solve the problem.

In Windows, the algorithm for ucs2 encoding and decoding using C is as follows:

// Ucs2 encoding // psrc: Source string pointer // pdst: Destination encoded string pointer // nsclength: Source String Length // return: destination encoded String Length int gsmencodeucs2 (const char * psrc, unsigned char * pdst, int nsclength) {int ndstlength; // Unicode wide Character Count wchar [128]; // Unicode string buffer // string --> Unicode string ndstlength =: multibytetowidechar (cp_acp, 0, psrc, nsclength, wchar, 128); // high/low byte reconciliation, output for (INT I = 0; I <ndstlength; I ++) {// first output the high byte * pdst ++ = wchar [I]> 8; // post-output low byte * pdst ++ = wchar [I] & 0xff;} // return the length of the target encoding string return ndstlength * 2;} // ucs2 decoding // psrc: source encoding string pointer // pdst: Destination string pointer // nsclength: source encoding String Length // return: Destination String Length int gsmdecodeucs2 (const unsigned char * psrc, char * pdst, int nsclength) {int ndstlength; // Number of Unicode wide characters wchar [128]; // Unicode string buffer // high/low byte pairs, assembled into Unicode for (INT I = 0; I <nsclength/2; I ++) {// The first high byte wchar [I] = * psrc ++ <8; // post-low byte wchar [I] | = * psrc ++;} // Unicode string --> string ndstlength =: widechartomultibyte (cp_acp, 0, wchar, nsclength/2, pdst, 160, null, null); // Add the end character pdst [ndstlength] = '/0' to the output string; // return the length of the target string return ndstlength ;}

Using the above encoding and decoding modules, you cannot encode short message strings into the formats required by PDU strings, or directly decode user information in the PDU strings into short message strings, this is because there is a difference between the printable string and the byte data. You can call the sscanf and sprintf functions cyclically to implement this transformation. The following provides algorithms that do not use these functions. They are also applicable to single-chip microcomputer and DSP programming environments.

// Printable string converted to byte data // For example: "c8329bfd0e01" --> {0xc8, 0x32, 0x9b, 0xfd, 0x0e, 0x01} // psrc: source string pointer // pdst: Destination Data Pointer // nsclength: Source String Length // return: Destination Data Length: int gsmstring2bytes (const char * psrc, unsigned char * pdst, int nsclength) {for (INT I = 0; I <nsclength; I + = 2) {// output 4-bit high if (* psrc> = '0' & * psrc <= '9') {* pdst = (* psrc-'0 ') <4;} else {* pdst = (* psrc-'A' + 10) <4;} psrc ++; // output 4 lower if (* psrc> = '0' & * psrc <= '9') {* pdst | = * psrc-'0 ';} else {* pdst | = * psrc-'A' + 10;} psrc ++; pdst ++;} // return the Target Data Length returnnsrclength/2 ;} // convert byte data to printable strings // For example: {0xc8, 0x32, 0x9b, 0xfd, 0x0e, 0x01} --> "c8329bfd0e01" // psrc: source Data Pointer // pdst: Destination string pointer // nsclength: Source Data Length // return: Destination String Length int gsmbytes2string (const unsigned char * psrc, char * pdst, int nsclength) {const char tab [] = "0123456789 abcdef"; // character query table for 0x0-0xf (INT I = 0; I <nsclength; I ++) {// output 4 low * pdst ++ = tab [* psrc> 4]; // output 4 high * pdst ++ = tab [* psrc & 0x0f]; psrc ++;} // Add an ending character * pdst = '/0' to the output string; // return the length of the target string, return nsclength * 2 ;}

We will not discuss the compression algorithm in GSM 03.42. If you are interested, you can study it in depth.

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.