本文主要是介绍串口接收数据c#,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自:http://blog.sina.com.cn/s/blog_425d77d70100j9jv.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,则解除挂起线程