日期:2014-05-19  浏览次数:20908 次

求serialport 短信收发实例(c#)
chiyongmin@126.com   发一份   谢谢   ,发的已经实现,最主要是接收短信部分..编码也有,只需要如何对接收缓冲区进行判断.

------解决方案--------------------
比较简单,你自己拽一个serialPort控件,设置好serialport控件的波特率一类的
双击控件 添加事件
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(500);
try
{
string data = this.serialPort1.ReadExisting();
str += data;
//MessageContent(str);
//if (str != " ")
//{
// MessageBox.Show(str);
//}
//str = " ";
MessageBox.Show(str);

if (str == "AT\r\n\n\r\nOK\r\n ")
MessageBox.Show(str);
if (str != " " && str.Contains( "AT+CMGS= ") && str.Contains( "OK "))
MessageBox.Show( "OK ");
if (str != " " && str.Contains( "+CMGR ") && str.Contains( "OK "))
MessageBox.Show(str);
if (str != " " && str.Contains( "AT+CMGL= ") && str.Contains( "OK "))
MessageBox.Show(str);
if (str != " " && str.Contains( "+CMTI ") && str.Contains( "SM "))
MessageBox.Show( "OK ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

做一个发送命令的方法
private void RecSetting(string conStr)
{
try
{
str = conStr;
this.serialPort1.WriteLine(str);
str = " ";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

用命令发短信就行了
private void btnConfirm_Click(object sender, EventArgs e)
{
//RecSetting( "AT+CMGD=1,3 " + Convert.ToChar(13) + Convert.ToChar(10));
}