《C#的串口通信源程序(共17页).doc》由会员分享,可在线阅读,更多相关《C#的串口通信源程序(共17页).doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
2、 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 = com
3、boBox4.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;/串口编号
4、 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 OD
5、D: 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(); button
6、1.Text = 打开串口; groupBox3.Enabled = false;/LED控制界面变灰色 textBox1.Text = Convert.ToString(sp.PortName) + 已关闭!; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void Form1_Load(object sender, EventArgs e) /初始化 textBox1.Text = 欢迎使用简易的串口助手!; groupBox3.Enabled = false;/LED控制
7、界面变灰色 groupBox6.Enabled = false; groupBox7.Enabled = false; groupBox8.Enabled = 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口
8、 comboBox2.SelectedIndex = 4;/波特率4800 comboBox3.SelectedIndex = 0;/校验位NONE comboBox4.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_C
9、lick(object sender, EventArgs e) try if (button2.Text = 开启) 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.Che
10、cked = false; checkBox5.Checked = false; checkBox6.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;
11、 catch (Exception er) 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);/往串口写数据 private void checkBox1_Checke
12、dChanged(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.Checked = false; label7.Text = 准备发送; t
13、extBox2.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; checkBox2.Checked = true; checkBox3.Checked = false; checkBox4.Checked = false; checkBox
14、5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; label7.Text = 准备发送; textBox2.Text = 2; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; catch (Exception er) MessageBox.Show(Erro
15、r: + er.Message, Error); return; private void checkBox3_CheckedChanged(object sender, EventArgs e) try if (checkBox3.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = true; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Che
16、cked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 准备发送; textBox2.Text = 3; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox4_Che
17、ckedChanged(object sender, EventArgs e) try if (checkBox4.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = true; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Check
18、ed = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 准备发送; textBox2.Text = 4; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox5_CheckedChanged(object sender, EventArgs e) try if (checkBox5.C
19、hecked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = true; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Chec
20、ked = false; radioButton4.Checked = false; label7.Text = 准备发送; textBox2.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 = fals
21、e; checkBox3.Checked = false; checkBox4.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 = 准备
22、发送; textBox2.Text = 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.Checked = false; ch
23、eckBox5.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; catch (Exception er) MessageBox.Show
24、(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; checkBox5.Checked = false; checkBox6.Checked = false; checkBo
25、x7.Checked = false; checkBox8.Checked = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Text = 准备发送; textBox2.Text = 8; catch (Exception 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; checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false;
限制150内