串口如何接收数据,接收到又如何处理
2011-08-06 13:57
786 查看
点击
http://topic.csdn.net/u/20090728/09/1E9D9C81-FC4A-4025-B350-A4D44E28E566.html
using System.IO.Ports;using System.Threading;
http://topic.csdn.net/u/20090728/09/1E9D9C81-FC4A-4025-B350-A4D44E28E566.html
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();}}}}
相关文章推荐
- 串口设备数据的接收和处理
- 串口通信——接收串口数据并处理(C语言)
- springmvc使用实体类接收表单提交数据中含有String类型对应Date类型的不匹配报错400时如何处理
- mfc串口通过串口接收到的数据进行分离处理
- STM32之USART串口接收数据处理
- 单片机处理用串口接收频次较高的数据方式
- 串口处理数据(接收、解析、回复、框架)
- boostrap table接收到后台返回的数据格式不一致 如何处理
- C#对串口数据接收的处理
- javascript如何处理/接收URL中的Json数据?
- 对PC某串口接收的外部数据进行处理操作的简单C程序 VC++6.0
- 如何清空串口接收数据显示框(EDIT控件)的数据
- c#中,确保数据接收完整的 串口处理程序
- C#中请问如何在串口通讯中接收输出HEX格式数据
- verilog串口接收多个数据进行处理的实现方法
- 如何处理串口通讯数据重合与分批到达的问题 2
- 遇到的问题(一):智能家居中WIFI模块设备在接收底层家具设备串口数据时由于断帧而出现的指令丢失的情况处理
- STM32之USART串口接收数据处理
- c# 串口所有数据接收 到串口的数据全部处理
- Qt从零开始制作串口调试助手-(第十章、串口接收数据处理)-Creator_Ly