收藏 分享(赏)

基于C#的串口通信上位机和下位机源程序.doc

上传人:精品资料 文档编号:10770506 上传时间:2020-01-08 格式:DOC 页数:18 大小:137KB
下载 相关 举报
基于C#的串口通信上位机和下位机源程序.doc_第1页
第1页 / 共18页
基于C#的串口通信上位机和下位机源程序.doc_第2页
第2页 / 共18页
基于C#的串口通信上位机和下位机源程序.doc_第3页
第3页 / 共18页
基于C#的串口通信上位机和下位机源程序.doc_第4页
第4页 / 共18页
基于C#的串口通信上位机和下位机源程序.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

1、 基于单片机串口通信的上位机和下位机实践串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线Universal Serial Bus或者USB混淆) 。大多数计算机包含两个基于 RS232的串口。串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS-232口。同时,串口通信协议也可以用于获取远程采集设备的数据。串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节( byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。它很简单并且能够实现远距离通信。比如IEEE488定义并行通行状态时,规定设备线总常不得超过20米,并且

2、任意两个设备间的长度不得超过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.Timers;namespace 单片机功能控制public part

3、ial class Form1 : Formpublic 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

4、.Text;/数据位Int32 int2 = Convert.ToInt32(str2);/将字符串转为整型Int32 int5 = Convert.ToInt32(str5);/将字符串转为整型groupBox3.Enabled = true;/LED控制界面变可选tryif (button1.Text = “打开串口“)if (str1 = null)MessageBox.Show(“请先选择串口!“, “Error“);return;sp.Close();sp = new SerialPort();sp.PortName = comboBox1.Text;/串口编号sp.BaudRate

5、 = 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 = P

6、arity.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) + “已开启!“;elsesp.Close();button1.Text = “打开串口“;gro

7、upBox3.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控制界面变灰色groupBox6.Ena

8、bled = false;groupBox7.Enabled = false;groupBox8.Enabled = false; button3.Enabled = false;button6.Enabled = false;timer1.Start();tryforeach (string com in System.IO.Ports.SerialPort.GetPortNames() /自动获取串行口名称boBox1.Items.Add(com);/默认设置comboBox1.SelectedIndex = 0;/选择第一个com口comboBox2.SelectedIndex = 4;

9、/波特率4800comboBox3.SelectedIndex = 0;/校验位NONEcomboBox4.SelectedIndex = 0;/停止位为1comboBox5.SelectedIndex = 0;/数据位为8catchMessageBox.Show(“找不到通讯端口!“, “ 串口调试助手“);private void timer1_Tick(object sender, EventArgs e)label6.Text = DateTime.Now.ToString();private void button2_Click(object sender, EventArgs e)

10、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.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 = “关闭“;elsegroupBox6.Enabled = false;button3.Enabled = false;button2.Text = “开启“;textBox2.Text = String.Empty;catch (Exception er)MessageBox.Show(“Error:“ + e

12、r.Message, “Error“);return;private void button3_Click(object sender, EventArgs e)groupBox6.Enabled = true;label7.Text = “已发送 “;if (textBox2.Text = “)MessageBox.Show(“发送失败,请选择发送的数据!“);elsesp.WriteLine(textBox2.Text);/往串口写数据private void checkBox1_CheckedChanged(object sender, EventArgs e)try if (check

13、Box1.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 = “准备发送“;textBox2.Text = “1“;catch (Exception er)MessageBox.Show(“Er

14、ror:“ + 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;checkBox5.Checked = false;checkBox6.Checked = false;checkBox7.Checked

15、= 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(“Error:“ + er.Message, “Error“);return;private void checkBox3_CheckedChan

16、ged(object sender, EventArgs e)tryif (checkBox3.Checked)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;radioBut

17、ton2.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_CheckedChanged(object sender, EventArgs e)tryif (checkBox4.Checked)checkBox1.Check

18、ed = false;checkBox2.Checked = false;checkBox3.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

19、= false;label7.Text = “准备发送“;textBox2.Text = “4“;catch (Exception er)MessageBox.Show(“Error:“ + er.Message, “Error“);return;private void checkBox5_CheckedChanged(object sender, EventArgs e)tryif (checkBox5.Checked)checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox

20、4.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 = “准备发送“;textBox2.Text = “5“;catch (Exception er)Me

21、ssageBox.Show(“Error:“ + er.Message, “Error“);return;private void checkBox6_CheckedChanged(object sender, EventArgs e)tryif (checkBox6.Checked)checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Checked = true;ch

22、eckBox7.Checked = false;checkBox8.Checked = false;radioButton1.Checked = false;radioButton2.Checked = false;radioButton3.Checked = false;radioButton4.Checked = false;label7.Text = “准备发送“;textBox2.Text = “6“;catch (Exception er)MessageBox.Show(“Error:“ + er.Message, “Error“);return;private void check

23、Box7_CheckedChanged(object sender, EventArgs e)tryif (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

24、= false;radioButton2.Checked = false;radioButton3.Checked = false;radioButton4.Checked = false;label7.Text = “准备发送“;textBox2.Text = “7“;catch (Exception er)MessageBox.Show(“Error:“ + er.Message, “Error“);return;private void checkBox8_CheckedChanged(object sender, EventArgs e)tryif (checkBox8.Checked

25、)checkBox1.Checked = 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;radio

26、Button4.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)tryif (button5.Text = “开启“)radioButton1.Checked = false;radioButton2.Checked = false;radioButton3.Checked

27、= false;radioButton4.Checked = 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;textBox

28、2.Text = String.Empty;button5.Text = “关闭“;elsegroupBox7.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)label7.Text = “已发送 “;if (t

29、extBox2.Text = “)MessageBox.Show(“发送失败。请选择发送的数据!“);elsesp.WriteLine(textBox2.Text);/往串口写数据private void radioButton1_CheckedChanged(object sender, EventArgs e)tryif (radioButton1.Checked)label7.Text = “准备发送“;textBox2.Text = “9“;checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = f

30、alse;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Checked = false;checkBox7.Checked = false;checkBox8.Checked = false;catch (Exception er)MessageBox.Show(“Error:“ + er.Message, “Error“);return;private void radioButton2_CheckedChanged(object sender, EventArgs e)tryif (radioButton2.Ch

31、ecked)label7.Text = “准备发送“;textBox2.Text = “a“;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:“

32、+ er.Message, “Error“);return;private void radioButton3_CheckedChanged(object sender, EventArgs e)tryif (radioButton3.Checked)label7.Text = “准备发送“;textBox2.Text = “b“;checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;che

33、ckBox6.Checked = false;checkBox7.Checked = false;checkBox8.Checked = false;catch (Exception er)MessageBox.Show(“Error:“ + er.Message, “Error“);return;private void radioButton4_CheckedChanged(object sender, EventArgs e)tryif (radioButton4.Checked)label7.Text = “准备发送“;textBox2.Text = “c“;checkBox1.Che

34、cked = 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, “Error“);return; 下位机程序:#include #include #incl

