Practice
At the beginning, you should prepare the following software and hardware:
Hardware: One Siemens 3508 or C35 Series Mobile Phone
One Siemens mobile phone communication data line
Software: vs. Net (C #)
SMS encoding class library (pdudecoding. CS)
Serial Communication Library (justinio. CS)
When the required software and hardware are ready, we can officially start. The following describes my test cases in detail.
Everything should be planned. Although our test cases are simple, we still draw a simple flowchart:
With the flowchart, I just understoodProgramHow to run it, and then look at the interface, it will make you more exciting.
Figure 2. Text message Terminal C # page
If you don't start again, someone will scold me. The development environment I am talking about is in vs. Net (C. Come go, go...
Step 1. Open vs. net, create a project, and select Visual C # project> Windows application. Enter your project name in the name. My name is smsforcsharp.
Step 2: design your program interface by referring to the interface diagram above. The following lists the main attributes of each control in my program.
Control name |
Control name attribute |
description |
textbox |
targetnumber |
receive mobile phone number |
textbox |
centernumber |
SMS center No. |
textbox |
smsstate |
the message returned after the SMS is sent. Note: Set the control to multiple rows. |
textbox |
smscontent |
text message content. Similarly, set as multiple lines |
ComboBox |
connectport |
the port connecting to the mobile phone, for example, COM1 \ com2 |
ComboBox |
connectbaudrate |
the baud rate of the serial port connection, which is important in serial communication |
button |
btnsend |
send button |
button |
btnconnect |
connection button, mainly used for program initialization |
button |
btnexit |
exit |
Step 3. Set pdudecoding. CS and justinio. CS mounts to the new project directory, open solution Resource Manager, right-click to add existing items, select two files, and then open the Class View. Is there two more classes in it, justinio and SMS. 3. If no, try again.
Figure 3: Class View after adding a class
Step 4. Reference The namespaceCodeOpen form1.cs (this is based on my computer. If you have changed it, please refer to your computer), add
Using justinio; Using SMS; Using system. IO; Using system. text; |
Step 5: In the smsformcsharp class, add two fields ss_port and SMS, which are respectively justinio and SMS objects, as shown below:
Step 6. Add the serial port initialization Code as follows:
/// <Summary> /// Initialize the serial port /// </Summary> Public bool initcom (string m_port, int m_baudrate) { Ss_port.portnum = m_port; // serial number Ss_port.baudrate = m_baudrate; // baud rate Ss_port.bytesize = 8; // data bit Ss_port.parity = 0 ;// Ss_port.stopbits = 1; // stop bit Ss_port.readtimeout = 1000; // read timeout Try { If (ss_port.opened) { Ss_port.close (); Ss_port.open (); } Else { Ss_port.open (); // open the serial port } Return true; } Catch (exception E) { MessageBox. Show ("error:" + E. Message ); Return false; } } |
Directly merge the above Code into your program, and ensure that it is added after the main function, press F5, debugging should be no problem, but there is no actual visible function above, just open the serial port.
Step 7. After opening the serial port, we should initialize the program, obtain the brand name, model, and SMS Center Number of the mobile phone, double-click the connection button, and paste the following code into the program:
/// <Summary> /// Initialize the code and obtain mobile phone information /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void btnconnect_click (Object sender, system. eventargs E) { Bool opened = initcom (connectport. selecteditem. tostring (), convert. toint32 (connectbaudrate. selecteditem. tostring (); // open and initialize the serial port Bool connected = false; If (opened) { Ss_port.write (encoding. ASCII. getbytes ("at + cgmi \ r"); // obtain the mobile phone brand String response = encoding. ASCII. getstring (ss_port.read (128 )); If (response. length> 0) { Connectstate. Text = response. substring (10, 7 ); Connected = true; } Else { Connectstate. Text = "unable to connect to the mobile phone "; Connected = false; } Ss_port.write (encoding. ASCII. getbytes ("at + cgmm \ r"); // obtain the mobile phone model Response = encoding. ASCII. getstring (ss_port.read (128 )); If (response. length> 0) { Connectstate. Text = connectstate. Text + "" + response. substring (10, 5) + "Connecting ......"; Connected = true; } Else { Connectstate. Text = "unable to connect to the mobile phone "; Connected = false; } Ss_port.write (encoding. ASCII. getbytes ("at + csca? \ R "); // obtain the SMS center number Response = encoding. ASCII. getstring (ss_port.read (128 )); If (response. length> 0) { Centernumber. Text = response. substring (20, 13 ); Connected = true; } Else { Connected = false; } If (connected = true) { Btnconnect. Enabled = false; Btnsend. Enabled = true; } Else { Btnconnect. Enabled = true; Btnsend. Enabled = false; } } } |
At this point, you can press F5 to compile and debug it. After making sure that your phone is connected to your computer, click the connect button to see if it is like mine, the phone model and SMS center number are displayed normally.
Figure 4. Program Interface after connection
Step 8: Check whether the above result is very close to successfully sending the text message.ArticleIt takes a lot of time for you to try again. Sorry.
Double-click the send button and paste the following code into the program.
/// // send a text message /// /// // private void btnsend_click (Object sender, system. eventargs e) {< br> string decodedsms = SMS. smsdecodedsms (centernumber. text, targetnumber. text, smscontent. text); byte [] Buf = encoding. ASCII. getbytes (string. format ("at + cmgs = {0} \ r", SMS. nlength); ss_port.write (BUF); strin G response = encoding. ASCII. getstring (ss_port.read (128); string sendstate = ""; If (response. length> 0 & response. endswith (">") {< br> ss_port.write (encoding. ASCII. getbytes (string. format ("{0} \ x01a", decodedsms); sendstate = "sent successfully! "; }< br> else {< br> sendstate =" failed to send "; }< P> string result = string. format ("{0}, {1}, {2}, \ n \ r", targetnumber. text, smscontent. text, sendstate); smsstate. text + = result; } |
Press F5! God, come on! You can send text messages now. Make sure that your phone can connect to your computer. Press connect, enter the target mobile phone number you want to send, and add the content you want to send to the content. Send it! Successful! The success is like this! Are you the same as mine?
Figure 5. Sent successfully
Remember to add and exit code. Double-click to exit and add the following code:
/// <Summary> /// Close the serial port and exit the program /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> Private void btnexit_click (Object sender, system. eventargs E) { Ss_port.close (); Application. Exit (); } |
A paragraph is reported here, and all functions are completed! However, as this is only a demo case, there are still a lot of issues that have not been considered, such as serial communication, in the actual operation can not be such an operation, should be processed with multithreading, a dedicated to reading the serial port, A dedicated serial port for writing. There are also a lot of Error-proof codes in the program that have not been added, and I hope some friends can add them and publish them. This is what I hope to see in this article. Please do not directly use this program in practice. We sincerely remind you!
I have finally finished writing, and I have relaxed a lot. I should have done it for a long time. For some personal reasons, I have not finished writing it in time, sorry, I hope you will continue to support me!
Debugging environment:
Windows 2000 Professional, Visual Studio. NET, Siemens 3508 mobile phone, Siemens dedicated data cable
Original: http://jly.blog.hexun.com/trackback.aspx? ArticleID = 3207223