串口接收数据实例

串口数据接收与处理
本文介绍了一个使用C#实现的串口数据接收项目。该项目能够实时接收串口数据,并将接收到的数据按指定格式保存到本地文件中。文中详细展示了如何通过线程管理和异常处理确保数据接收的稳定性和准确性。
[size=medium][color=darkblue]今天我的串口接收数据的项目终于做完了,拿出来共享一下~~

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();
}
}


}
}
[/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值