-
大小: 38KB文件类型: .zip金币: 2下载: 0 次发布日期: 2021-06-08
- 语言: C#
- 标签: C# COM端口 serialPort
资源简介
C# 通过COM端口读取数据
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

代码片段和文件信息
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.Threading;
namespace COM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender EventArgs e)
{
initCom();
}
///
/// 初始化COM端口
///
private void initCom()
{
////////////初始化端口
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
//设置波特率为9600
serialPort1.BaudRate = 9600;
//设置端口号,这里使用COM1端口
serialPort1.PortName = “COM1“;
//设置 DataReceived 事件发生前内部输入缓冲区中的字节数为8
serialPort1.ReceivedBytesThreshold = 8;
//将事件处理方法添加到事件中去
textChanged += new UpdateTextEventHandler(XXX);
try
{
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show(“未能打开端口,请检查是否已经连接串口.\n“ + ex.Message);
}
}
///
/// 业务、DB等
///
private void XXX()
{
}
//定义委托
private delegate void UpdateTextEventHandler();
//定义事件
private event UpdateTextEventHandler textChanged;
///
/// 接收数据
///
///
///
private void serialPort1_DataReceived(object sender System.IO.Ports.SerialDataReceivedEventArgs e)
{
bool b = serialPort1.IsOpen;
if (!b)
{
return;
}
int bytes = serialPort1.BytesToRead;
byte[] receivedData = new byte[bytes];
Thread.Sleep(300);
serialPort1.Read(receivedData 0 bytes);
this.textBox1.Text = byteToHexStr(receivedData);
serialPort1.DiscardInBuffer();
this.Invoke(textChanged);
}
///
/// 十六进制转字符串
///
///
///
public static string byteToHexStr(byte[] bytes)
{
string returnStr = ““;
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString(“X2“);
}
}
return returnStr;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-09-18 10:03 COM\
目录 0 2012-09-18 09:56 COM\bin\
目录 0 2012-09-18 10:04 COM\bin\Debug\
文件 9728 2012-09-18 10:04 COM\bin\Debug\COM.exe
文件 22016 2012-09-18 10:04 COM\bin\Debug\COM.pdb
文件 14328 2012-09-18 10:02 COM\bin\Debug\COM.vshost.exe
文件 490 2007-07-21 01:33 COM\bin\Debug\COM.vshost.exe.manifest
文件 3717 2012-09-18 10:01 COM\COM.csproj
文件 895 2012-09-18 09:56 COM\COM.sln
文件 7168 2012-09-18 09:56 COM\COM.suo
文件 3036 2012-09-18 10:03 COM\Form1.cs
文件 2885 2012-09-18 09:59 COM\Form1.Designer.cs
文件 6013 2012-09-18 09:59 COM\Form1.resx
目录 0 2012-09-18 09:56 COM\obj\
目录 0 2012-09-18 10:04 COM\obj\Debug\
文件 376 2012-09-18 10:04 COM\obj\Debug\COM.csproj.FileListAbsolute.txt
文件 847 2012-09-18 10:04 COM\obj\Debug\COM.csproj.GenerateResource.Cache
文件 9728 2012-09-18 10:04 COM\obj\Debug\COM.exe
文件 180 2012-09-18 10:04 COM\obj\Debug\COM.Form1.resources
文件 22016 2012-09-18 10:04 COM\obj\Debug\COM.pdb
文件 180 2012-09-18 10:04 COM\obj\Debug\COM.Properties.Resources.resources
目录 0 2012-09-18 09:56 COM\obj\Debug\TempPE\
文件 484 2012-09-18 09:56 COM\Program.cs
目录 0 2012-09-18 09:56 COM\Properties\
文件 1362 2012-09-18 09:56 COM\Properties\AssemblyInfo.cs
文件 2856 2012-09-18 09:56 COM\Properties\Resources.Designer.cs
文件 5612 2012-09-18 09:56 COM\Properties\Resources.resx
文件 1088 2012-09-18 09:56 COM\Properties\Settings.Designer.cs
文件 249 2012-09-18 09:56 COM\Properties\Settings.settings
相关资源
- C# TIP文件生成和拆解
- C#解析HL7消息的库135797
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
评论
共有 条评论