深圳市未来时代科技有限公司
标题: 利用串口实现短信发送功能的一个国外程序 [打印本页]
作者: admin 时间: 2014-3-3 17:30
标题: 利用串口实现短信发送功能的一个国外程序
public SerialPort OpenPort(string p_strPortName,
int p_uBaudRate, int p_uDataBits,
int p_uReadTimeout, int p_uWriteTimeout)
{
receiveNow = new AutoResetEvent(false);
SerialPort port = new SerialPort();
try
{
port.PortName =p_strPortName; //COM1
port.BaudRate = p_uBaudRate; //9600
port.DataBits = p_uDataBits; //8
port.StopBits =StopBits.One; //1
port.Parity = Parity.None; //None
port.ReadTimeout =p_uReadTimeout; //300
port.WriteTimeout = p_uWriteTimeout; //300
port.Encoding =Encoding.GetEncoding("iso-8859-1");
port.DataReceived += newSerialDataReceivedEventHandler
(port_DataReceived);
port.Open();
port.DtrEnable = true;
port.RtsEnable = true;
}
catch (Exception ex)
{
throw ex;
}
return port;
}
Send SMS
In the second tab, you can send SMS:
public bool sendMsg(SerialPort port, string PhoneNo, string Message)
{
bool isSend = false;
try
{
string recievedData =ExecCommand(port,"AT", 300, "No phone connected");
recievedData =ExecCommand(port,"AT+CMGF=1", 300,
"Failed to setmessage format.");
String command = "AT+CMGS="" + PhoneNo + """;
recievedData =ExecCommand(port,command, 300,
"Failed toaccept phoneNo");
command = Message + char.ConvertFromUtf32(26) + " ";
recievedData =ExecCommand(port,command, 3000,
"Failed to sendmessage"); //3 seconds if(recievedData.EndsWith(" OK "))
{
isSend = true;
}
else if (recievedData.Contains("ERROR"))
{
isSend = false;
}
return isSend;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
Read SMS
In the third tab, you can read SMS:
public ShortMessageCollection ReadSMS(SerialPort port)
{
// Set up the phoneand 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 setmessage format.");
// Use character set"PCCP437"
ExecCommand(port,"AT+CSCS="PCCP437"", 300,
"Failed to setcharacter set.");
// Select SIM storage
ExecCommand(port,"AT+CPMS="SM"", 300,
"Failed toselect message storage.");
// Read the messages
string input = ExecCommand(port,"AT+CMGL="ALL"", 5000,
"Failed to readthe 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;
}
Delete SMS
In the fourth and last tab, you can count the number of SMS and delete SMSas well.
public bool DeleteMsg(SerialPort port , string p_strCommand)
{
bool isDeleted = false;
try
{
#region Execute Command
string recievedData =ExecCommand(port,"AT", 300, "No phone connected");
recievedData =ExecCommand(port,"AT+CMGF=1", 300,
"Failed to setmessage format.");
String command = p_strCommand;
recievedData =ExecCommand(port,command, 300, "Failed todelete message");
#endregion
if (recievedData.EndsWith(" OK "))
{
isDeleted = true;
}
if (recievedData.Contains("ERROR"))
{
isDeleted = false;
}
return isDeleted;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
欢迎光临 深圳市未来时代科技有限公司 (http://inextera.com/) |
Powered by Discuz! X3.1 |