C # SMS sending software

Source: Internet
Author: User

This text message sending software is connected to a text message modem through the serial port of the Computer (here I use a Siemens 6688 mobile phone), and the software is used to control the text message modem to send text messages.

Now that you need to communicate with the sms modem through the serial port, you need a serial communication control. Here I use Microsoft's mscomm32.ocx serial communication control. The specific implementation method is to set the attributes of mscomm32.ocx (serial port setting, sending data type setting, read/write buffer setting, etc.) to achieve the purpose of communication between the serial port and sms modem, encode the entered Short Message, send the encoded Short Message to the sms modem, and then control the sending of the modem through the AT command. The entire software consists of threeProgramComponents: one is the main sending program, the other is the short message encoding program, and the third is the serial communication control, which is described one by one below.

I. Serial Communication controls

Microsoft's serial communication control MSComm simplifies programming and allows you to quickly establish communication applications. The MSComm control transmits and receives data through the serial port to provide the serial communication function for applications. MSComm controls are very convenient for serial programming. programmers do not have to spend time learning more complex API functions.
MSComm attributes and events
The MSComm control has many important attributes. Here we only talk about the attributes of this application.
1) comport: Set and return the communication port number. during design, it can be set to any number from 1 to 16. The comport attribute must be set before the port is opened;
2) settings: Set and return the baud rate, parity, data bit, and stop bit in the form of a string. The format is "BBBB, P, D, s", and BBBB is the baud rate, P indicates the parity, D indicates the number of data digits, and s indicates the number of stopped digits;
3) portopen: Set and return the status of the communication port. You can also open or close the port. True indicates that the serial port is opened;
4) input: return and delete characters from the receiving buffer;
5) Output: Write a string to the transmission buffer;
6) inputmode: sets the type of data to be sent and received. inputmodeconstants. cominputmodetext indicates the text mode, and inputmodeconstants. cominputmodebinary indicates the binary mode;
7) inputlen: number of bytes read from the receiving buffer. If the value is 0, the data in the entire buffer zone is read.
8) outbuffercount: return the number of bytes waiting for sending in the sending buffer. You can clear the buffer by setting this attribute to 0.
9) inbuffercount: return the number of bytes waiting for sending in the receiving buffer. You can clear the buffer by setting this attribute to 0.

Ii. Text message encoding

Short Message is text information, text message modem only supports ASCII format code text information, ASCII format includes English letters A-Z and numbers 0-9 and some punctuation characters, for this reason, Chinese text messages must be encoded into ASCII code. There are three types of text message encoding: block mode, at command-based text mode, and at command-based PDU mode encoding, the Siemens 6688 mobile phone supports PDU mode encoding. This encoding method sends the text message body after hexadecimal encoding. PDU encoding can be divided into 7-bit, 8-bit, and 16-bit encoding. 7-bit encoding can contain up to 160 characters, and 8-bit encoding can contain up to 140 characters, 16-bit encoding can contain up to 70 characters. This encoding is used to display Unicode (ucs2) style information and can be displayed on most mobile phones. Therefore, I use ucs2 encoding, it can contain up to 70 characters, whether in English or Chinese.

A text message consists of three parts: one is the recipient's mobile phone number, the other is the SMS center number, and the third is the text message content. In the actual coding process, the three parts are separated and combined into one.

(1) SMS center number encoding

First, swap the odd and even digits of the text message center number, and then check whether the length is an even number. If not, add f at the end and swap the last digit with F. Then add 91 to the front of the number, which is equivalent to adding a "+" character. Finally, the total length after the encoding is calculated and divided by 2, and the number is formatted as two hexadecimal numbers. For example, if the SMS center number in Guangzhou is 8613800200500 and the encoded value is 08916830000200005f0, 08 is the length of 916830000200005f0 except 2, that is, 16/2 = 8, and the binary hexadecimal representation is 08.

(Ii) receiver's mobile phone number code

First, check whether the first two digits of the current receiver's mobile phone number have 86 characters. If not, add 86, and then swap the parity. Finally, check whether the entire length is even. If not, add F, and swap the last digit with F. For example, if the mobile phone number is 13918765434, the encoding is 683119785634f4.

(3) Text Message Content Encoding

First, it adopts the unicode format of the big-Endian byte sequence, that is, the high and low bit interchange, and then stores it in a byte array, and removes the "-" character during the Unicode encoding process, finally, divide the entire encoded text message content by 2 and format it into two hexadecimal numbers before the encoded text message content.

For example, the text message content is: Hello, hello !, After encoding: 4f60597dffoc00480065006c006c006f0021, add 11000d91 before the mobile phone number encoding according to the domestic PDU encoding principle, add 000800 before the text message content, and encode the above three parts.

Iii. At command

The at command is similar to the doscommand. It consists of commands and some parameters to control a function of modem. There are many AT commands. The basic format is at + operation operator + carriage return. The text message sending software I wrote only uses two at commands: at + csca? And at + cmgs, the first command is to get the SMS center number stored in the SIM card of the mobile phone, and the other command is to send a text message. Generally, an AT command is sent to the modem. If the modem completes execution, an execution result is returned.

4. Main Program of the SMS sending software

Below is the main program'sCodeFigure 1 shows the running interface of the main program

 

