串口如何接收数据,接收到又如何处理? 串口如何接收数据,接收到又如何处理?求高人发个例子(最好附带协议),收到就散分啊,email:[email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System.IO.Ports;using System.Threading;using System.IO;namespace SerialPortDemo{ public partial class Form1 : Form { bool isStar = false; Thread threadReceive = null; SerialPort serialPort = null; int i = 0; int k = 0; public Form1() { InitializeComponent(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 2; serialPort = new SerialPort(); if (serialPort.IsOpen) serialPort.Close(); //获得参数 serialPort.PortName = comboBox1.SelectedItem.ToString(); serialPort.BaudRate = int.Parse(comboBox2.SelectedItem.ToString()); serialPort.Open(); ThreadStart method = new ThreadStart(ReceiveData);//在线程上执行此方法 threadReceive = new Thread(new ThreadStart(method)); } private void button1_Click(object sender, EventArgs e) { try { if (button1.Text == "接收数据") { if (!isStar)//如果isStar等于true,则启动线程 { threadReceive.Start(); this.toolStripStatusLabel1.Text = "正在接收数据......"; } else { threadReceive.Resume();//如果isStar等于false,则解除挂起线程 this.toolStripStatusLabel1.Text = "暂停接收..."; } button1.Text = "停止接收"; } else { threadReceive.Suspend();//如果是"停止接收",则挂起线程 button1.Text = "接收数据"; this.toolStripStatusLabel1.Text = "暂停接收..."; } } catch (Exception ex) { MessageBox.Show(ex.Message); serialPort.Close(); } } //不断循环接收数据 private void ReceiveData() { while (true) { Thread.Sleep(500); this.isStar = true; this.SynReceiveData(); } } //通过串口取数据 private void SynReceiveData() { MessageBox.Show("接收数据" + k); k++; string inputData = serialPort.ReadExisting(); try { if (inputData != string.Empty) { if (inputData.Trim().Length == 45) { SetText(inputData); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } //分隔字符串并保存在指定目录下 private void SetText(string str) { string aa = str; StreamWriter sw = null; if (aa != string.Empty) { string aaa = aa.Insert(13, ","); aaa = aaa.Insert(32, ","); String fileName = "C:\\GAJ_PUB\\kaoqin"; if (Directory.Exists(fileName) == false)//如果目录不存在 { Directory.CreateDirectory(fileName);//创建目录 sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");//创建文本 } else { sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt"); } sw.WriteLine(aaa); sw.Flush(); sw.Close(); i++; MessageBox.Show("文件已存入指定目录下!!"); } } private void button2_Click(object sender, EventArgs e) { if (threadReceive.ThreadState==ThreadState.Running)//当前状态为启动线程 { threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Unstarted) { this.Close(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (threadReceive.ThreadState == ThreadState.Running)//当前状态为启动线程 { threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程 { threadReceive.Resume(); threadReceive.Abort(); this.Close(); } } }} http://blog.csdn.net/zggxyx2004/archive/2008/10/04/3015761.aspx 我觉得还是自己动手比较好。串口很简单的。初始化(端口号,波特率奇偶校验、数据位,等参数),创建DataReceived事件。然后读懂你要连接的设备的串口通讯协议,当你向你的串口设备发送(write)一个命令时,它会触发一个dataReceived事件,然后读串口(read)就行了。大多数通讯的问题,都不是卡壳在端口上,都是对设备说明书(通讯协议)读的不够详细。 配置SqlDataSource时,提示“未将对象引用设置到对象的实例” 枚举网上邻居共享文件夹的子文件夹问题 我有一套真人荷官的博彩软件,但有点问题,希望有人能帮助一下,定现金酬谢 想学ASP.NET不知道看什么书好。 请问DataGridView 中如何显示详细的时间 关于DataSet.Table[0].Row的问题,请大侠执教! 关于向二维数组写值问题 c#读变频器参数 NHibernate报错No persister for实体名 水晶报表的一个超难问题! vs2005的问题,急问!! 数据绑定是,如何指定datagridview的cell格式?
using System.Threading;
using System.IO;namespace SerialPortDemo
{
public partial class Form1 : Form
{
bool isStar = false;
Thread threadReceive = null;
SerialPort serialPort = null;
int i = 0;
int k = 0; public Form1()
{
InitializeComponent();
this.comboBox1.SelectedIndex = 0;
this.comboBox2.SelectedIndex = 2; serialPort = new SerialPort();
if (serialPort.IsOpen)
serialPort.Close();
//获得参数
serialPort.PortName = comboBox1.SelectedItem.ToString();
serialPort.BaudRate = int.Parse(comboBox2.SelectedItem.ToString());
serialPort.Open(); ThreadStart method = new ThreadStart(ReceiveData);//在线程上执行此方法
threadReceive = new Thread(new ThreadStart(method));
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (button1.Text == "接收数据")
{
if (!isStar)//如果isStar等于true,则启动线程
{
threadReceive.Start();
this.toolStripStatusLabel1.Text = "正在接收数据......";
}
else
{
threadReceive.Resume();//如果isStar等于false,则解除挂起线程
this.toolStripStatusLabel1.Text = "暂停接收...";
}
button1.Text = "停止接收";
}
else
{ threadReceive.Suspend();//如果是"停止接收",则挂起线程
button1.Text = "接收数据";
this.toolStripStatusLabel1.Text = "暂停接收...";
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
serialPort.Close(); } }
//不断循环接收数据
private void ReceiveData()
{
while (true)
{
Thread.Sleep(500);
this.isStar = true;
this.SynReceiveData();
}
} //通过串口取数据
private void SynReceiveData()
{
MessageBox.Show("接收数据" + k);
k++;
string inputData = serialPort.ReadExisting();
try
{
if (inputData != string.Empty)
{
if (inputData.Trim().Length == 45)
{
SetText(inputData);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} } //分隔字符串并保存在指定目录下
private void SetText(string str)
{
string aa = str;
StreamWriter sw = null; if (aa != string.Empty)
{
string aaa = aa.Insert(13, ",");
aaa = aaa.Insert(32, ","); String fileName = "C:\\GAJ_PUB\\kaoqin";
if (Directory.Exists(fileName) == false)//如果目录不存在
{
Directory.CreateDirectory(fileName);//创建目录
sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");//创建文本
}
else
{
sw = File.CreateText("C:\\GAJ_PUB\\kaoqin\\person" + i + ".txt");
}
sw.WriteLine(aaa); sw.Flush(); sw.Close();
i++;
MessageBox.Show("文件已存入指定目录下!!");
} } private void button2_Click(object sender, EventArgs e)
{
if (threadReceive.ThreadState==ThreadState.Running)//当前状态为启动线程
{
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Unstarted)
{
this.Close();
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (threadReceive.ThreadState == ThreadState.Running)//当前状态为启动线程
{
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.Suspended)//当前状态为挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
else if (threadReceive.ThreadState == ThreadState.SuspendRequested)//当前状态为正在挂起线程
{
threadReceive.Resume();
threadReceive.Abort();
this.Close();
}
}
}
}
串口很简单的。
初始化(端口号,波特率奇偶校验、数据位,等参数),创建DataReceived事件。
然后读懂你要连接的设备的串口通讯协议,
当你向你的串口设备发送(write)一个命令时,它会触发一个dataReceived事件,然后读串口(read)就行了。大多数通讯的问题,都不是卡壳在端口上,都是对设备说明书(通讯协议)读的不够详细。