C # Introduction to the principle and method of developing terminal-type SMS GSM

Source: Internet
Author: User
Tags microsoft website
C # Introduction to the principle and method of developing terminal-type SMS GSM

Young people who haven't sent text messages must belong to the very rare protected animal. Today's well-developed text messages have become an important means for people to communicate, which also contains huge market and economic benefits, those who master the text message technology are also sought after by major companies and are currently a shining star in the workplace. This article introduces the principles and implementation methods of text messages, focuses on the encoding method, at command of text messages, and serial communication method using C.

  

Preface

  

Currently, there are three ways to send text messages:

  

1. Gateway: it applies to the local telecommunications department and does not require additional equipment. It is applicable to large communication companies such as Huawei, aotian, ZTE, and Asiainfo.

  

2. Terminal Mode: connect to the computer through a data cable to send text messages using settings such as GSM Modem (mobile phones supporting AT commands can also, this method is applicable to small and individual users. To implement this method, you must understand serial communication, at command, SMS encoding, and decoding, which is also the focus of this article.

  

3. Some websites are used for implementation. The method is simple. However, website dependencies are too high and network requirements are high, which is not suitable for project development.

  

    

Principles

  

SMS code

  

In terms of sending and receiving text messages, there are three modes generated successively by Time: block mode, text mode based on AT command, PDU Modem Based on at command, text mode is relatively simple, multiple Nokia mobile phones support this mode. Most Siemens mobile phones only support the PDU mode. The PDU mode is a method for sending or receiving SMS messages from mobile phones. The text of Short Messages is transmitted after hexadecimal encoding. Currently, PDU has replaced block mode because we mainly discuss the transmission of PDU mode. Take the Siemens 3508 mobile phone as an example.

  

SMS is a specification developed by Etsi (GSM 03.40 and GSM 03.38 ). When 7-bits encoding is used, it can send a maximum of 160 characters. However, it is 8-bit encoded and can send a maximum of 140 characters, which is usually not displayed on a mobile phone; it can contain up to 70 characters in 16-bit encoding and is used to display Unicode (UCS2) text information, which can be displayed on most mobile phones. We are talking about UCS2 encoding today. That is to say, a maximum of 70 characters can be sent, whether in English or Chinese.

  

For example, we want to send the following message to my mobile phone 13715342642. "Hello, Hello! ". Before sending the email, you must be clear that the SMS number in the SIM card's location is not the SMS center number in your current location. For example, if I am in Shenzhen, the SMS center number in Shenzhen is: 8613800755000. Even if I am outside China, the SMS center number is still in Shenzhen. We get the following information from the above:

  

Received mobile phone number: 13715342642

SMS center No.: 8613800755000

Text message content: Hello, Hello!

  

In actual use, the above information is not executed by the mobile phone, and will be executed only when the mobile phone is encoded. Check the encoded information first:

  

Bytes

  

Let me explain it:

  

08-refers to the length of the SMS center number, that is, the length of (91) + (683104705500f0)

  

91-indicates the short message center number type. 91 indicates that TON/NPI complies with the International/E.164 standard and requires the plus sign (+) before the number. In addition, there are other values, but 91 is the most commonly used.

  

68310000705500f0-SMS center number. The actual number should be 8613800731500 (the letter F indicates the length minus 1) because the location is slightly processed ). This needs to be modified based on different regions. The preceding (08) + (91) + (6830000705500f0) actually forms a part of the entire text message, which is called the address of the SMSC ).

  

11-File Header bytes

  

00-information type (TP-message-Reference)

  

0d-length of the callednumber

  

91-called Number Type

  

In actual processing, we usually write 11000d91 into the program, because in China, the data will not change.

  

683117352446f2-called number. After displacement processing, the actual number is "8613715342642 ". The above (00) + (0d) + (91) + (683117352446f2) constitutes the second part of the destination address (TP-destination-address) of the entire text message ).

  

00-Protocol Identification TP-PID, which is generally 00

  

08-data encoding scheme TP-DCS (TP-data-coding-scheme), using the previously mentioned USC2 (16bit) Data Encoding

  

00-period TP-VP (TP-valid-period)

  

12-length TP-UDL (TP-user-data-length), that is, 4f60597dff0c00480065006c006c length 36/2 = 18 hexadecimal 12

  

4f60597dff0c00480065006c006c 006f0021-here is the text message content. The actual content is: "Hello, hello! "

  

At command

  

When talking about AT commands, there is a thick book, which is beyond the scope of our discussion today. Here I will only discuss the several AT commands necessary to send text messages.

  

The gsm at commands (from GSM07.05) related to SMS are shown in table 1:

  

AT command power

AT + CMGC Send an SMS command (Send a short message command)

AT + CMGD Delete SMS message (Short message for deleting SIM card memory)

AT + CMGF Select SMS message formate (Select the Short message format: 0-PDU; 1-text)

AT + CMGL List SMS message from preferred store (List short message PDU/text: 0/"rec unread"-UNREAD, 1/"rec read"-READ, 2/"sto unsent"-to be SENT, 3/"sto sent"-SENT, 4/"ALL"-ALL)

AT + CMGR Read SMS message (Read short message)

AT + CMGS Send SMS message (Send short message)

AT + CMGW Write SMS message to memory (Write a short message to be sent to the SIM memory)

AT + CMSS Send SMS message from storage (Send short messages from SIN | M memory)

