欢迎来到沃文网! | 帮助中心 分享知识,传播智慧!
沃文网
全部分类
  • 教学课件>
  • 医学资料>
  • 技术资料>
  • 学术论文>
  • 资格考试>
  • 建筑施工>
  • 实用文档>
  • 其他资料>
  • ImageVerifierCode 换一换
    首页 沃文网 > 资源分类 > DOC文档下载
    分享到微信 分享到微博 分享到QQ空间

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

    • 资源ID:879953       资源大小:136KB        全文页数:18页
    • 资源格式: DOC        下载积分:10积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: QQ登录 微博登录
    二维码
    微信扫一扫登录
    下载资源需要10积分
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,下载更划算!
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    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 parti

    3、al 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 = c

    4、omboBox4.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;/串口

    5、编号 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

    6、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) + 已开启!; else sp.Close(); butt

    7、on1.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

    8、控制界面变灰色 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;/选择第一个co

    9、m口 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

    10、_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 = false; checkBox4.C

    11、hecked = 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.Empt

    12、y; 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_Chec

    13、kedChanged(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; checkB

    15、ox5.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(Er

    16、ror: + 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.C

    17、hecked = 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_C

    18、heckedChanged(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.Che

    19、cked = 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

    20、.Checked) 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.Ch

    21、ecked = 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 = fa

    22、lse; 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 =

    23、准备发送; 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;

    24、checkBox5.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.Sh

    25、ow(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; check

    26、Box7.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.Checke


    注意事项

    本文(基于C#的串口通信上位机和下位机源程序.doc)为本站会员(精***)主动上传,沃文网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知沃文网(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服点击这里,给沃文网发消息,QQ:2622162128 - 联系我们

    版权声明:以上文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如有侵权请立即联系:2622162128@qq.com ,我们立即下架或删除。

    Copyright© 2022-2024 www.wodocx.com ,All Rights Reserved |陕ICP备19002583号-1

    陕公网安备 61072602000132号     违法和不良信息举报:0916-4228922