版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Visual Basic与RS-232串口通信论文题目: Visual Basic与RS-232串口通信实作者:高丽君班级:自动化0803班学号: 0806050303时间: 2011年5月8日Visual Basic与RS-232串口通信实验一、实验目的:通过软件Visual Basic编写程序和硬件计算机、RS-232实现比较完善的通信:数据的接收与发送。二、实验器材:PC机一台、RS-232一条、Visual Basic软件三、实验内容:(1) 在线设置通信速率、奇偶校验、通信口而无需重新启动程序;(2) 设置定时发送的数据和时间间隔;(3) 自动显示接收到的数据,并能在在字符串、十进制
2、、十六进制之间进行自由切换;(4) 自动保存设置参数。四、实验原理:4.1Visual Basic 简介Visual Basic(VB)是一种由微软公司开发的包含协助开发环境的事件驱动编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松的使用VB提供的组件快速建立一个应用程序。4.1.1窗体控件控件就是构成或者说建造Visual Basic应用程序的图形化工具,包括窗体、按钮、复选框、列表框、数据控件、表格控件和图片控件等等.。每个控件都有自己的属性和事件。默认的属性值会在控件
3、创建的时候提供,但是程序员也可以进行更改。很多的属性值可以在运行时候随着用户的动作和修改进行改动,这样就形成了一个动态的程序。 VB的组件既可以拥有用户界面,也可以没有。这样一来服务器端程序就可以处理增加的模块。41.2VB的程序可以包含一个或多个窗体,或者是一个主窗体和多个子窗体。有很少功能的对话框窗口(比如没有最大化和最小化按钮的窗体)可以用来提供弹出功能事件由用户或操作系统引发的动作。事件的示例有击键、单击鼠标(Click)、双击鼠标(DblClick)、一段时间的限制,或从端口接收数据。4.2 RS-232串口通信原理串口是计算机上一种非常通用设备通信的协议,大多数计算机包含两个基于R
4、S-232的串口。串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS-232口。同时,串口通信协议也可以用于获取远程采集设备的数据。电气特性:RS-232标准的信号传输的最大电缆长度为30M; RS-232C上传送的数字量采用负逻辑,且与地对称。逻辑1:-3-15V (单片机一般为-10V)逻辑0:+3+15V (单片机一般为+10V)4.3串口通信串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。它很简单并且能够实现远距
5、离通信。典型地,串口用于ASCII码字符的传输。通信使用3根线完成:地线,发送,接收。由于串口通信是异步的,端口能够在一根线上发送数据同时在另一根线上接收数据。其他线用于握手,但是不是必须的。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通行的端口,这些参数必须匹配:波特率:这是一个衡量通信速度的参数。它表示每秒钟传送的bit的个数。例如300波特表示每秒钟发送300个bit。当我们提到时钟周期时,我们就是指波特率例如如果协议需要4800波特率,那么时钟是4800Hz。这意味着串口通信在数据线上的采样率为4800Hz。通常电话线的波特率为14400,28800和36600
6、。波特率可以远远大于这些值,但是波特率和距离成反比。高波特率常常用于放置的很近的仪器间的通信,典型的例子就是GPIB设备的通信。数据位:这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据不会是8位的,标准的值是5、7和8位。如何设置取决于你想传送的信息。比如,标准的ASCII码是0127(7位)。扩展的ASCII码是0255(8位)。如果数据使用简单的文本(标准 ASCII码),那么每个数据包使用7位数据。每个包是指一个字节,包括开始/停止位,数据位和奇偶校验位。由于实际数据位取决于通信协议的选取,术语“包”指任何通信的情况。停止位:用于表示单个包的最后一位。典型的值为1,1.
7、5和2位。由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。适用于停止位的位数越多,不同时钟同步的容忍程度越大,但是数据传输率同时也越慢。奇偶校验位:在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高和低。当然没有校验位也是可以的。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位。例如,如果数据是011,那么对于偶校验,校验位为0,保证逻辑高的位数是偶数个。如果是奇校验,校验位位1,这样就有3个逻辑高位。高位和低
8、位不真正的检查数据,简单置位逻辑高或者逻辑低校验。这样使得接收设备能够知道一个位的状态,有机会判断是否有噪声干扰了通信或者是否传输和接收数据是否不同步.五、实验步骤:5.1创建项目文件创建一个基于对话框程序,其中设置“自动发送”、“手动发送”、“开始接收”、“设置参数”和“清除”按钮,分别用于自动向串口发送数据、手动向串口发送数据、从串口接收数据和设置串口参数(串口参数,发送字符,自动发送间隔);一个对话框,响应“设置参数”按钮操作,用于设置串口参数,发送字符、自动发送间隔。具体过程如下:(1)启动,选择“文件”“新建工程”命令。()在工程模板中选择“标准EXE”,Visual Basic将自
9、动产生一个Form窗体,属行都是默认设置。(3)选择“文件”“保存工程”命令,(4)在控件工具栏上单击鼠标右键,在弹出的对话框中选择“部件”,选择Microsoft CommControl6,0,单击“确定”按钮把它加入到控件工具栏中。5.2设计窗体主界面如图所示:5.3设置参数界面从工程菜单中选择“添加窗体”为主窗体添加对话框,如图所示:54设计发送与接收的流程图:开始开始初始化初始化选择接收数据类型选择发送数据类型设置参数设置参数接收发送结束结束发送流程图接收流程图5.5程序设计5.5.1建立公共模块:(1)在菜单选择“工程”“添加模块”命令,则弹出“添加模块”对话框。(2)选择模块图标后
10、,单击“打开”按钮,则模块已经添加到项目中了。将此模块命名为Main。在模块中定义整个项目的公共变量。Public intPort As Integer '串行口号Public strSet As String '协议设置Public intTime As Integer '发送时间间隔Public blnAutoSendFlag As Boolean '发送标志Public blnReceiveFlag As Boolean '接收标志Public intOutMode As Integer '发送模式Public strSendText As
11、 String '发送文本数据Public bytSendByte() As Byte '发送二进制数据Public intHexChk As Integer '十六进制编码标志Public intAsciiChk As Integer 'ASCII码标志Public intAddressChk As Integer '地址标志Public intAdd48Chk As Integer '4/8位地址标志5.5.2 定义模块变量'接收模块Public bytReceiveByte() As Byte '接收到的字节Public i
12、ntReceiveLen As Integer '接收到的字节数'显示模块Public strAddress As String '地址信息Public strHex As String '十六进制编码Public strAscii As String 'ASCII码Public intHexWidth As Integer '显示列数Public intOriginX As Long '横向原点(像素)Public intOriginY As Integer '纵向原点(行)Public intLine As Integer &
13、#39;总行数'显示常量Public Const ChrWidth = 105 '单位宽度Public Const ChrHeight = 2 * ChrWidth '单位高度Public Const BorderWidth = 210 '预留边界Public Const LineMax = 16 '最大显示行数'*'输入处理'处理接收到的字节流,并保存在全局变量'bytReceiveRyte()'*Public Sub InputManage(bytInput() As Byte, intInputLenth A
14、s Integer)Dim n As Integer '定义变量及初始化ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)For n = 1 To intInputLenth Step 1bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)Next nintReceiveLen = intReceiveLen + intInputLenthEnd Sub'为输出准备文本'保存在全局变量'strText'strHex'st
15、rAddress'总行数保存在'intLinePublic Sub GetDisplayText()Dim n As IntegerDim intValue As IntegerDim intHighHex As IntegerDim intLowHex As IntegerDim strSingleChr As String * 1Dim intAddress As IntegerDim intAddressArray(8) As IntegerDim intHighAddress As Integer strAscii = "" '设置初值str
16、Hex = ""strAddress = "" '* '获得16进制码和ASCII码的字符串 '*For n = 1 To intReceiveLenintValue = bytReceiveByte(n - 1) If intValue < 32 Or intValue > 128 Then '处理非法字符strSingleChr = Chr(46) '对于不能显示的ASCII码,Else '用"."表示strSingleChr = Chr(intValue)End If
17、strAscii = strAscii + strSingleChr intHighHex = intValue 16 intLowHex = intValue - intHighHex * 16 If intHighHex < 10 Then intHighHex = intHighHex + 48 Else intHighHex = intHighHex + 55 End If If intLowHex < 10 Then intLowHex = intLowHex + 48 Else intLowHex = intLowHex + 55 End If strHex = str
18、Hex + " " + Chr$(intHighHex) + Chr$(intLowHex) + " "If (n Mod intHexWidth) = 0 Then '设置换行strAscii = strAscii + Chr$(13) + Chr$(10)strHex = strHex + Chr$(13) + Chr$(10)ElseEnd IfNext n'*'获得地址字符串'*intLine = intReceiveLen intHexWidthIf (intReceiveLen - intHexWidth *
19、intLine) > 0 Then intLine = intLine + 1 End If For n = 1 To intLine intAddress = (n - 1) * intHexWidth If intAdd48Chk = 1 Then intHighAddress = 8ElseintHighAddress = 4End IfintAddressArray(0) = intAddress For m = 1 To intHighAddress intAddressArray(m) = intAddressArray(m - 1) 16 Next mFor m = 1 T
20、o intHighAddressintAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16Next m For m = 1 To intHighAddressIf intAddressArray(intHighAddress - m) < 10 Then intAddressArray(intHighAddress-m)=intAddressArray(intHighAddress - m) + Asc("0") ElseintAddressArray(intHighAddress
21、-m)=intAddressArray(intHighAddress - m) + Asc("A") - 10End IfstrAddress=strAddress+Chr$(intAddressArray(intHighAddress-m)Next mstrAddress = strAddress + Chr$(13) + Chr$(10) '设置换行 Next n '*End Sub'*'显示输出'*Public Sub display() Dim intViewWidth As Long '横向宽度(像素) Dim in
22、tViewLine As Integer '纵向宽度(行) Dim strDisplayAddress As String Dim strDisplayHex As String Dim strDisplayAscii As String strDisplayAddress = "" strDisplayHex = "" strDisplayAscii = "" Dim intStart As Integer Dim intLenth As Integer intStart = intOriginY * (intHexWidt
23、h + 2) + 1 intLenth = intViewLine * (intHexWidth + 2) strDisplayAscii = Mid(strAscii, intStart, intLenth)End Sub'*'文本无变化的刷新'*Public Sub ScrollRedisplay() Call displayEnd Sub'*'文本发生变化的刷新'*Public Sub SlideRedisplay() Call GetDisplayText Call displayEnd Sub5.5.3 主窗体程序设置初始的发送和接收参
24、数;设置各复选框的初始状态;初始串行口。'*'字符表示的十六进制数转化为相应的整数'错误则返回 -1'*Function ConvertHexChr(str As String) As IntegerDim test As Integertest = Asc(str)If test >= Asc("0") And test <= Asc("9") Thentest = test - Asc("0")ElseIf test >= Asc("a") And test &
25、lt;= Asc("f") Thentest = test - Asc("a") + 10ElseIf test >= Asc("A") And test <= Asc("F") Thentest = test - Asc("A") + 10Elsetest = -1 '出错信息End IfConvertHexChr = testEnd Function'*'字符串表示的十六进制数据转化为相应的字节串'返回转化后的字节数'*Function s
26、trHexToByteArray(strText As String, bytByte() As Byte) As IntegerDim HexData As Integer '十六进制(二进制)数据字节对应值Dim hstr As String * 1 '高位字符Dim lstr As String * 1 '低位字符Dim HighHexData As Integer '高位数值Dim LowHexData As Integer '低位数值Dim HexDataLen As Integer '字节数Dim StringLen As Integ
27、er '字符串长度Dim Account As Integer '计数strTestn = "" '设初值HexDataLen = 0strHexToByteArray = 0StringLen = Len(strText)Account = StringLen 2ReDim bytByte(Account) For n = 1 To StringLenDo '清除空格hstr = Mid(strText, n, 1) n = n + 1If (n - 1) > StringLen ThenHexDataLen = HexDataLen
28、 - 1 Exit ForEnd IfLoop While hstr = " "Dolstr = Mid(strText, n, 1)n = n + 1If (n - 1) > StringLen ThenHexDataLen = HexDataLen - 1Exit ForEnd IfLoop While lstr = " "n = n - 1If n > StringLen ThenHexDataLen = HexDataLen - 1Exit ForEnd IfHighHexData = ConvertHexChr(hstr)LowHe
29、xData = ConvertHexChr(lstr)If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字符中断转化HexDataLen = HexDataLen - 1Exit ForElseHexData = HighHexData * 16 + LowHexDatabytByte(HexDataLen) = HexDataHexDataLen = HexDataLen + 1End IfNext n IfHexDataLen>0Then '修正最后一次循环改变的数值HexDataLen = HexDataLen - 1
30、ReDim Preserve bytByte(HexDataLen)ElseReDim Preserve bytByte(0)End IfIf StringLen = 0 Then '如果是空串,则不会进入循环体strHexToByteArray = 0ElsestrHexToByteArray = HexDataLen + 1End IfEnd FunctionPrivate Sub combo_Click() If Mbo.Text = "按ASCII码" ThenintOutMode = 0 ElseintOutMode = 1 End IfEnd SubPr
31、ivate Sub chkAddress_Click() If chkAddress.Value = 0 Then intAddressChk = 0 Else intAddressChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub chkAddress48_Click() If chkAddress48.Value = 1 Then intAdd48Chk = 1 Else intAdd48Chk = 0 End If Call SlideRedisplayEnd SubPrivate Sub chkAscii_Click() If c
32、hkAscii.Value = 1 Then intAsciiChk = 1 Else intAsciiChk = 0 End If Call ScrollRedisplayEnd SubPrivate Sub chkHex_Click() If chkHex.Value = 0 Then intHexChk = 0 Else intHexChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub send_Click() If blnAutoSendFlag Then Main.ctrTimer.Enabled = False If Not b
33、lnReceiveFlag Then Main.ctrMSComm.PortOpen = False End If Main.send.Caption = "自动发送" Else If Not Main.ctrMSComm.PortOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Main.ctrTimer.Interval = intTime Main.ctrTimer.Enabled = Tr
34、ue Main.send.Caption = "停止发送" End If blnAutoSendFlag = Not blnAutoSendFlagEnd SubPrivate Sub clear_Click() Dim bytTemp(0) As Byte ReDim bytReceiveByte(0) intReceiveLen = 0 Call InputManage(bytTemp, 0) Call GetDisplayText Call displayEnd SubPrivate Sub send2_Click() If Not Main.ctrMSComm.Po
35、rtOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Call ctrTimer_Timer If Not blnAutoSendFlag Then Main.ctrMSComm.PortOpen = False End IfEnd SubPrivate Sub Receive_Click() If blnReceiveFlag Then If Not blnAutoSendFlag And Not blnRecei
36、veFlag Then Main.ctrMSComm.PortOpen = False End If Main.Receive.Caption = "开始接收" Else If Not Main.ctrMSComm.PortOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Main.ctrMSComm.InputLen = 0 Main.ctrMSComm.InputMode = 0 Main.c
37、trMSComm.InBufferCount = 0 Main.ctrMSComm.RThreshold = 1 Main.Receive.Caption = "停止接收" End If blnReceiveFlag = Not blnReceiveFlagEnd SubPrivate Sub setting_Click() config.Show config.Port.Text = str(intPort) config.Setting.Text = strSet config.Time.Text = str(intTime)End SubPrivate Sub ctr
38、MSComm_OnComm() Dim bytInput() As Byte Dim intInputLen As Integer Case comEvReceive If blnReceiveFlag ThenIf Not Main.ctrMSComm.PortOpen ThenMain.ctrMSComm.CommPort = intPortMain.ctrMSComm.Settings = strSetMain.ctrMSComm.PortOpen = TrueEnd If'此处添加处理接收的代码Main.ctrMSComm.InputMode = comInputModeBin
39、aryReDimCall InputManage(bytInput, intInputLen)Call GetDisplayTextCall displayIf Not blnAutoSendFlag And Not blnReceiveFlag ThenMain.ctrMSComm.PortOpen = False End IfEnd SelectEnd SubPrivate Sub ctrTimer_Timer() Dim longth As Integer If intOutMode = 0 Then Main.txtReceive.Text = "ascii" Ma
40、in.ctrMSComm.Output = strSendText Else 'add code longth = strHexToByteArray(strSendText, bytSendByte() If longth > 0 Then Main.ctrMSComm.Output = bytSendByte End If End IfEnd Sub'*'初始化'*Private Sub Form_Load()If ctrMSComm.PortOpen = False ThenctrMSComm.PortOpen = TrueEnd If '设
41、置默认发送接收关闭状态 blnAutoSendFlag = False blnReceiveFlag = False '接收初始化 intReceiveLen = 0 '默认发送方式为ASCII intOutMode = 0 Mbo.Text = "按ASCII码" '默认显示宽度位数为8 intHexWidth = 8 '默认各复选框处于选定状态 intHexChk = 1 intAsciiChk = 1 intAddressChk = 1 intAdd48Chk = 1 Main.chkAddress.Value = intAddressChk Main.chkAscii.Valu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度金融纠纷诉讼保全担保合同大全3篇
- 2025版选矿厂承包合同及矿山安全生产技术服务协议3篇
- 预制塑料梯形槽施工方案
- 通风窗更换施工方案
- 镇江交通标志施工方案
- 购销合同合同
- 全新融资合作协议合同下载
- 二零二五版水坝应急抢修与维修服务承包协议3篇
- 二零二五年度5G通信技术个人技术入股合同3篇
- 有关拍摄场地租赁合同范文
- 中医门诊病历
- 广西华银铝业财务分析报告
- 无违法犯罪记录证明申请表(个人)
- 电捕焦油器火灾爆炸事故分析
- 大学生劳动教育PPT完整全套教学课件
- 继电保护原理应用及配置课件
- 《杀死一只知更鸟》读书分享PPT
- 盖洛普Q12解读和实施完整版
- 2023年Web前端技术试题
- 品牌策划与推广-项目5-品牌推广课件
- DB31T 685-2019 养老机构设施与服务要求
评论
0/150
提交评论