Pdudecoding SMS =NewPdudecoding ();

MSComm COM =NewMSComm ();

Thread t, W;

Private

{

Xiancheng1 ();

}

VoidXiancheng1 ()// Connect to the serial port

{

T =NewThread (NewThreadstart (initcom ));// Define the connection thread

T. Start ();// Start the thread

}

Private BoolLianjie (StringDuankou,StringBotelv)

{

Try

{

StringDk = duankou. Trim (). substring (3 );

If(COM. portopen =True) COM. portopen =False;

Com. commp ORT = convert. toint16 (DK );// Serial number

Com. inputmode = mscommlib. inputmodeconstants. cominputmodetext;// The transmitted data is in the text format

Com. inputlen = 0;// Read the content of the entire receiving buffer

Com. settings = botelv + ", N, 8, 1 ";// Set basic parameters for the serial port

Com. portopen =True;// Open the serial port

Com. outbuffercount = 0;// Clear the sending Buffer

Com. inbuffercount = 0;// Clear the receiving buffer

Return True;

}

Catch

{

MessageBox. Show ("error: the connection parameter is incorrect. select another one! ");

Return False;

}

}

 

VoidInitcom ()

{

IntI;

If(Lianjie (duankou. selecteditem. tostring (), bote. selecteditem. tostring ()))

StringBuffer = "";

StringBufy = "at + csca? \ R ";

For(I = 0; I <5; I ++)

Com. Output = bufy;// Send data to the serial port sending Buffer

Buffer + = com. input;// Receives data from the buffer area from the serial port.

 

Break;

}

}

If(I> 4)

{

Label5.text = "connection failed. Try again !! ";

}

Else

{

IntNum1 = buffer. lastindexof ("86 ");

Zhongxinhao. Text = buffer. substring (num1, 13 );

Label5.text = "connection successful !! ";

}

}

Else

{

Label5.text = "connection failed. Try again !! ";

}

Com. portopen =False;

T. Abort ();

}

Private VoidButton2_click (ObjectSender, system. eventargs E)

{

If(COM. portopen =True) COM. portopen =False;

Application. Exit ();

}

VoidFsduanxin ()

{

W =NewThread (NewThreadstart (FS ));// Define the sending thread

W. Start ();// Start the thread

}

Private VoidButton3_click (ObjectSender, system. eventargs E)

{

Fsduanxin ();

}

VoidFS ()

{

If(Lianjie (duankou. selecteditem. tostring (), bote. selecteditem. tostring ()))

{

StringDhnum = shoujihaoma. text;

Fsdx (dhnum );// Send SMS

Com. portopen =False;

W. Abort ();

}

}

VoidFsdx (StringDanw.haoma)// Send the SMS segment

{

IntI = 0;

Duanxinshoufa. Text = "sending, please wait ";

StringBuffer = "";

StringDecodedsms = SMS. smsdecodedsms (zhongxinhao. Text, danw.haoma, neirong. Text );

StringBufy = "at + cmgs =" + SMS. nlength + "\ r ";

For(I = 0; I <10; I ++)

{

Com. Output = bufy;

Thread. Sleep (300 );

Buffer + = com. input;

Thread. Sleep (500 );

If(Buffer. length> 0 & buffer. endswith ("> "))// Determine the return result of at Command Execution

{

StringBuf = decodedsms + "\ x01a ";

Com. Output = Buf;

Thread. Sleep (1000 );

StringChenggong = "sent successfully! ";

StringResult = string. Format ("{0}, {1}, {2 }. \ N ", danw.haoma, neirong. Text, Chenggong );

Duanxinshoufa. Text + = "\ r \ n" + result;

Break;

}

Duanxinshoufa. Text + = ".";

}

 

If(I> 9)

{

Duanxinshoufa. Text = "sending failed. Please try again! ";

W. Abort ();

}

}

Thread control is used in the program to coordinate the synchronization between program running and serial communication. In addition, a loop is used when at commands are used in the main program. By checking the sending result of AT commands, you can determine whether at commands are correctly executed. If at commands are not correctly executed, the at command is re-issued through loop control until the AT command is correctly executed.

The interface of this program is easy to use. I put it for users who are interested (Click here to download it ). Note:

1. I only applied this program on the Siemens 6688 and sl45i mobile phones.

2. This program is developed on the. NET platform. Therefore, you need to install ms.net. framework1.1 on your computer. This is a free software and has been downloaded online.

3. The mscomm32.ocx control used in the program needs to be registered. The registration procedure is as follows:

① Copy the mscomm32.ocx and mscomm32.dep files to the System32 folder.

② Register the OCX control with regsvr32, a registration tool under Windows, click "start"-> "run", and enter regsvr32 c: \ windows \ system32 \ mscomm32.ocx in it.

③ Manually create a primary key entry in the Registry: First click "start"> "run", then enter the Regedit command to open the registry, and find hkey_classes_root \ licenses, add the primary key 4250e830-6ac2-11cf-8adb-00aa00c00905 and set the content to kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun.

 

 

 

 

 

 

 

{

If(Buffer. indexof ("86 ")! =-1)// Determine the return result of at Command Execution

 

 

Thread. Sleep (1000 );{{

 

 

 

 

 

 

 

void button#click ( Object sender, system. eventargs e)

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.