【本文转自】http://hi.baidu.com/hexi1985/item/7919a9d67379a54cfa576841
最近在写一个上位机的监控程序用C#,如何实现串口接收数据,参考如下代码:
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();
}
}
}
}
加载中,请稍候......