C#串口实例 源代码

上传者: 25860565 | 上传时间: 2022-05-17 15:17:21 | 文件大小: 52KB | 文件类型: ZIP
namespace 串口实例 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSend_Click(object sender, EventArgs e) { //手动发送 SendMsg(); } private void SendMsg() { this.serialPort1.Write(this.txtSend.Text); } #region 控制输入数字 private void ComboBox_TextUpdate(object sender, EventArgs e) { ComboBox cbo = sender as ComboBox; int n; if (!int.TryParse(cbo.Text, out n)) { cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1); } else { if (n <= 0) { cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1); } } } private void txtTime_TextChanged(object sender, EventArgs e) { TextBox text = sender as TextBox; int n; if (!int.TryParse(text.Text, out n)) { text.Text = text.Text.Substring(0, text.Text.Length - 1); } else { if (n <= 0) { text.Text = text.Text.Substring(0, text.Text.Length - 1); } else { this.timer1.Interval = n; } } } #endregion /// /// 清空显示文本框 /// /// /// private void btnClear_Click(object sender, EventArgs e) { this.txtRecive.Clear(); } private void timer1_Tick(object sender, EventArgs e) { SendMsg(); } private void cbtSend_CheckedChanged(object sender, EventArgs e) { CheckBox check = sender as CheckBox; if (check.Checked) { this.timer1.Start(); } else { this.timer1.Stop(); } } /// /// 接收返回的数据 /// /// /// private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string a=""; if (this.cbkShow16.Checked) { //十六进制显示 a = this.serialPort1.ReadExisting(); } else { //字符串显示 byte[] by = new byte[this.serialPort1.BytesToRead]; this.serialPort1.Read(by, 0, this.serialPort1.BytesToRead); a = Encoding.ASCII.GetString(by); } this.txtRecive.AppendText(a); this.txtRecive.ScrollToCaret(); } private void Form1_Load(object sender, EventArgs e) { //加载 this.serialPort1.RtsEnable = true; button1_Click(this.btnOpen, null); } private void button1_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn.Text == "打开端口") { try { this.serialPort1.PortName = this.cboPortName.Text; this.serialPort1.BaudRate = Convert.ToInt32(this.cboRaudRate.Text); this.serialPort1.DataBits = Convert.ToInt32(this.cboDataBits.Text); this.serialPort1.ReceivedBytesThreshold = Convert.ToInt32(this.cboReceivedBytesThreshold.Text); this.serialPort1.Open(); btn.Text = "关闭端口"; } catch { MessageBox.Show("打开端口失败,请检查端口是否被占用."); } } else { this.serialPort1.Close(); btn.Text = "打开端口"; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { this.serialPort1.Close(); } catch { } } } }

文件下载

资源详情

[{"title":"( 25 个子文件 52KB ) C#串口实例 源代码","children":[{"title":"串口实例","children":[{"title":"串口实例.suo <span style='color:#111;'> 19.50KB </span>","children":null,"spread":false},{"title":"串口实例.sln <span style='color:#111;'> 878B </span>","children":null,"spread":false},{"title":"串口实例","children":[{"title":"bin","children":[{"title":"Debug","children":[{"title":"串口实例.pdb <span style='color:#111;'> 27.50KB </span>","children":null,"spread":false},{"title":"串口实例.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"串口实例.vshost.exe <span style='color:#111;'> 11.33KB </span>","children":null,"spread":false},{"title":"串口实例.exe <span style='color:#111;'> 14.00KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Form1.Designer.cs <span style='color:#111;'> 13.58KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 493B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"x86","children":[{"title":"Debug","children":[{"title":"串口实例.pdb <span style='color:#111;'> 27.50KB </span>","children":null,"spread":false},{"title":"串口实例.Form1.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"GenerateResource.write.1.tlog <span style='color:#111;'> 678B </span>","children":null,"spread":false},{"title":"串口实例.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.17KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 2.10KB </span>","children":null,"spread":false},{"title":"GenerateResource.read.1.tlog <span style='color:#111;'> 296B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"串口实例.csproj.FileListAbsolute.txt <span style='color:#111;'> 1015B </span>","children":null,"spread":false},{"title":"串口实例.exe <span style='color:#111;'> 14.00KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"串口实例.csproj <span style='color:#111;'> 3.60KB </span>","children":null,"spread":false},{"title":"Form1.cs <span style='color:#111;'> 4.80KB </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 6.07KB </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.33KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.81KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明