35、ude #define uchar unsigned char /宏定义#define uint unsigned int /宏定义uchar b; /用来接收数据存储uchar j=0;uint i=0; /用来判断相应的子程序uchar LED1= 0xfe; /作为流水灯的开始uchar LED2=0Xfe; /作为跑马灯的开始uchar LED3 = 0xfE; /作为二进制加法灯的开始uchar LED4=0XFF; /作为花样灯的开始uchar code table=0xff,/全灭0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,/依次逐个点亮0xf

36、e,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,/依次逐个叠加0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,/依次逐个递减0x7e,0xbd,0xdb,0xe7,0xe7,0xdb,0xbd,0x7e,/两边靠拢后分开0x7e,0x3c,0x18,0x00,0x00,0x18,0x3c,0x7e,/两边叠加后递减0x00;/全亮void delay(void); /延时函数的声明void liushuideng(void); /流水灯的声明void paomadeng(void); /跑马灯的声明void huayangdeng(vo

37、id); /花样灯的声明void erjinzhideng(void); /二进制加法灯的声明void xianshi(uint j); /显示程序void init(void); /初始化函数void main (void) init(); /初始化 while(1) xianshi(i); /显示void init(void) /初始化函数的定义SCON = 0x50; /REN=1 允许串行接受状态,串口工作模式 1 TMOD|= 0x20; /定时器工作方式 2 PCON|= 0x80; /TH1 = 0xFD; /baud*2 /* reload value 19200、数据位 8、

38、停止位1。效验位无 (11.0592) TH1 = 0xF3; /baud*2 /* 波特率 4800、数据位 8、停止位 1。效验位无 (12M)TL1 = 0xF3; TR1 = 1; /开启定时器PS=1; /把串口中断的优先级设为最高 ES = 1; /开串口中断 EA = 1; / 开总中断 / IE = 0x0;void delay(void) /延时函数的定义uchar i;uchar ms=250;while(ms-)for(i = 0; i 120; i+);void liushuideng(void) /流水灯P2 = LED1;delay(); LED1 = LED1 1

39、; /循环右移 1 位,点亮下一个 LED “为左移位if(P2 =0X00 )LED1 = 0xfe; / 0xfe = 1111 1110 void paomadeng(void) /跑马灯P2 = LED2;LED2 = _crol_(LED2,1); /循环右移 1 位,点亮下一个 LED 此函数位库函数delay();void huayangdeng(void) /花样灯 P2=tablej+;delay();if(j=42)j=0; void erjinzhideng(void) /二进制加法灯P2 = LED4;delay();LED4-;void xianshi(uint j)

40、 /调用相对应的程序if(j=1)P2=0xfe; /点亮第一个 LEDif(j=2)P2=0xfd; /点亮第二个 LEDif(j=3)P2=0xfb; /点亮第三个 LEDif(j=4)P2=0xf7; /点亮第四个 LEDif(j=5)P2=0xef; /点亮第五个 LEDif(j=6)P2=0xdf; /点亮第六个 LEDif(j=7)P2=0xbf; /点亮第七个 LEDif(j=8)P2=0x7f; /点亮第八个 LEDif(j=9)liushuideng(); /流水灯if(j=10)paomadeng(); /跑马灯if(j=11)huayangdeng(); /花样灯if(j

41、=12)erjinzhideng(); /二进制加法灯void com_isr(void) interrupt 4 /串口中断入口函数ES=0; /再中断进行处理程序的时候关闭串口中断b=SBUF; /将接收的数据存于变量中switch(b) /分支语句,进行判断 case 1:i=1;break; /点亮第一个 LED 的赋值case 2:i=2;break; /点亮第二个 LED 的赋值case 3:i=3;break; /点亮第三个 LED 的赋值case 4:i=4;break; /点亮第四个 LED 的赋值case 5:i=5;break; /点亮第五个 LED 的赋值case 6:i=6;break; /点亮第六个 LED 的赋值case 7:i=7;break; /点亮第七个 LED 的赋值case 8:i=8;break; /点亮第八个 LED 的赋值case 9:i=9;break; /流水灯的赋值case a:i=10;break; /跑马灯的赋值case b:i=11;break; /花样灯的赋值case c:i=12;break; /二进制加法灯的赋值 default:break;RI=0; /清除中断标志位ES=1; /处理完中断程序再开启串口中断

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报