C # send SMS via serial port

Source: Internet
Author: User
Tags microsoft website

Mobile phone message Mass as a daily notice, announcements, weather forecasts and other information of a publishing platform, is low-cost, easy to operate and many other characteristics, become the first choice for corporate communications. This paper introduces the encoding of SMS, at instruction and the method of using C # to realize serial communication.

At present, there are three main ways of sending SMS:

1, Gateway mode: to the current telecommunications department applications, do not need additional equipment. But the cost is relatively high, to the current Shanghai mobile company launched the bulk SMS package, for small and medium-sized enterprises to send the number of short messages to see,

There is little advantage in texting mass and making calls. For a big company of tens of thousands of people, it could be cheap. This is not a cost-effective way for companies with hundreds of or fewer people.

2, the end way: With the help of GSM Modem (SMS cat) and other equipment, through the data line and PC serial port connection, to mass SMS, and the current telecommunications Department of the Thumb Faction sense of the zone, such as the package, the basic cost in a text message about 5 cents, and the price of SMS cat is different, the price varies , the cheapest two hundred or three hundred money can be done, the expensive also about 1000 yuan. This approach is suitable for small businesses and individuals, and this way does not require network support, only a computer with a serial port can be. But one drawback of this way is that if the number of people more than the case, the sending of information will be slow, and although SMS cat manufacturers propaganda support all GSM card, but in the market 150,158,159 card compatibility is poor, which I used 150 of the card, can only send information, and receive no reply message. This method needs to understand serial communication, at instruction, SMS encoding, decoding.

3, the use of the website to achieve, the way is very simple. However, the dependence on the Web site is strong, the stability of the network requirements are high, and need SMS middleware support, not suitable for SMS development.

4, is also the terminal way, is a high-performance SMS cat support, the text message cat can be inserted at the same time 2, up to 8 mobile phone card, the simultaneous SMS mass. At present, we receive a lot of mobile phone spam message is basically this way issued, this support 2 mobile phone card SMS cat price in about 1000 yuan, support 8 mobile phone card SMS cat price about 4,000 yuan. And the transmission speed is fast. But there are three drawbacks to this approach:

(1) The message cat in a period of time to send information is limited, such as only with a mobile phone card SMS cat, an hour can only publish 700 messages, (2) This message mass, will be regarded as spam messages by the telecommunications department, will be blocked out, such as Shanghai, the current way can not be used, during the Olympic Games, It seems that Beijing will also block out this information.

(3) The greater the amount of information sent, the lower the price of a single message. But is to send 100,000 text messages, the average price of a single message is also in the appearance of 5 cents. In detail below, the second type of SMS cat way of the principle of mass SMS. The principle of sending text messages is attached, which is not the focus of our development. SMS encoding in the transmission and delivery of text messages, by time to produce successively,

A total of three modes are produced:

Block mode, text mode based on at command, PDU Modem based on command, text mode is simple, and multiple Nokia phones support this mode. Most Siemens phones only support PDU mode, PDU mode is a way to send or receive SMS messages, text messages are sent by hexadecimal encoding. Currently

The 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 using 7-bits encoding, it can send up to 160 characters, but with 8-bit encoding, you can send up to 140 characters, usually not directly from the phone, and 16-bit encoding, a maximum of 70 characters, is used to display Unicode (UCS2) text information, Can be displayed by most mobile phones. Today we are talking about UCS2 encoding, which means that you can send up to 70 characters, whether English or Chinese.

