基于C#的串口通信上位机和下位机源程序文件_第1页
基于C#的串口通信上位机和下位机源程序文件_第2页
基于C#的串口通信上位机和下位机源程序文件_第3页
基于C#的串口通信上位机和下位机源程序文件_第4页
免费预览已结束,剩余31页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.基于单片机串口通信的上位机和下位机实践串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线UniversalSerialBus或者 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 : 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 = combo

4、Box5.Text;/停止位String str5 = comboBox4.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.Po

5、rtName = 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:参数不正确 , Error);break;switch (str3)case NONE:sp.Parity = Parity.None; b

6、reak;.下载可编辑 .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();/打开串口button1.Text = 关闭串口 ;textBox1.Text = Convert.ToString(sp.PortName) + 已开启 ! ;elsesp.Clos

7、e();button1.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

8、= false;/LED控制界面变灰色groupBox6.Enabled = 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.SelectedI

9、ndex = 0;/选择第一个 com 口comboBox2.SelectedIndex = 4;/波特率 4800comboBox3.SelectedIndex = 0;/校验位 NONEcomboBox4.SelectedIndex = 0;/停止位为 1comboBox5.SelectedIndex = 0;/数据位为 8catchMessageBox.Show(找不到通讯端口! , 串口调试助手 );private void timer1_Tick(object sender, EventArgs e)label6.Text = DateTime.Now.ToString();.下载可

10、编辑 .private void button2_Click(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 = fa

11、lse;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.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

12、= String.Empty;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(发送失败 ,请选择发送的数据! );elsesp.WriteLine(textBox2.Text);/往串口写数据private void c

13、heckBox1_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.Checked = false;label7.Text = 准备

14、发送 ;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;checkBox2.Checked = true;checkBox3.Checked = false;checkBox4.Checked = false;check

15、Box5.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(Err

16、or: + er.Message, Error);return;private void checkBox3_CheckedChanged(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.Check

17、ed = 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_Checked

18、Changed(object sender, EventArgs e)tryif (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.Checked = false;radi

19、oButton2.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)tryif (checkBox5.Checked)checkBox1.

20、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.Checked = false;radioBut

21、ton4.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)tryif (checkBox6.Checked).下载可编辑 .checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked =

22、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 = 准备发送 ;textBox2.Text = 6;catch (Exce

23、ption er)MessageBox.Show(Error: + er.Message, Error);return;.下载可编辑 .private void checkBox7_CheckedChanged(object sender, EventArgs e)tryif (checkBox7.Checked)checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Ch

24、ecked = 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(Error: + er.Message, Error);return;pr

25、ivate void checkBox8_CheckedChanged(object sender, EventArgs e)tryif (checkBox8.Checked)checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Checked = false;checkBox7.Checked = false;.下载可编辑 .checkBox8.Checked = tr

26、ue;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)tryif (button5.Tex

27、t = 开启 ).下载可编辑 .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;checkBox7.Ch

28、ecked = false;checkBox8.Checked = false;groupBox7.Enabled = true;button6.Enabled = true;textBox2.Text = String.Empty;button5.Text = 关闭 ;elsegroupBox7.Enabled = false;button6.Enabled = false;button5.Text = 开启 ;.下载可编辑 .textBox2.Text = String.Empty;catch (Exception er)MessageBox.Show(Error: + er.Messag

29、e, Error);return;private void button6_Click(object sender, EventArgs e)label7.Text = 已发送 ;if (textBox2.Text = )MessageBox.Show(发送失败 。 请选择发送的数据! );elsesp.WriteLine(textBox2.Text);/往串口写数据private void radioButton1_CheckedChanged(object sender, EventArgs e).下载可编辑 .tryif (radioButton1.Checked)label7.Text

30、 = 准备发送 ;textBox2.Text = 9;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, Error);r

31、eturn;.下载可编辑 .private void radioButton2_CheckedChanged(object sender, EventArgs e)tryif (radioButton2.Checked)label7.Text = 准备发送 ;textBox2.Text = a;checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Checked = fa

32、lse;checkBox7.Checked = false;checkBox8.Checked = false;catch (Exception er).下载可编辑 .MessageBox.Show(Error: + er.Message, Error);return;private void radioButton3_CheckedChanged(object sender, EventArgs e)tryif (radioButton3.Checked)label7.Text = 准备发送 ;textBox2.Text = b;checkBox1.Checked = false;check

33、Box2.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;private void radioButton4_CheckedChanged(obj

34、ect sender, EventArgs e)tryif (radioButton4.Checked)label7.Text = 准备发送 ;textBox2.Text = c;checkBox1.Checked = false;checkBox2.Checked = false;checkBox3.Checked = false;checkBox4.Checked = false;checkBox5.Checked = false;checkBox6.Checked = false;.下载可编辑 .checkBox7.Checked = false;checkBox8.Checked =

35、false;catch (Exception er)MessageBox.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= 0xfe;/ 作为流水灯的开始uchar LED2=0Xfe;/ 作为跑马灯的开始uchar LED3 = 0xfE

36、;/ 作为二进制加法灯的开始uchar LED4=0XFF;/ 作为花样灯的开始uchar code table=0xff,/ 全灭0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,/依次逐个点亮0xfe,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,/两边叠加

37、后递减0x00;/ 全亮void delay(void);/ 延时函数的声明void liushuideng(void);/ 流水灯的声明void paomadeng(void);/ 跑马灯的声明void huayangdeng(void);/ 花样灯的声明.下载可编辑 .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允许串行接受状态,串口工作模式1TMOD|= 0x2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论