电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

C#的串口通信源程序

C#的串口通信源程序_第1页
1/17
C#的串口通信源程序_第2页
2/17
C#的串口通信源程序_第3页
3/17
基于单片机串口通信的上位机和下位机首先亮出C#的源程序吧。主要界面:只是作为简单的运用,可以扩展的。源代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO.Ports;using System.Timers;namespace 单片机功能控制{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort sp = new SerialPort(); private void button1_Click(object sender, EventArgs e) { String str1 = comboBox1.Text;//串口号 String str2 = comboBox2.Text;//波特率 String str3 = comboBox3.Text;//校验位 String str4 = comboBox5.Text;//停止位 String str5 = comboBox4.Text;//数据位 Int32 int2 = Convert.ToInt32(str2);//将字符串转为整型 Int32 int5 = Convert.ToInt32(str5);//将字符串转为整型 groupBox3.Enabled = true;//LED控制界面变可选 try { if (button1.Text == "打开串口") { if (str1 == null) { MessageBox.Show("请先选择串口!", "Error"); return; } sp.Close(); sp = new SerialPort(); sp.PortName = comboBox1.Text;//串口编号 sp.BaudRate = int2;//波特率 switch (str4)//停止位 { case "1": sp.StopBits = StopBits.One; break; case "1.5": sp.StopBits = StopBits.OnePointFive; break; case "2": sp.StopBits = StopBits.Two; break; default: MessageBox.Show("Error:参数不正确", "Error"); break; } switch (str3) { case "NONE": sp.Parity = Parity.None; break; case "ODD": sp.Parity = Parity.Odd; break; case "EVEN": sp.Parity = Parity.Even; break; default: MessageBox.Show("Error:参数不正确", "Error"); break; } sp.DataBits = int5;//数据位 sp.Parity = Parity.Even;//设置串口属性 sp.Open();//打开串口 button1.Text = "关闭串口"; textBox1.Text = Convert.ToString(sp.PortName) + "已开启!"; } else { sp.Close(); button1.Text = "打开串口"; groupBox3.Enabled = false;//LED控制界面变灰色 textBox1.Text = Convert.ToString(sp.PortName)...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

C#的串口通信源程序

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部