Now for example we want to send the following message, send "Hello, hello!" to my phone 13715342642. Before sending, you should be clear, cell phone SIM card location of the SMS center number, not where you are located in the text message center number, like me in Shenzhen, Shenzhen, the Message center number is: 8613800755000, even if I now to the field, SMS Center is still Shenzhen.   From the above we get the following information: Received mobile phone number: 13715342642 SMS Center number: 8613800755000 SMS content: Hello, hello!. In the actual use, the above information is not executed for the mobile phone, to be encoded by the mobile phone will be executed, regardless, look at the encoded information: 0891683108705500f011000d91683117352446f2000800124f60597dff0c00480065006c006c006f0021 don't understand, let me explain: 08-refers to the length of the SMS center number , which means the length of (683108705500f0) + (91)-refers to the short information Center number type.   91 is TON/NPI comply with the international/e.164 standard, refers to the number before the ' + ' number, and there are other values, but 91 most commonly used. 683108705500F0-SMS Center number. Because the position is slightly handled, the actual number should be: 8613800731500 (the letter F refers to the length minus 1). This needs to be changed according to the different regions. The preceding (+) + (683108705500f0) actually constitutes a part of the entire text message, which is known as the SMS Center address of the SMSC.

11-File Header bytes

00-type of information (tp-message-reference)

0D-called number length

91-called Number type

In fact, in the actual processing, we usually write 11000d91 to die in the program, because at home, the data will not change. 683117352446F2-called number, after the displacement process, the actual number is "8613715342642".

Above (+) + (0D) + (683117352446F2), which constitutes the second part of the entire text message destination address (tp-destination-address).

00-Protocol ID Tp-pid, here is generally 00

08-Data encoding scheme Tp-dcs (tp-data-coding-scheme), using the previously mentioned USC2 (16bit) data encoding

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

12-Length Tp-udl (tp-user-data-length), that is, the length of 4f60597dff0c00480065006c006c 36/2 = 18 of 16 into the 4f60597dff0c00480065006c006c 00 6f0021-here is the text message content, the actual content is: "Hello, hello!" At command speaking at the AT command can be more, there is a thick book, not part of the scope of our discussion today, here I will only discuss in sending text messages must be used in a few at instructions.

The SMS-related GSM at instruction (from GSM07.05) is shown in table 1: The AT command function AT+CMGC send an SMS command (issue a short message) AT+CMGD Delete SMS message (remove the SIM card memory Short message) AT+CMGF Select SMS Message formate (select Short Message Information format: 0-pdu;1-text) AT+CMGL list SMS message from preferred Store (list short message PDU in SIM card) /text:

0/"REC Unread"-Unread,

1/"REC read"-Read,

2/"STO unsent"-ready to send,

3/"STO SENT"-Issued,

4/"All"-full)

At+cmgr read SMS message (reading short messages)

At+cmgs Send SMS message (send short messages)

AT+CMGW write SMS message to memory (writes a short message to the SIM memory for outgoing messages)

AT+CMSS Send SMS Message from storage (sin| m send short message in memory)

At+cnmi New SMS message indications (show newly received short message)

At+cpms Preferred SMS Message storage (select Short Message memory)

At+csca SMS Service Center address (Short Message center addresses)

AT+CSCB Select cell Broadcast messages (select cellular Broadcast message)

AT+CSMP set SMS text mode parameters (set short message textual pattern parameters)

AT+CSMS Select Message Service (choose SMS)

Table I: The relevant GSM at command I now use an example to illustrate how these instructions are used: First use the mobile phone data cable to connect the phone to the computer serial port, and the serial port baud rate is set to 19200, you can start.

1, first Test your connection and mobile phone support at command, please enter in your serial debugging program: at< Carriage return > The screen returns "OK" indicates that the computer and the phone are connected properly, so that we can carry out other at command test

2, set the text message send format at+cmgf=1< carriage return > The screen returned "OK" indicates that the message is now sent to the PDU mode, if it is set to text mode, then at+cmgf=0< carriage return >

