1、基于C#的串口通信上位机和下位机源程序基于单片机串口通信的上位机和下位机实践 串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线Universal Serial Bus或者USB混淆)。大多数计算机包含两个基于RS232的串口。串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS-232口。同时,串口通信协议也可以用于获取远程采集设备的数据。 串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。它很简单并且能够实现远距离通信。比如IEEE488定义并行通行状态时,
2、规定设备线总常不得超过20米,并且任意两个设备间的长度不得超过2米;而对于串口而言,长度可达1200米。 首先亮出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.Timer
3、s; 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 = co
4、mboBox5.Text;/停止位 String str5 = comboBox4.Text;/数据位 Int32 int2 = Convert.ToInt32(str2);/将字符串转为整型 Int32 int5 = Convert.ToInt32(str5);/将字符串转为整型 groupBox3.Enabled = true;/LED控制界面变可选 try 晩?畢瑴湯?敔瑸?打开串口) if (str1 = null) 敍獳条?硯匮潨?请先选择串口!, Error); return; sp.Close(); sp = new SerialPort(); sp.PortName = com
5、boBox1.Text;/串口编号 sp.BaudRate = int2;/波特率 switch (str4)/停止位 case : sp.StopBits = StopBits.One; break; case .5: sp.StopBits = StopBits.OnePointFive; break; case : sp.StopBits = StopBits.Two; break; default: MessageBox.Show(Error:参数不正确, Error); break; switch (str3) case NONE: sp.Parity = Parity.None;
6、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();/打开串口 畢瑴湯?敔瑸?尠关闭串口; 整瑸潂?吮硥?潃癮牥?潔瑓楲杮猨?潐瑲慎敭?已开启!; else sp.Close(); 畢瑴湯?敔瑸?尠打开串口; groupBox3.E
7、nabled = false;/LED控制界面变灰色 整瑸潂?吮硥?潃癮牥?潔瑓楲杮猨?潐瑲慎敭?已关闭!; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void Form1_Load(object sender, EventArgs e) 初始化/ 整瑸潂?吮硥?欢迎使用简易的串口助手!; groupBox3.Enabled = false;/LED控制界面变灰色 groupBox6.Enabled = false; groupBox7.Enabled = false; g
8、roupBox8.Enabled = false; button3.Enabled = false; button6.Enabled = false; timer1.Start(); try foreach (string com in System.IO.Ports.SerialPort.GetPortNames() 自动/ 获取串行口名称boBox1.Items.Add(com); /默认设置 选择第一个com口 comboBox1.SelectedIndex = 0;/ 4800 comboBox2.SelectedIndex = 4;/波特率 NONE comboBox3.Select
9、edIndex = 0;/校验位 1 comboBox4.SelectedIndex = 0;/停止位为 8 comboBox5.SelectedIndex = 0;/数据位为 catch ); 串口调试助手 敍獳条?硯匮潨?找不到通讯端口!?尠 private void timer1_Tick(object sender, EventArgs e) label6.Text = DateTime.Now.ToString(); private void button2_Click(object sender, EventArgs e) try ) 晩?畢瑴湯?敔瑸?开启 groupBox6.E
10、nabled = 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; checkBox6.Checked = false; che
11、ckBox7.Checked = false; checkBox8.Checked = false; button3.Enabled = true; textBox2.Text = String.Empty; 畢瑴湯?敔瑸?尠关闭; else groupBox6.Enabled = false; button3.Enabled = false; 畢瑴湯?敔瑸?尠开启; textBox2.Text = String.Empty; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private vo
12、id button3_Click(object sender, EventArgs e) groupBox6.Enabled = true; 慬敢?吮硥?已发送; if (textBox2.Text = ) 敍獳条?硯匮潨?发送失败,请选择发送的数据!); else sp.WriteLine(textBox2.Text);/往串口写数据 private void checkBox1_CheckedChanged(object sender, EventArgs e) try if (checkBox1.Checked) checkBox1.Checked = true; checkBox2.C
13、hecked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; 慬敢?吮硥?准备发送; textBox2.Text = ; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void check
14、Box2_CheckedChanged(object sender, EventArgs e) try if (checkBox2.Checked) checkBox1.Checked = false; checkBox2.Checked = true; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; 慬敢?吮硥?准备发送
15、; textBox2.Text = ; radioButton1.Checked = false; radioButton2.Checked = false; 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.Ch
16、ecked) checkBox1.Checked = false; checkBox2.Checked = 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.Check
17、ed = false; radioButton4.Checked = false; 慬敢?吮硥?准备发送; textBox2.Text = ; 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; checkBo
18、x3.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; 慬敢?吮硥?准备发送; textBox2.Text
19、= ; 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.Checked = false; checkBox4.Checked = false; checkBox5.Checked = t
20、rue; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; 慬敢?吮硥?准备发送; textBox2.Text = _x0005_; catch (Exception er) MessageBox.Show(Error: + er.Message,
21、 Error); return; private void checkBox6_CheckedChanged(object sender, EventArgs e) try if (checkBox6.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = true; checkBox7.Checked = false; ch
22、eckBox8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; 慬敢?吮硥?准备发送; textBox2.Text = ; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox7_CheckedChanged(object sender
23、, EventArgs e) try if (checkBox7.Checked) checkBox1.Checked = false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = true; checkBox8.Checked = false; radioButton1.Checked = false; radioButton2.
24、Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; 慬敢?吮硥?准备发送; textBox2.Text = _x0007_; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void checkBox8_CheckedChanged(object sender, EventArgs e) try if (checkBox8.Checked) checkBox1.Checked =
25、 false; checkBox2.Checked = false; checkBox3.Checked = false; checkBox4.Checked = false; checkBox5.Checked = false; checkBox6.Checked = false; checkBox7.Checked = false; checkBox8.Checked = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.C
26、hecked = false; 慬敢?吮硥?准备发送; textBox2.Text = 8; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void button5_Click(object sender, EventArgs e) try 晩?畢瑴湯?敔瑸?开启) radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Chec
27、ked = false; 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; 畢瑴湯?敔瑸?尠关闭; else groupBox7.Enabled = false; button6.Enabled = false; 畢瑴湯?敔瑸?尠开启; textBox2.Text = String.Empty; catch (Exception er) MessageBox.Show(Error: + er.Message, Error); return; private void button6_Click(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1