串口如何接收数据,接收到又如何处理
来源:互联网 发布:连接db2数据库-4499 编辑:程序博客网 时间:2024/05/13 03:29
点击
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# SerialPort串口通信发送接收,处理接收数据完整
- C#对串口数据接收的处理
- 串口通信之数据接收处理1
- 串口设备数据的接收和处理
- 串口通信之数据接收处理1
- c#串口数据接收处理大纲
- mfc串口通过串口接收到的数据进行分离处理
- 串口接收中断进不去,如何解决
- PHP如何接收json数据
- 串口接收处理函数
- 串口接收处理思路
- javascript如何处理/接收URL中的Json数据?
- linux如何查看某个端口接收到的数据
- Fragment 跳转到 Activity 如何接收回来的数据
- 串口接收数据c#
- 串口接收数据c#
- 每天学一点flex(3):flex的调试的几个简单技巧
- WTL头文件中包含的类
- GDB调试精粹及使用实例
- C/C++代码风格摘录
- 获取浏览器插件方法
- 串口如何接收数据,接收到又如何处理
- 线程中 CloseHandle()函数的使用
- delphi图片写入SQL数据库
- CSS图片垂直居中方法整理集合 !
- delphi中Canvas(画布)的运用
- delphi函数大全
- Cookie特性
- 项目中的方法,工具和技术
- 《那些年啊,那些事——一个程序员的奋斗史》——19