3, send the text message to send the content and hand to the number is still the same as above in the code, after the code, get the data to be sent as follows   0891683108705505f011000d91683117352446f2000800124f60597d002c00480065006c006c006f0021 we use the following command to send at+cmgs=33< carriage return.   If you return ">", enter the above encoded data and end with Ctrl + Z, and wait a moment, and you will see the return OK. Explain why at+cmgs=33, that's how it gets: 11000d91683117352446f2000800124f60597d002c00480065006c006c006f0021 the length of the string divided by 2 results , the above string, text message center number plus SMS content to get, how to get, please review the decoding part in our discussion earlier, a complete SMS sent, as long as the implementation of three at the command, at, at+cmgs=? , at+cmgs=? You can do it. Due to space,

I can only mention so much here, if you want to know more, you can ask the handset manufacturers to request the AT command white Paper, which is very detailed.

As mentioned above, only for our actual preparation, we also have to send a way, according to our needs, we choose the least investment, to achieve a more convenient serial communication.  Note that the serial port through the data line with the mobile phone, with at command to achieve the sending of text messages, in our choice of data lines, the proposed purchase of the original factory, non-original matching, in the course of use, often appear some puzzling problems, for example, the phone screen is black, mobile phone always prompt battery power shortage and so on. Serial communication

In C # to achieve serial communication, many people are overwhelmed, in the forum can often see "How to use MSComm to achieve serial communication", "How to connect the serial port and equipment" and so on.   In fact, foreign netizens have already included these in the FAQ. In general, serial communication is implemented in C #,

We have four ways to:

First: Through the MSComm control this is the simplest and most convenient method. Can be functionally difficult to control freely, at the same time this control in VS2003 environment, if need to develop serial communication in the VS2003 environment, need to borrow this control in VB60, and also need to register, in the course of debugging procedures, will appear again let you register the situation (this interested friends , can add me qq:617586962, we discuss together, I use this way to develop several communication backstage). After VS2005, including the VS2008 in the control has been added to the use of convenient.

Second: Microsoft in. NET introduces a new serial port control, based on the. NET P/invoke call method implementation, detailed everyone can visit the Microsoft website http://msdn.microsoft.com/msdnmag/issues/02/10/netserialcomm/default.aspx, convenient to get more information.

Third: The use of third-party control, but generally have to pay, not very practical, not for consideration

IV: I use the API to write serial communication, so difficult high, but for us, can be convenient to achieve their own various functions in this article, we use the fourth method to achieve serial communication, but not their own writing, with a foreign user-ready already packaged class library, but the function of simple point, It's enough for us.

In the entire terminal text message operation process, and the serial port communication, only uses four functions, opens, writes, reads, closes the serial port. The following is a class library definition of these four features:

Open the serial port:

Function prototype: public void Open ()

Description: Open a pre-set port

Example:

Using Justinio;

static Justinio.commport Ss_port = new Justinio.commport ();

Ss_port. Portnum = COM1; Port number

Ss_port. BaudRate = 19200; Serial communication baud rate

Ss_port. ByteSize = 8; Data bits

Ss_port. Parity = 0; Parity check

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 ();//opening the serial port

}

return true;

} catch (Exception e)

{

MessageBox.Show ("Error:" + e.message);

return false;

} Write Serial:

Function Prototypes:

public void Write (byte[] writebytes)

Writebytes is your write byte, note that the string is converted to a byte array to communicate

Example:

Ss_port. Write (Encoding.ASCII.GetBytes ("at+cgmi/r")); Get the phone brand

Read Serial:

Function Prototypes:

Public byte[] Read (int numbytes)

NumBytes read into the cache number, note that the reading is a byte array, to be used in the actual application of character conversion

Example: string response = Encoding.ASCII.GetString (ss_port. Read (128)); Read 128 byte cache

Close the serial port:

Function prototype: Ss_port. Close ()

Example: Ss_port. Close (); Realize on the top of said half a day, SMS cat is actually two functions:

(1) The SMS content of the PC is sent to the cat via SMS;

(2) The outside of the text message through the cat to collect, in fact, the API is to initialize the device, read information, write information of the process;///////////////////this article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/hbqhdlc/ Archive/2008/10/02/3009889.aspx

C # send SMS via serial port

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.