AT + CNMI New SMS message indications (display New short messages)

AT + CPMS Preferred SMS message storage (select Short message Memory)

AT + csca sms service center address (Short Message center address)

AT + CSCB Select cell broadcast messages (Select cellular broadcast messages)

AT + CSMP Set SMS text mode parameters (Set Short Message text mode parameters)

AT + CSMS Select Message Service (Select short Message Service)

Table 1: Related gsm at commands

  

The following example shows how to use these commands:

  

Connect your mobile phone to your computer's serial port with the mobile phone data cable and set the baud rate of the serial port to 19200.

  

1. First, test whether your connection and mobile phone support the AT command. In your serial port debugging program, enter:

  

AT <press enter>

  

If "OK" is returned on the screen, it indicates that the computer is connected to the mobile phone normally, so that we can test other AT commands.

  

2. Set the text message sending format

  

AT + CMGF = 1 <press enter>

  

If "OK" is returned on the screen, the current message sending method is PDU. If it is set to TEXT, AT + CMGF = 0 <press enter>

  

3. send text messages

  

The sent content and manual number are still the same as those in the encoding. After encoding, the data to be sent is as follows:

  

Bytes

  

We use the following command to send

  

AT + CMGS = 33 <press enter>

  

If ">" is returned, enter the encoded data above and end with CTRL + Z. Wait a moment and you will see that the return result is OK.

  

Let's explain why AT + CMGS = 33 is like this:

  

11000D91683117352446F2000800124F60597D002C00480065006C006C006F0021

  

The result is obtained by dividing the length of this string by 2. The above string, the SMS center number, and the text message content are obtained. For details, refer to the Decoding Section.

  

In our previous discussion, a complete text message is sent, as long as three AT commands are executed, AT, AT + CMGS =? , AT + CMGS =? You can. Due to the length, I can only mention so much here. If you want to learn more, you can ask mobile phone manufacturers for the AT Instruction White Paper, which is very detailed.

  

As mentioned above, we can only prepare for the actual situation. We also need a transmission channel. Based on our needs, we choose to invest the least to achieve convenient serial communication. Note: The serial port is connected to the mobile phone through the data line, and the AT command is used to send text messages. When we select the data line, we recommend that you purchase the original factory configuration instead of the original factory configuration. during use, some problems often occur. For example, if the screen of a mobile phone is black, the mobile phone always prompts that the battery power is insufficient.

Serial Communication

  

Many people are overwhelmed to implement serial communication in C, on the forum, we often see problems such as "How to Use MSCOMM to implement serial communication" and "how to connect devices through serial ports. As a matter of fact, foreign netizens have long listed these items in the FAQ.

  

Generally, there are four methods to implement serial communication in C:

  

First, the MSCOMM control is the simplest and most convenient method. It is difficult to control the functions. At the same time, this control is not included in the system, so it has to be registered. It is not covered in this article. Can access http://www.devhood.com/tutorials/tutorial_details.aspx? Tutorial_id = 320. The author was very enthusiastic about the tutorial written by a foreign netizen. I sent an email to him and soon replied.

  

Second: Microsoft. NET introduces a new serial control based on. net p/Invoke call method implementation. For more information, visit the Microsoft website http: // msdn.microsoft.com/msdnmag/issues/02/10/netserialcomm/default.aspx.

  

Third: third-party controls are used, but payment is generally required. They are not practical and are not considered.

  

Fourth, it is difficult to write Serial Communication Using APIs, but it is convenient for us to implement various functions we want.

  

In this article, we use the fourth method to implement serial communication, but we did not write it by ourselves. We use a library already encapsulated by a foreign user. However, the function is simple and it is enough for us.

  

During the SMS operation on the terminal, only four functions are used for communication with the serial port to enable, write, read, and disable the serial port. The class library defines these four functions:

  

Open the serial port:

  

Function prototype: public void Open ()

  

Note: Open the preset Port

  

Example:

  

Using JustinIO;

  

Static JustinIO. comatrix ORT ss_port = new JustinIO. comatrix ORT ();

Ss_port.PortNum = COM1; // port number

Ss_port.BaudRate = 19200; // The baud rate of serial communication.

Ss_port.ByteSize = 8; // data bit

Ss_port.Parity = 0; // parity

Ss_port.StopBits = 1; // stop bit

Ss_port.ReadTimeout = 1000; // read timeout

Try

{

If (ss_port.opened)

{

Ss_port.close ();

Ss_port.open (); // open the serial port

}

Else

{

Ss_port.open (); // open the serial port

}

Return true;

}

Catch (exception E)

{

MessageBox. Show ("error:" + E. Message );

Return false;

}

  

Serial Port writing:

  

Function prototype: Public void write (byte [] writebytes)

  

Writebytes is the byte you write. Note that strings must be converted to byte Arrays for communication.

  

Example:

  

Ss_port.write (encoding. ASCII. getbytes ("at + cgmi" R "); // obtain the mobile phone brand

  

Read Serial Port:

  

Function prototype: Public byte [] Read (INT numbytes)

  

Numbytes read cache count. Note that the byte array is read and character conversion is required in actual applications.

  

Example:

  

String response = encoding. ASCII. getstring (ss_port.read (128); // read 128 bytes of Cache

  

Close the serial port:

  

Function prototype: ss_port.close ()

  

Example:

  

Ss_port.close ();

  

I will only talk about this here because of its wide range of space and serial communication.

Related Article

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.