【SerialPort】控件的使用实例
来源:互联网 发布:淘宝直通车排名规则 编辑:程序博客网 时间:2024/05/22 18:22
以下是【SerialPort】控件的使用实例,发送端和接收端代码除了端口号(例:“COM9”)不同,其他都一样:
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;namespace Test_Serialport_串口控件_{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1; string strCom = "COM9"; private delegate void ShowDelegate(string strshow); private void Form1_Load(object sender, EventArgs e) { //我现在用的COM1端口,按需要可改成COM2,COM3 InitCOM(strCom); OpenPort(); //this.txtReceive.InvokeRequired = true; } //初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型 public void InitCOM(string PortName) { port1 = new SerialPort(PortName); port1.BaudRate = 9600;//波特率 port1.Parity = Parity.None;//无奇偶校验位 port1.StopBits = StopBits.Two;//两个停止位 port1.Handshake = Handshake.RequestToSend;//控制协议 port1.ReceivedBytesThreshold = 1;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数 port1.DataReceived += new SerialDataReceivedEventHandler(port1_DataReceived);//DataReceived事件委托 } //DataReceived事件委托方法 private void port1_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { StringBuilder currentline = new StringBuilder(); //循环接收数据 while (port1.BytesToRead > 0) { char ch = (char)port1.ReadByte(); currentline.Append(ch); } //在这里对接收到的数据进行处理 ShowTxt(currentline.ToString()); //this.txtReceive.Text = currentline.ToString(); // currentline = new StringBuilder(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } } public void ShowTxt(string strShow) { if (this.txtReceive.InvokeRequired) { // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以 this.txtReceive.Invoke(new ShowDelegate(ShowTxt), strShow); } else { this.txtReceive.Text = strShow; } } //打开串口的方法 public void OpenPort() { try { port1.Open(); if (port1.IsOpen) { Console.WriteLine("the port is opened!"); this.labStat.Text = strCom + " 已连接!"; } else { Console.WriteLine("failure to open the port!"); this.labStat.Text = "连接失败!"; } } catch { } } //关闭串口的方法 public void ClosePort() { port1.Close(); if (!port1.IsOpen) { Console.WriteLine("the port is already closed!"); } } //向串口发送数据 public void SendCommand(string CommandString) { byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString); port1.Write(WriteBuffer, 0, WriteBuffer.Length); } //调用实例 private void btnSend_Click(object sender, EventArgs e) { string str = this.txtSend.Text; SendCommand(str); } private void timer1_Tick(object sender, EventArgs e) { labTime.Text = "当前时间是:" + DateTime.Now.ToLongTimeString(); } }}
0 0
- 【SerialPort】控件的使用实例
- VS 2005 中SerialPort控件的使用
- 浅谈SerialPort控件的使用(C#)
- SerialPort 控件使用
- vb.net中serialport控件的一些使用经验
- vb.net中serialport控件的一些使用经验
- vb.net中serialport控件的一些使用经验 .
- 使用.net的SerialPort串口监控控件接收串口数据
- C#中SerialPort串口控件使用之字符的接收
- C# serialport控件的一些理解
- C#自带的serialPort串口控件
- VisualStudio2005中serialPort控件访问串口实例[转载]
- 关于serialPort控件的使用跟INI文件的创建,读取,写入方法
- PC和单片机通信(一)---使用SerialPort控件
- PC和单片机通信(二)---使用SerialPort控件
- 使用SerialPort传输图片文件的实验
- serialport 控件 串口通讯
- C# 使用serialport 控件读写串口的大数据量解决方案 -- 以及与串口操作中常见的错误
- IE访问页面时自动切换到兼容模式的问题
- android edittext 去边框
- ~这些年,我们一起学过的Java~04~接口与抽象类
- 冠心病支架手术后谁能为冠心病人撑起一片天?
- .Net 调用SAP RFC接口来读取数据实战纪实
- 【SerialPort】控件的使用实例
- storm的bolt实现阶段统计
- 奇怪的NVL2问题,提示NOT DECLARE
- 使用Devexpress ChartControl中的Pie图
- log4j详解
- CSS精灵技术
- linux 通过程序名杀死进程
- 深入理解计算机系统之旅(三)程序的机器级表示
- mysql中char与varchar的区别分析