Send text messages via Asp.net SMS

Source: Internet
Author: User

Nowadays, mobile phones have become the main tool for mass communication. Phone-relatedProgramThis section describes how to use SMS cats to send and receive text messages through typical examples.

1. Solution Analysis

One way to send text messages is to send text messages using the SMS cat. In this example, the serial port SMS cat is used.

The so-called SMS cat is actually an industrial GSM modem that connects to a computer through a serial port and can be used to send and receive short messages through at Command Control. Currently, SMS cats that are widely used in China are all assembled with Siemens or Wavecom modules as the core, which is more stable and efficient than ordinary mobile phones.

SMS cat is a hardware device that uses a SIM card to send text messages. It is connected to a computer through a serial port or USB interface (depending on the device model. In the program, you can use the SMS cat to send or receive text messages. In this example, the serial port SMS is used. When purchasing the SMS cat, the SDK's SMS cat development kit will be included, which provides the function for operating the sms cat (encapsulated in the dllforvc. dll dynamic library ).

2. Implementation Process
The following uses a text message to send a text message, and click send text message to send a text message to a specified mobile phone number. When a new text message is sent, click the receive text message button to display the text message.

Procedure:

(1) create a new website. The default homepage is default. aspx.

(2) Add 6 textbox text boxes to the page, it is used to display the COM port, baud rate, machine number, authorization number of the SMS cat, the mobile phone number for receiving the text message, and the content of the text message to be sent.

(3) add two buttons on the page to send and receive SMS messages respectively.

(4) Main ProgramCodeAs follows.

First, create a class library, name it baisc, and then create a public class named GMs in this class library to obtain the dynamic library dllforvc that comes with SMS cats. DLL, and then compile the library into basic. DLL file, reference it in the project, the specific code is as follows.

Import the necessary namespace. The Code is as follows.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Call some functions in dllforvc. DLL to obtain various methods for calling on the default. aspx page. The specific code is as follows.

Namespace basic
{
Public class GMS
{
// Initialize the GSM Modem and connect it to the GSM Modem
// Parameter description:
// Device: identifies the communication port. If it is null, the system automatically detects the port.
// Baudrate: identifies the communication baud rate. If it is null, the system will automatically detect it.
// Initstring: identifies the initialization command, which is null.
// Charset: identifies the character set. It is null.
// Swhandshake: identifies whether the software handshakes are performed. Set this parameter to false.
// Sn: The authorization number that identifies the SMS cat. It must be filled in according to the actual situation.
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodeminitnew ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern bool gsmmodeminitnew (
String device,
String baudrate,
String initstring,
String charset,
Bool swhandshake,
String Sn );
// Obtain the new ID number of the SMS cat
// Parameter description:
// Device: communication port. If it is null, the system automatically checks the port.
// Baudrate: Communication baud rate. If it is null, the system will automatically detect it.
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemgetsninfonew ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern string gsmmodemgetsninfonew (string device, string baudrate );
// Obtain the current communication port
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemgetdevice ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern string gsmmodemgetdevice ();
// Get the current communication baud rate
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemgetbaudrate ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern string gsmmodemgetbaudrate ();
// Disconnect and release memory space
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemrelease ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern void gsmmodemrelease ();
// Get error message
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemgeterrormsg ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern string gsmmodemgeterrormsg ();
// Send Short Message
// Parameter description:
// Servicecenteraddress: identifies the SMS center number, which is null.
// Encodeval: Specifies the short message encoding format. If it is 8, it indicates the Chinese text message encoding.
// Text: Text message content.
// Textlen: the length of the text message.
// Phonenumber: The phone number used to receive text messages.
// Requeststatusreport: indicates the status report.
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemsmssend ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern bool gsmmodemsmssend (
String servicecenteraddress,
Int encodeval,
String text,
Int textlen,
String phonenumber,
Bool requeststatusreport );
// The string format returned for receiving short messages is: Mobile Phone Number | SMS content | mobile phone number | SMS content |
// Rd_opt is 1 and will not be processed after receiving the short message. 0 indicates that the received message will be deleted.
[Dllimport ("dllforvc. dll ",
Entrypoint = "gsmmodemsmsreadall ",
Charset = charset. ANSI,
Callingconvention = callingconvention. stdcall)]
Public static extern string gsmmodemsmsreadall (INT rd_opt );
}
}
On the default. aspx page, import the namespace using system. text. The specific code is as follows.
Using system. text;

When a page is loaded, call the gsmmodemgetsninfonew method, gsmmodemgetdevice method, and gsmmodemgetbaudrate method in the GMS class to display the machine number, COM port, and baud rate respectively. The specific code is as follows.

Protected void page_load (Object sender, eventargs E)
{
// Machine number
This.txt jqhm. Text = Basic. GMS. gsmmodemgetsninfonew (txtcom. Text, txtbtl. Text );
This.txt com. Text = Basic. GMS. gsmmodemgetdevice (); // COM1
This.txt BTL. Text = Basic. GMS. gsmmodemgetbaudrate (); // baud rate
}
After entering all the information, click send SMS. The program first connects to the device through the GMS modeminitnew method in the GMS class. If the connection is successful, the program sends the entered text message content to the specified mobile phone number through the gsmmodemsmssend method in the GMS class. If the connection fails, the program outputs the error information through the gsmmodemgeterrormsg method in the GMS class, the code in the Click Event of the send SMS button is as follows.

Protected void btnsend_click (Object sender, eventargs E)
{
If (txtsjhm. Text = "")
{
Page. registerstartupscript ("", "<SCRIPT> alert ('the mobile phone number cannot be blank! ') </SCRIPT> ");
Txtsjhm. Focus ();
Return;
}
If (txtcontent. Text = "")
{
Page. registerstartupscript ("", "<SCRIPT> alert ('The text message content cannot be blank! ') </SCRIPT> ");
Txtcontent. Focus ();
Return;
}
// Connect the device
If (basic. GMS. gsmmodeminitnew (txtcom. Text, txtbtl. Text, null, null, false, txtpower. Text) = false)
{
Page. registerstartupscript ("", "<SCRIPT> alert ('device connection failed! '"+ Basic. GMS. gsmmodemgeterrormsg () +"') </SCRIPT> ");
Return;
}
// Send SMS
If (basic. GMS. gsmmodemsmssend (null, 8, txtcontent. Text, encoding. Default. getbytecount (txtcontent. Text), txtsjhm. Text, false) = true)
Page. registerstartupscript ("", "<SCRIPT> alert ('message sent successfully! ') </SCRIPT> ");
Else
Page. registerstartupscript ("", "<SCRIPT> alert ('sms sending failed! '"+ Basic. GMS. gsmmodemgeterrormsg () +"') </SCRIPT> ");
}
When you click the receive text message button, the program reads the text message sent to the mobile phone card using the gsmmodemsmsreadall method in the GSM class. The code in the Click Event of the receive text message button is as follows.

Protected void btnresvice_click (Object sender, eventargs E)
{
// Connect the device
If (basic. GMS. gsmmodeminitnew (txtcom. Text, txtbtl. Text, null, null, false, txtpower. Text) = false)
{
Page. registerstartupscript ("", "<SCRIPT> alert ('Connection failed! '"+ Basic. GMS. gsmmodemgeterrormsg () +"') </SCRIPT> ");
Return;
}
// Receive SMS
Txtcontent. Text = Basic. GMS. gsmmodemsmsreadall (1 );
If (txtcontent. Text. Length <= 0)
{
Page. registerstartupscript ("", "<SCRIPT> alert ('no new information! ') </SCRIPT> ");
Return;
}
Txtsjhm. Text = txtcontent. Text. substring (0, 13 );
Txtcontent. Text = txtcontent. Text. substring (13, txtcontent. Text. Length-13 );
}

3. note:
because SMS is a serial communication device, you must submit SMS messages in a serial manner and wait for a response to submit the next message. Otherwise, the SMS will crash. If multiple threads concurrently operate the SMS cat, it will also cause the SMS cat to crash. Even for sending and receiving the same SMS cat, it must be a serial one or later, instead of two concurrent threads.

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.