随便写个用SerialPort操作串口的简单例子
2010-01-28 16:13
337 查看
/*随便写个用SerialPort操作串口的简单例子(待修正及优化)*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort("COM3");
delegate void AppendStringDelegate(string str);
public Form1()
{
InitializeComponent();
}
private void readData()
{
AppendStringDelegate appendStrMethod = new AppendStringDelegate(AppendString);
while (true)
{
if (sp.IsOpen)
{
int count = sp.BytesToRead;
byte[] bytes = new byte[count];
try
{
int DataLength = sp.Read(bytes, 0, bytes.Length);
if (DataLength > 0)
{
if (listBox1.InvokeRequired)
{
listBox1.Invoke(appendStrMethod, Encoding.UTF8.GetString(bytes));
}
}
Thread.Sleep(1000);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(readData));
thread.IsBackground = true;
thread.Start();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (!sp.IsOpen)
{
sp.Open();
}
sp.RtsEnable = true;
sp.Encoding = Encoding.GetEncoding("UTF-8");
sp.Write(this.textBox1.Text);
}
private void AppendString(string str)
{
listBox1.Items.Add(str);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sp.Close();
Application.ExitThread();
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SerialPort sp = new SerialPort("COM3");
delegate void AppendStringDelegate(string str);
public Form1()
{
InitializeComponent();
}
private void readData()
{
AppendStringDelegate appendStrMethod = new AppendStringDelegate(AppendString);
while (true)
{
if (sp.IsOpen)
{
int count = sp.BytesToRead;
byte[] bytes = new byte[count];
try
{
int DataLength = sp.Read(bytes, 0, bytes.Length);
if (DataLength > 0)
{
if (listBox1.InvokeRequired)
{
listBox1.Invoke(appendStrMethod, Encoding.UTF8.GetString(bytes));
}
}
Thread.Sleep(1000);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(readData));
thread.IsBackground = true;
thread.Start();
}
private void btnSend_Click(object sender, EventArgs e)
{
if (!sp.IsOpen)
{
sp.Open();
}
sp.RtsEnable = true;
sp.Encoding = Encoding.GetEncoding("UTF-8");
sp.Write(this.textBox1.Text);
}
private void AppendString(string str)
{
listBox1.Items.Add(str);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
sp.Close();
Application.ExitThread();
Application.Exit();
}
}
}
相关文章推荐
- C# 使用serialport 控件读写串口的大数据量解决方案 -- 以及与串口操作中常见的错误
- 简单的几个 T-SQL 操作 XML 的小例子
- nios中关于串口(uart)的一种非常简单的操作方式
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。(转帖)
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
- C#操作MySQL数据库的简单例子
- 使用jxl读写excel(jxl操作excel)的简单例子
- 随便:LINQ OVER DataSet和ADO.NET的简单例子
- C#串口serialPort操作
- C#操作MySQL数据库的简单例子
- Python操作json数据的一个简单例子
- pywinauto简单操作写字板的例子
- Go语言基础学习五-一些数值操作的简单例子
- 对PC某串口接收的外部数据进行处理操作的简单C程序 VC++6.0
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
- c#操作Access数据库的简单例子
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。 ——兔子党逍遥原创,转来分享学习
- C#操作MySQL数据库的简单例子
- java连接mysql与简单操作mysql的增删改查简单例子
- jxls操作Excel表的导出,一个简单的例子