基本信息
源码名称:C# 串口通信源码(常用的RS232通信范例)
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2016-07-27
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.IO.Ports; using System.Linq; using System.Windows.Forms; //使用C#自带serialPort控件进行串口编程94011.com.cn namespace serialport { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { //使用委托进行跨线程读取数据 Invoke (new EventHandler (delegate { txtreceived.Text = comport .ReadExisting (); } ) ); } private void Form1_Load(object sender, EventArgs e) { foreach (string com in System .IO .Ports .SerialPort.GetPortNames ()) //自动获取串行口名称 cmbPortName.Items.Add(com); cmbPortName.SelectedIndex = 0; } private void btnopen_Click(object sender, EventArgs e) { try { if (comport.IsOpen) comport.Close(); else { // Set the port's settings comport.BaudRate = int.Parse(cmbBaudRate.Text); comport.DataBits = int.Parse(cmbDataBits.Text); comport.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text); comport.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text); comport.PortName = cmbPortName.Text; // Open the port comport.Open(); } gbPortSettings.Enabled = !comport.IsOpen; txtsend.Enabled = btnsend.Enabled = comport.IsOpen; if (comport.IsOpen) btnopen.Text = "&C关闭端口"; else btnopen.Text = "&O打开端口"; if (comport.IsOpen) txtsend.Focus(); } catch (Exception er) { MessageBox .Show ("端口打开失败!" er.Message ,"提示");} } private void btnsend_Click(object sender, EventArgs e) { for (int i=0;i< txtsend .Lines .Count() ;i ) comport.WriteLine(txtsend.Lines [i]); } private void btnexit_Click(object sender, EventArgs e) { Close(); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://94011.info"); } } }