比较简单,你自己拽一个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));
}
双击控件 添加事件
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));
}
下面是我收集的资料!1、在C#中使用SerialPort类实现串口通信 遇到多线程问题
http://blog.21ic.com/user1/1949/archives/2010/66360.html
2、C#基于事件驱动的多串口多线程串口通讯软件架构设计
http://hi.baidu.com/rambochow/blog/item/b662cfc417887da58326ac31.html
3、c#编写串口通讯代码 多线程实现
http://download.chinaprj.cn/detail/iDbsTbrr
4、[转载]C#中串口通信编程
http://blog.pfan.cn/sword2008/38218.html
5、C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
http://blog.csdn.net/wuyazhe/archive/2010/05/17/5598945.aspx
6、C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?
http://blog.csdn.net/wuyazhe/archive/2010/05/19/5606276.aspx
7、C# 串口操作系列(3) -- 协议篇,二进制协议数据解析
http://blog.csdn.net/wuyazhe/archive/2010/05/27/5627253.aspx
8、关于SerialPort写串口程序的问题
http://topic.csdn.net/u/20100607/15/08e0264a-ebcd-4864-b56b-c3d149305de0.html?10845
9、SerialPort编程(C#)
http://libinguest.blog.163.com/blog/static/4534208200811252208704/