




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Data;using System.Threading;namespace BusApp/ / Form1 的摘要说明。/ public int iPort=1; /1,2,3,4public int iRate=9600; /1200,2400,4800,9600public byte bSize=8; /8 bitspublic byte bParity=0; / 0-4=no,odd,eve
2、n,mark,space public byte bStopBits=1; / 0,1,2 = 1, 1.5, 2 public int iTimeout=1000;public mycom mycom1=new mycom(;public byte recb;/ / 必需的设计器变量。/ public Form1(InitializeComponent(;/ / 清理所有正在使用的资源。/ protected override void Dispose( bool disposing if( disposing if (components != null components.Dispos
3、e(;base.Dispose( disposing ;#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要使用代码编辑器修改/ 此方法的内容。/ private void InitializeComponent(this.SuspendLayout(;/ / msg/ / / label1/ / / label2/ / / t_send/ / / button1/ / / groupBox1/ / / button2/ / / t_port/ / / label3/ / / t_rate/ / / label4/ / / t_bytesize/ / /
4、label5/ / / t_stopbyte/ / / label6/ / / t_parity/ / / button3/ / / button4/ / / textBox8/ / / label7/ / / button5/ / / Form1/ this.Name = "Form1"this.Text = "串口通讯(小y设计)"this.Load += new System.EventHandler(this.Form1_Load;this.ResumeLayout(false;#endregion/ / 应用程序的主入口点。/ </sum
5、mary>STAThreadstatic void Main( Application.Run(new Form1(;/程序开启,串口初始化private void Form1_Load(object sender, System.EventArgs emycom1.PortNum=iPort;mycom1.BaudRate=iRate;mycom1.ByteSize=bSize;mycom1.Parity=bParity;mycom1.StopBits=bStopBits;mycom1.ReadTimeout=iTimeout;if(this.OpenCom(msg.AppendTex
6、t("串口初始化成功rn"elsemsg.AppendText("串口初始化失败!rn"/显示包信息public string dis_package(byte rebstring temp=""foreach(byte b in rebtemp+=b.ToString("X2"+" "return temp;/开串口public bool OpenCom(try if (mycom1.Opened mycom1.Close(; mycom1.Open(; /打开串口 else mycom1.O
7、pen(;/打开串口 return true;catch(Exception e MessageBox.Show("错误:" + e.Message; return false;/发送按钮private void button1_Click(object sender, System.EventArgs eif(t_send.Text=""MessageBox.Show("发送数据为空!"return;byte temp1=mysendb(;int sendnumb=0;trysendnumb=mycom1.Write(temp1;m
8、sg.AppendText("rn发送数据("+sendnumb+":"+dis_package(temp1;recb=mycom1.Read(50;/if(recb.Length!=0msg.AppendText("rn接收到数据包:"+dis_package(recb;catchmsg.AppendText("rn发送失败!"return;/OpenCom(;/去掉发送数组中的空格public string delspace(string putinstring putout=""for(i
9、nt i=0;i if(putini!=' 'putout+=putini;return putout;/提取数据包public byte mysendb(string temps=delspace(t_send.Text;byte tempb=new byte50;int j=0;for(int i=0;i tempbj=Convert.ToByte(temps.Substring(i,2,16;byte send=new bytej;Array.Copy(tempb,send,j;return send;/清空按钮private void button3_Click(obj
10、ect sender, System.EventArgs et_send.Text=string.Empty;msg.Text=string.Empty;/参数设置private void button2_Click(object sender, System.EventArgs emycom1.PortNum=Convert.ToInt16(t_port.Text; /1,2,3,4mycom1.BaudRate=Convert.ToInt16(t_rate.Text; /1200,2400,4800,9600mycom1.ByteSize=Convert.ToByte(t_bytesize
11、.Text,10; /8 bitsmycom1.Parity=Convert.ToByte(t_parity.Text,10; / 0-4=no,odd,even,mark,spacemycom1.StopBits=Convert.ToByte(t_stopbyte.Text,10; / 0,1,2 = 1, 1.5, 2 /iTimeout=3;if(this.OpenCom(msg.AppendText("串口初始化成功rn"elsemsg.AppendText("串口初始化失败!rn"/程序关闭,结束串口mycom1.Close(;private void button5_Click(object sender, System.EventArgs
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 61496-3:2025 EN Safety of machinery - Electro-sensitive protective equipment - Part 3: Particular requirements for active opto-electronic protective devices responsive t
- 【正版授权】 IEC 61400-5:2020/AMD1:2025 EN Amendment 1 - Wind energy generation systems - Part 5: Wind turbine blades
- 重庆汽车培训理论知识课件
- 重大安全发现管控课件
- 老年人糖尿病护理课件
- 老年人播音主持课件
- 老年人应急知识培训方案课件
- CN120204435A 一种综合药品稳定性实验箱灭菌控制方法及系统
- 水工监测工-渗流观测考试题库
- 第三节 第3课时 难点专攻夺高分-与圆有关的综合问题 2026年高三数学第一轮总复习
- 2025四川德阳经济技术开发区管理委员会考核招聘事业单位人员3人笔试备考试题及答案解析
- 2025至2030中国课外辅导行业发展研究与产业战略规划分析评估报告
- 电梯维护保养标准作业指导书
- 纪念西路军课件
- 一年级书法教学设计方案
- 排球队朱婷史记课件
- 2025年中国烟花爆竹协会烟花工艺师认证考试模拟题及答案
- 网络机房建设方案:规划、设计及实施指南
- 饮料厂合作合同协议书模板
- 医院风险评估体系构建与实施
- 2025年初级注册安全工程师(其他安全)历年参考题库含答案详解(5套)
评论
0/150
提交评论