版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.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.ponentModel;using System.Data;using System.Drawing;using System.Te*t;using System.Windows.Forms;using System.IO.Ports;using System.Timers;namespace 单片机功能控制 public partial class
3、Form1 : Form public Form1() Initializeponent(); SerialPort sp = new SerialPort(); private void button1_Click(object sender, EventArgs e) String str1 = boBo*1.Te*t;/串口号 String str2 = boBo*2.Te*t;/波特率 String str3 = boBo*3.Te*t;/校验位 String str4 = boBo*5.Te*t;/停顿位 String str5 = boBo*4.Te*t;/数据位 Int32 in
4、t2 = Convert.ToInt32(str2);/将字符串转为整型 Int32 int5 = Convert.ToInt32(str5);/将字符串转为整型 groupBo*3.Enabled = true;/LED控制界面变可选try if (button1.Te*t = 翻开串口) if (str1 = null) MessageBo*.Show(请先选择串口!, Error); return; sp.Close();sp = new SerialPort(); sp.PortName = boBo*1.Te*t;/串口编号 sp.BaudRate = int2;/波特率 switc
5、h (str4)/停顿位 case 1: sp.StopBits = StopBits.One; break; case 1.5: sp.StopBits = StopBits.OnePointFive; break; case 2: sp.StopBits = StopBits.Two; break; default: MessageBo*.Show(Error:参数不正确, Error); break; switch (str3) case NONE: sp.Parity = Parity.None; break; case ODD: sp.Parity = Parity.Odd; bre
6、ak; case EVEN: sp.Parity = Parity.Even; break; default: MessageBo*.Show(Error:参数不正确, Error); break; sp.DataBits = int5;/数据位 sp.Parity = Parity.Even;/设置串口属性 sp.Open();/翻开串口 button1.Te*t = 关闭串口; te*tBo*1.Te*t = Convert.ToString(sp.PortName) + 已开启!; else sp.Close(); button1.Te*t = 翻开串口; groupBo*3.Enabl
7、ed = false;/LED控制界面变灰色 te*tBo*1.Te*t = Convert.ToString(sp.PortName) + 已关闭!; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void Form1_Load(object sender, EventArgs e) /初始化 te*tBo*1.Te*t = 欢迎使用简易的串口助手!; groupBo*3.Enabled = false;/LED控制界面变灰色 groupBo*6.Enabled = fals
8、e; groupBo*7.Enabled = false; groupBo*8.Enabled = false; button3.Enabled = false; button6.Enabled = false; timer1.Start(); try foreach (string in System.IO.Ports.SerialPort.GetPortNames() /自动获取串行口名称 this.boBo*1.Items.Add(); /默认设置 boBo*1.SelectedInde* = 0;/选择第一个口 boBo*2.SelectedInde* = 4;/波特率4800 boB
9、o*3.SelectedInde* = 0;/校验位NONE boBo*4.SelectedInde* = 0;/停顿位为1 boBo*5.SelectedInde* = 0;/数据位为8 catch MessageBo*.Show(找不到通讯端口!, 串口调试助手); private void timer1_Tick(object sender, EventArgs e) label6.Te*t = DateTime.Now.ToString(); private void button2_Click(object sender, EventArgs e) try if (button2.T
10、e*t = 开启) groupBo*6.Enabled = true; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6
11、.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; button3.Enabled = true; te*tBo*2.Te*t = String.Empty; button2.Te*t = 关闭; else groupBo*6.Enabled = false; button3.Enabled = false; button2.Te*t = 开启; te*tBo*2.Te*t = String.Empty; catch (E*ception er) MessageBo*.Show(Error: + er.
12、Message, Error); return; private void button3_Click(object sender, EventArgs e) groupBo*6.Enabled = true; label7.Te*t = 已发送; if (te*tBo*2.Te*t = ) MessageBo*.Show(发送失败,请选择发送的数据!); else sp.WriteLine(te*tBo*2.Te*t);/往串口写数据 private void checkBo*1_CheckedChanged(object sender, EventArgs e) try if (check
13、Bo*1.Checked) checkBo*1.Checked = true; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 1; catch (E*ception er) MessageBo*.
14、Show(Error: + er.Message, Error); return; private void checkBo*2_CheckedChanged(object sender, EventArgs e) try if (checkBo*2.Checked) checkBo*1.Checked = false; checkBo*2.Checked = true; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; chec
15、kBo*7.Checked = false; checkBo*8.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 2; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void chec
16、kBo*3_CheckedChanged(object sender, EventArgs e) try if (checkBo*3.Checked) checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = true; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; radioButt
17、on1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 3; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void checkBo*4_CheckedChanged(object sender, EventArgs e) try if (ch
18、eckBo*4.Checked) checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = true; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioBut
19、ton3.Checked = false; radioButton4.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 4; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void checkBo*5_CheckedChanged(object sender, EventArgs e) try if (checkBo*5.Checked) checkBo*1.Checked = false; checkBo*2.Check
20、ed = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = true; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.
21、Te*t = 准备发送; te*tBo*2.Te*t = 5; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void checkBo*6_CheckedChanged(object sender, EventArgs e) try if (checkBo*6.Checked) checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked =
22、false; checkBo*5.Checked = false; checkBo*6.Checked = true; checkBo*7.Checked = false; checkBo*8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 6; catch (E*ception er) Messag
23、eBo*.Show(Error: + er.Message, Error); return; private void checkBo*7_CheckedChanged(object sender, EventArgs e) try if (checkBo*7.Checked) checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false
24、; checkBo*7.Checked = true; checkBo*8.Checked = false; radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 7; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void
25、 checkBo*8_CheckedChanged(object sender, EventArgs e) try if (checkBo*8.Checked) checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = true; radi
26、oButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; label7.Te*t = 准备发送; te*tBo*2.Te*t = 8; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void button5_Click(object sender, EventArgs e) try if (button5.
27、Te*t = 开启) radioButton1.Checked = false; radioButton2.Checked = false; radioButton3.Checked = false; radioButton4.Checked = false; checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo
28、*7.Checked = false; checkBo*8.Checked = false; groupBo*7.Enabled = true; button6.Enabled = true; te*tBo*2.Te*t = String.Empty; button5.Te*t = 关闭; else groupBo*7.Enabled = false; button6.Enabled = false; button5.Te*t = 开启; te*tBo*2.Te*t = String.Empty; catch (E*ception er) MessageBo*.Show(Error: + er
29、.Message, Error); return; private void button6_Click(object sender, EventArgs e) label7.Te*t = 已发送; if (te*tBo*2.Te*t = ) MessageBo*.Show(发送失败。请选择发送的数据!); else sp.WriteLine(te*tBo*2.Te*t);/往串口写数据 private void radioButton1_CheckedChanged(object sender, EventArgs e) try if (radioButton1.Checked) label
30、7.Te*t = 准备发送; te*tBo*2.Te*t = 9; checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; catch (E*ception er) MessageBo*.Show(Error: + er.M
31、essage, Error); return; private void radioButton2_CheckedChanged(object sender, EventArgs e) try if (radioButton2.Checked) label7.Te*t = 准备发送; te*tBo*2.Te*t = a; checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; che
32、ckBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void radioButton3_CheckedChanged(object sender, EventArgs e) try if (radioButton3.Checked) label7.Te*t = 准备发送; te*tBo*2.Te*t = b; checkBo*1.
33、Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked = false; checkBo*8.Checked = false; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; private void radioBu
34、tton4_CheckedChanged(object sender, EventArgs e) try if (radioButton4.Checked) label7.Te*t = 准备发送; te*tBo*2.Te*t = c; checkBo*1.Checked = false; checkBo*2.Checked = false; checkBo*3.Checked = false; checkBo*4.Checked = false; checkBo*5.Checked = false; checkBo*6.Checked = false; checkBo*7.Checked =
35、false; checkBo*8.Checked = false; catch (E*ception er) MessageBo*.Show(Error: + er.Message, Error); return; 下位机程序:*include *include *include *define uchar unsigned char /宏定义*define uint unsigned int /宏定义uchar b; /用来接收数据存储uchar j=0;uint i=0; /用来判断相应的子程序uchar LED1= 0*fe; /作为流水灯的开场uchar LED2=0*fe; /作为跑
36、马灯的开场uchar LED3 = 0*fE; /作为二进制加法灯的开场uchar LED4=0*FF; /作为把戏灯的开场uchar code table=0*ff,/全灭0*fe,0*fd,0*fb,0*f7,0*ef,0*df,0*bf,0*7f,/依次逐个点亮0*fe,0*fc,0*f8,0*f0,0*e0,0*c0,0*80,0*00,/依次逐个叠加0*80,0*c0,0*e0,0*f0,0*f8,0*fc,0*fe,0*ff,/依次逐个递减0*7e,0*bd,0*db,0*e7,0*e7,0*db,0*bd,0*7e,/两边靠拢后分开0*7e,0*3c,0*18,0*00,0*00
37、,0*18,0*3c,0*7e,/两边叠加后递减0*00;/全亮void delay(void); /延时函数的声明void liushuideng(void); /流水灯的声明void paomadeng(void); /跑马灯的声明void huayangdeng(void); /把戏灯的声明void erjinzhideng(void); /二进制加法灯的声明void *ianshi(uint j); /显示程序void init(void); /初始化函数void main (void) init(); /初始化 while(1) *ianshi(i); /显示 void init(v
38、oid) /初始化函数的定义 SCON = 0*50; /REN=1允许串行承受状态,串口工作模式1 TMOD|= 0*20; /定时器工作方式2 PCON|= 0*80; /TH1 = 0*FD; /baud*2 /* reload value 19200、数据位8、停顿位1。效验位无 (11.0592) TH1 = 0*F3; /baud*2 /* 波特率4800、数据位8、停顿位1。效验位无 (12M) TL1 = 0*F3; TR1 = 1; /开启定时器PS=1; /把串口中断的优先级设为最高 ES = 1; /开串口中断 EA = 1; / 开总中断 / IE = 0*0; void delay(void) /延时
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 5G通信设备制造行业市场调研分析报告
- 云物流服务行业市场调研分析报告
- 建筑风能利用行业市场调研分析报告
- 宠物用首饰产品供应链分析
- 牙科用贵金属合金商业机会挖掘与战略布局策略研究报告
- 假发粘贴胶水产品供应链分析
- 医用拐杖产品供应链分析
- 卸妆霜产品供应链分析
- 制饮料用机器人出租行业经营分析报告
- 垫席产品供应链分析
- 风险评估与论证制度
- 2024年辅警招聘考试试题库及完整答案(全优)
- 茶叶生产质量管理制度
- 2024年4月自考12656毛中特试题
- 水利工程(水电站)安全生产标准化管理体系方案(达标所需资料全套汇编)
- 2024年高考语文现代文阅读之文学类阅读教考衔接题型
- 海康威视公司发展战略研究
- 行政事业单位会计监督
- 2023-2024学年译林版八年级上学期英语12月月考模拟试卷(含答案解析)
- 【川教版】《生命 生态 安全》五上第8课《防患于未“燃”》课件
- 永久避难硐室避险安全知识课件
评论
0/150
提交评论