使用C#应用程序从移动设备发送和接收SMS [英] Send and Receive SMS from Mobile to Mobile using C# Application
本文介绍了使用C#应用程序从移动设备发送和接收SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
http://imageshack.com/a/img22/7094/iw4j.jpg
我想使用C#应用程序发送和接收从手机到手机的短信。移动必须使用USB端口连接到计算机。
如果有人可以帮助我,请帮助我并向我发送示例项目。
http://imageshack.com/a/img22/7094/iw4j.jpg
I want to send and receive SMS from mobile to mobile using C# application. Mobile must connect to computer using USB port.
If any one can help me please please help me and send me sample project.
推荐答案
<pre><pre lang="css">public class ShortMessage
{
#region Private Variables
private string index;
private string status;
private string sender;
private string alphabet;
private string sent;
private string message;
#endregion
#region Public Properties
public string Index
{
get { return index; }
set { index = value; }
}
public string Status
{
get { return status; }
set { status = value; }
}
public string Sender
{
get { return sender; }
set { sender = value; }
}
public string Alphabet
{
get { return alphabet; }
set { alphabet = value; }
}
public string Sent
{
get { return sent; }
set { sent = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
#endregion
}
public class ShortMessageCollection : List<ShortMessage>
{
}
#region Read SMS
public AutoResetEvent receiveNow;
public ShortMessageCollection ReadSMS(SerialPort port)
{
// Set up the phone and read the messages
ShortMessageCollection messages = null;
try
{
#region Execute Command
// Check connection
ExecCommand(port, "AT", 300, "No phone connected");
// Use message format "Text mode"
ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");
// Use character set "PCCP437"
ExecCommand(port, "AT+CSCS=\"PCCP437\"", 300, "Failed to set character set.");
// Select SIM storage
ExecCommand(port, "AT+CPMS=\"SM\"", 300, "Failed to select message storage.");
// Read the messages
string input = ExecCommand(port, "AT+CMGL=\"ALL\"", 5000, "Failed to read the messages.");
#endregion
#region Parse messages
messages = ParseMessages(input);
#endregion
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
if (messages != null)
return messages;
else
return null;
}
public ShortMessageCollection ParseMessages(string input)
{
ShortMessageCollection messages = new ShortMessageCollection();
try
{
Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");
Match m = r.Match(input);
while (m.Success)
{
ShortMessage msg = new ShortMessage();
//msg.Index = int.Parse(m.Groups[1].Value);
msg.Index = m.Groups[1].Value;
msg.Status = m.Groups[2].Value;
msg.Sender = m.Groups[3].Value;
msg.Alphabet = m.Groups[4].Value;
msg.Sent = m.Groups[5].Value;
msg.Message = m.Groups[6].Value;
messages.Add(msg);
m = m.NextMatch();
}
}
catch (Exception ex)
{
throw ex;
}
return messages;
}
#endregion
这篇关于使用C#应用程序从移动设备发送和接收SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文