版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 基于单片机串口通信的上位机和下位机首先亮出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
2、: 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;/
3、数据位 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
4、 = 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:参数不正确", "Er
5、ror"); 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.
6、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) + "已关闭!" catch
7、(Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void Form1_Load(object sender, EventArgs e) /初始化 textBox1.Text = "欢迎使用简易的串口助手!" groupBox3.Enabled = false;/LED控制界面变灰色 groupBox6.Enabled = false; groupBox7.Enabled = false; groupBox8.Enabled
8、= false; button3.Enabled = false; button6.Enabled = false; timer1.Start(); try foreach (string com in System.IO.Ports.SerialPort.GetPortNames() /自动获取串行口名称 boBox1.Items.Add(com); /默认设置 comboBox1.SelectedIndex = 0;/选择第一个com口 comboBox2.SelectedIndex = 4;/波特率4800 comboBox3.SelectedIndex = 0;/校验位NONE com
9、boBox4.SelectedIndex = 0;/停止位为1 comboBox5.SelectedIndex = 0;/数据位为8 catch MessageBox.Show("找不到通讯端口!", "串口调试助手"); private void timer1_Tick(object sender, EventArgs e) label6.Text = DateTime.Now.ToString(); private void button2_Click(object sender, EventArgs e) try if (button2.Text
10、= "开启") groupBox6.Enabled = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; c
11、heckBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; button3.Enabled = true; textBox2.Text = String.Empty; button2.Text = "关闭" else groupBox6.Enabled = false; button3.Enabled = false; button2.Text = "开启" textBox2.Text = String.Empty; catch (Exception er
12、) MessageBox.Show("Error:" + er.Message, "Error"); return; private void button3_Click(object sender, EventArgs e) groupBox6.Enabled = true; label7.Text = "已发送" if (textBox2.Text = "") MessageBox.Show("发送失败,请选择发送的数据!"); else sp.WriteLine(textBox2.Text
13、);/往串口写数据 private void checkBox1_CheckedChanged(object sender, EventArgs e) try if (checkBox1.Checked) checkBox1.Checked = true; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8
14、.Checked = false; label7.Text = "准备发送" textBox2.Text = "1" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox2_CheckedChanged(object sender, EventArgs e) try if (checkBox2.Checked) checkBox1.Checked = false; che
15、ckBox2.Checked = true; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; label7.Text = "准备发送" textBox2.Text = "2" radioButton1.Checked = false; radioButton2.Checked = f
16、alse; radioButton3.Checked = false; radioButton4.Checked = false; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox3_CheckedChanged(object sender, EventArgs e) try if (checkBox3.Checked) checkBox1.Checked = false; checkBox2.Checke
17、d = false; checkBox3.Checked = true; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.T
18、ext = "准备发送" textBox2.Text = "3" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox4_CheckedChanged(object sender, EventArgs e) try if (checkBox4.Checked) checkBox1.Checked = false; checkBox2.Checked = false; ch
19、eckBox3.Checked = false; checkBox4.Checked = true; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "准
20、备发送" textBox2.Text = "4" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox5_CheckedChanged(object sender, EventArgs e) try if (checkBox5.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Check
21、ed = false; checkBox4.Checked = false; checkBox5.Checked = true; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "准备发送" tex
22、tBox2.Text = "5" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox6_CheckedChanged(object sender, EventArgs e) try if (checkBox6.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; c
23、heckBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = true; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "准备发送" textBox2.Text =
24、"6" catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox7_CheckedChanged(object sender, EventArgs e) try if (checkBox7.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Chec
25、ked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = true; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "准备发送" textBox2.Text = "7"
26、 catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void checkBox8_CheckedChanged(object sender, EventArgs e) try if (checkBox8.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false;
27、checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = "准备发送" textBox2.Text = "8" catch (Excep
28、tion er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void button5_Click(object sender, EventArgs e) try if (button5.Text = "开启") radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false;
29、checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; groupBox7.Enabled = true; button6.Enabled = true; textBox2.Text = String.Empty; butt
30、on5.Text = "关闭" else groupBox7.Enabled = false; button6.Enabled = false; button5.Text = "开启" textBox2.Text = String.Empty; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void button6_Click(object sender, EventArgs e) labe
31、l7.Text = "已发送" if (textBox2.Text = "") MessageBox.Show("发送失败。请选择发送的数据!"); else sp.WriteLine(textBox2.Text);/往串口写数据 private void radioButton1_CheckedChanged(object sender, EventArgs e) try if (radioButton1.Checked) label7.Text = "准备发送" textBox2.Text = "9&
32、quot; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Er
33、ror"); return; private void radioButton2_CheckedChanged(object sender, EventArgs e) try if (radioButton2.Checked) label7.Text = "准备发送" textBox2.Text = "a" checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Ch
34、ecked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; catch (Exception er) MessageBox.Show("Error:" + er.Message, "Error"); return; private void radioButton3_CheckedChanged(object sender, EventArgs e) try if (radioButton3.Checked) label7.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人员流动与内部调岗制度
- 成功的八项伟大心理法则
- 部编版五年级语文下册第二单元各类阅读真题(含小古文、非连续性文本等)名师解析连载
- 2024年长春客运丛业资格证考试
- 2024年新疆客运资格证考试题目下载什么软件
- 2024年银川客车考试题库
- 2024年福州客运资格从业证考试
- 2024年湖北客车驾驶员考试试题及答案
- 2024年铜陵资格证客运题库
- 英语-浙江省湖州、衢州、丽水2024年11月三地市高三教学质量检测试卷试题和答案
- 广东省深圳市2023-2024学年高一上学期生物期中试卷(含答案)
- 第七章 立体几何与空间向量综合测试卷(新高考专用)(学生版) 2025年高考数学一轮复习专练(新高考专用)
- 大学美育(同济大学版)学习通超星期末考试答案章节答案2024年
- 2024年2024年离婚协议书模板
- 中国急性缺血性卒中诊治指南(2023版)
- 福建省残疾人岗位精英职业技能竞赛(美甲师)参考试题及答案
- 航空器系统与动力装置学习通超星期末考试答案章节答案2024年
- 中考英语过去将来时趣味讲解动态课件(43张课件)
- 过敏性休克完整版本
- 08D800-8民用建筑电气设计与施工防雷与接地
- 科学的体育锻炼课件(图文)
评论
0/150
提交评论