


免费预览已结束,剩余27页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
.北京科技大学课程设计(软件设计)报告班级:姓名:学号 : 指导教师:日期:年月日;.目录1 设计内容12 应用程序总体分析与设计13 应用程序各功能模块分析与设计23.1 连接服务器23.2 加项33.3 读取数据53.4 显示与隐藏73.5 暂停与继续93.6 报表与记录104 程序测试114.1 测试过程114.1.1 连接测试114.1.2 显示与隐藏测试114.1.3 暂停按钮114.1.4 数据保存124.2 测试结果124.2.1 连接结果124.2.2 显示与隐藏结果124.2.3 暂停显示134.2.4 数据保存结果135 程序文档145.1 使用说明145.2 程序说明146 学习收获26.1 设计内容opc(ole for process control又)称为过程控制的对象连接与嵌入技术。它是它的出现为基于 windows 的应用程序和现场过程控制应用建立了桥梁。在过去,为了存取现场设备的数据信息, 每一个应用软件开发商都需要编写专用的接口函数。由于现场设备的种类繁多,且产品的不断升级,往往给用户和软件开发商带来了巨大的工作负担。通常这样也不能满足工作的实际需要,系统集成商和开发商急切需要一种具有高效性、可靠性、开放性、可互操作性的即插即用的设备驱动程序。在这种情况下,opc标准应运而生。opc标准以微软公司的ole技术为基础, 它的制定是通过提供一套标准的ole/com 接口完成的, 在 opc技术中使用的是ole 2技术,ole标准允许多台微机之间交换文档、图形等对象。opc的出现大大缩短了软件开发的时间。本次课程设计就是要求使用vb 设计一个简单的客户端程序,这个程序能读取本机服务器上面的数据,并以直观的柱形图显示出来。另外,还应该添加一些别的功能。2 应用程序总体分析与设计为了能连接服务器,首先在 pc机运行模拟的服务器。然后,在 vb6.0 环境下,创建各种用于显示数据和控制的控件。给每一个控件添加事件响应函数。 程序运行的流程如下:开始连接服务器添加 item读取数据显示与保存退出.3 应用程序各功能模块分析与设计3.1 连接服务器首先使用 new 关键字创建一个opc对象,然后调用connect 方法连接到服务器。然后书写连接按钮的响应函数。private sub btnconnnect_click() 调用 connect 子程序call connect(opcj.sampleserver.1)end subsub connect(strprogid as string, optional strnode as string)if objserver is nothing then 建立一个opc服务器对象 set objserver = new opcserverend ifif objserver.serverstate = opcdisconnected then 连接 opc服务器objserver.connect strprogid, strnodeend ifif objgroups is nothing then 建立一个opc组集合set objgroups = objserver.opcgroupsend ifif objtestgrp is nothing then 添加一个opc组set objtestgrp = objgroups.add(testgrp) end ifend sub3.2 加项对 opc服务器进行访问前,必须先在opc组里添加要访问的opc标签。这里添加opc标签的标识符和数目是固定的,但是实际的opc应用程序往往要按照用户的指定或读取组态文件取得和处理需要添加opc标签。sub additem()dim stritemids(2) as string dim lclienthandles(2) as long dim lerrors() as longdim i as integerif objtestgrp is nothing then exit subend ifif not objitems is nothing then if objitems.count 0 thenexit sub end ifend if 设置组活动状态objtestgrp.isactive = true 取消组非同期通知objtestgrp.issubscribed = false 建立 opc项集合set objitems = objtestgrp.opcitems 生成标签的项标识符stritemids(1) = 反应罐温度 .valuestritemids(2) = 反应罐液位 .value lclienthandles(1) = 1lclienthandles(2) = 2 添加 opc项call objitems.additems(2, stritemids, _ lclienthandles, lserverhandles, lerrors)end subprivate sub btnadditem_click() 调用 additem 子程序call additemif not objtestgrp is nothing thenif objtestgrp.opcitems.count 0 then 启动定时器tmupdate.enabled = trueelse end ifend ifend sub3.3 读取数据为了实时显示数据,还要设置一个定时中断类型的服务函数,然后没间隔一段时间去向服务器读取一下数据。定时器会在加载项的时候启动。private sub tmupdate_timer() dim vtitemvalues() as variant dim lerrors() as longdim strbuf as string dim nwidth as integer dim nheight as integerdim ndrawheight as integer dim sglscale as singledim i as integer 同期读取syncread opccache, vtitemvalues, lerrors 棒图的表示for i = 1 to 4 数据的格式化if pauseflag(i) = 0 thenstrbuf = format(vtitemvalues(i), #.000) 表示数据字符串lbbar(i).caption = strbuf 计算棒的宽和高nwidth = picbar(i).scalewidth nheight = picbar(i).scaleheightsglscale = vtitemvalues(i) / 100 ndrawheight = cint(nheight * sglscale) 清除现棒图picbar(i).cls 绘制棒图if vtitemvalues(i) 90 then picbar(i).line (0, nheight - ndrawheight)-(nwidth, nheight), rgb(255, 0,0), bf else picbar(i).line (0, nheight - ndrawheight)-(nwidth, nheight), rgb(0, 255,0), bf end ifpicbar(i).line (0, nheight - ndrawheight)-(nwidth, nheight), rgb(vtitemvalues(i) * 2.55, 255 - vtitemvalues(i) * 2.55, 0), bfend ifnextif fileflag = 1 thenprint #fileno, date; time, str(vtitemvalues(1), str(vtitemvalues(1), str(vtitemvalues(1), str(vtitemvalues(1)end ifend sub3.4 显示与隐藏为了方便显示与隐藏其中某些项,这里给一个柱形图配置一个复选框,当复选框选中的时候,该项目便隐藏起来。取消复选框,该项目又会显示出来。这样子的功能对于只需要观察某些数据的时候,很是方便。具体实现方式如下:private sub check1_click()if check1.value = 1 then picbar(1).visible = false lbbar(1).visible = false label1.visible = falseelsepicbar(1).visible = true lbbar(1).visible = truelabel1.visible = true end ifend subprivate sub check2_click()if check2.value = 1 then picbar(2).visible = false lbbar(2).visible = false label2.visible = falseelsepicbar(2).visible = true lbbar(2).visible = truelabel2.visible = trueend ifend subprivate sub check3_click()if check3.value = 1 then picbar(3).visible = false lbbar(3).visible = false label3.visible = falseelsepicbar(3).visible = true lbbar(3).visible = truelabel3.visible = trueend if end subprivate sub check4_click()if check4.value = 1 then picbar(4).visible = false lbbar(4).visible = false label4.visible = falseelsepicbar(4).visible = true lbbar(4).visible = truelabel4.visible = trueend if end sub3.5 暂停与继续在实际生产环境中,遇到一些特殊情况需要暂停以观察数据,以防止被后面新的数据刷新。所以四个柱形图又都配置了一个暂停复选框,当复选框选中的时候,柱形图暂 停更新,当取消复选框,柱形图继续更新。实现代码如下:private sub check5_click()if check5.value = 1 then pauseflag(4) = 1elsepauseflag(4) = 0 end ifend subprivate sub check6_click()if check6.value = 1 then pauseflag(1) = 1elsepauseflag(1) = 0 end ifend subprivate sub check7_click()if check7.value = 1 then pauseflag(2) = 1elsepauseflag(2) = 0 end ifend subprivate sub check8_click()if check8.value = 1 then pauseflag(3) = 1elsepauseflag(3) = 0 end ifend sub其中的 pauseflag是个全局变量用于指示该柱形图的数据是不是暂停了,如果是, 在程序刷新的过程中则不对其刷新。3.6 报表与记录工业生产中,保留历史数据是必须的。为了分析一些特殊情况下的原因,必须要知道相关的数据,所以保留历史数据是很重要的部分。数据量大的时候可以使用数据库, 但是小数据量可以使用文件保存数据的方式。这里,使用文件的方式记录读取的数据。private sub check9_click()fileflag = check9.value if fileflag = 1 thenfileno = freefile()open c:userschengdesktopshunjun & str(year(now) & 年 & str(month(now) & 月 & str(day(now) & 日.txt for append as filenoelseclose fileno end ifend sub.当保存文件复选框选中的时候,打开今天所对应的文件,当取消选中的时候,关闭打开的文件。 如果文件打开, 相应的变量的状态会更改以指示当前需要向文件中写入数据,所以定时中断的时候,会向文件中写入数据。if fileflag = 1 thenprint #fileno, date; time, str(vtitemvalues(1), str(vtitemvalues(1),str(vtitemvalues(1), str(vtitemvalues(1)end if4 程序测试4.1 测试过程软件的测试主要包含连接测试,显示与隐藏测试,暂停测试和数据记录测试。4.1.1连接测试打开程序,点击运行。然后分别点击连接和加项按钮。图 4-1 连接测试4.1.2显示与隐藏测试在程序运行状态下,选中隐藏复选框。图 4-2 显示与隐藏4.1.3暂停按钮取消所有隐藏复选框,然后选中部分暂停按钮。如下图所示图 4-3 暂停4.1.4数据保存选中保存数据复选框,记录当前时间,过30 秒取消选中。图 4-4 保存数据4.2 测试结果4.2.1连接结果为了更方便的显示结果,显示的颜色会随着数据的变化而变化,数据越接近于100柱形图的颜色就越接近于红色。数据越接近于0,柱形图颜色越接近于绿色。图 4-5 数据显示测试4.2.2显示与隐藏结果勾选前两个隐藏复选框后,发现相关的内容隐藏了。图 4-6 显示与隐藏4.2.3暂停显示勾选暂停显示后,相应的柱形图不再同旁边的数据一起刷新了。图 4-7 暂停显示4.2.4数据保存结果当勾选保存数据复选框后,数据便被写入当天的文本文件。下图中左边一列记录的日期和时间,右边记录的是四个变量的数据。图 4-8 数据保存5 程序文档5.1 使用说明双击 shunjun 文件夹中的“工程1.exe”,程序开始执行。连接-加项,数据就能正常显示了。隐藏,暂停和保存数据复选框分别控制的是隐藏柱形图,柱形图暂停更新和保存文件。5.2 程序说明option base 1 option explicit建立 opc对象 -声明变量dim withevents objserver as opcserver dim objgroups as opcgroupsdim withevents objtestgrp as opcgroup dim objitems as opcitemsdim lserverhandles() as longdim pauseflag(1 to 4) as integer.dim fileflag as integer dim filenoprivate sub btnconnnect_click() 调用 connect 子程序call connect(opcj.sampleserver.1)end subprivate sub btnadditem_click() 调用 additem 子程序call additemif not objtestgrp is nothing thenif objtestgrp.opcitems.count 0 then 启动定时器tmupdate.enabled = trueelse end ifend ifend subprivate sub btnquit_click() 卸载窗体unload fmmainend subprivate sub check1_click()if check1.value = 1 then picbar(1).visible = false lbbar(1).visible = false label1.visible = falseelsepicbar(1).visible = true lbbar(1).visible = true label1.visible = trueend if end subprivate sub check2_click()if check2.value = 1 then picbar(2).visible = false lbbar(2).visible = false label2.visible = falseelsepicbar(2).visible = true lbbar(2).visible = truelabel2.visible = true end ifend subprivate sub check3_click()if check3.value = 1 then picbar(3).visible = false lbbar(3).visible = false label3.visible = falseelsepicbar(3).visible = true lbbar(3).visible = true label3.visible = trueend if end subprivate sub check4_click()if check4.value = 1 then picbar(4).visible = false lbbar(4).visible = false label4.visible = falseelsepicbar(4).visible = true lbbar(4).visible = true label4.visible = trueend if end subprivate sub check5_click()if check5.value = 1 then pauseflag(4) = 1elsepauseflag(4) = 0 end ifend subprivate sub check6_click()if check6.value = 1 then pauseflag(1) = 1elsepauseflag(1) = 0 end ifend subprivate sub check7_click()if check7.value = 1 then pauseflag(2) = 1elsepauseflag(2) = 0 end ifend subprivate sub check8_click()if check8.value = 1 then pauseflag(3) = 1elsepauseflag(3) = 0 end ifend subprivate sub check9_click()fileflag = check9.value if fileflag = 1 thenfileno = freefile()open c:userschengdesktopshunjun & str(year(now) & 年 & str(month(now) & 月 & str(day(now) & 日.txt for append as filenoelseclose fileno end ifend subprivate sub form_load() tmupdate.enabled = false tmupdate.interval = 1000end subsub connect(strprogid as string, optional strnode as string)if objserver is nothing then 建立一个 opc服务器对象set objserver = new opcserverend ifif objserver.serverstate = opcdisconnected then 连接 opc服务器objserver.connect strprogid, strnodeend ifif objgroups is nothing then 建立一个 opc组集合set objgroups = objserver.opcgroupsend ifif objtestgrp is nothing then 添加一个 opc组set objtestgrp = objgroups.add(testgrp)end ifend subsub additem()dim stritemids(4) as string dim lclienthandles(4) as long dim lerrors() as longdim i as integerif objtestgrp is nothing then exit subend ifif not objitems is nothing then if objitems.count 0 thenexit sub end ifend if 设置组活动状态objtestgrp.isactive = true 取消组非同期通知objtestgrp.issubscribed = false 建立 opc项集合set objitems = objtestgrp.opcitems 生成标签的项标识符stritemids(4) = tag4stritemids(1) = tag1 stritemids(2) = tag2 stritemids(3) = tag3 lclienthandles(4) = 4lclienthandles(1) = 1lclienthandles(2) = 2lclienthandles(3) = 3 添加 opc项call objitems.additems(4, stritemids, _ lclienthandles, lserverhandles, lerrors)end subsub disconnect() dim lerrors() as longif not objitems is nothing then if objitems.count 0 then 清除 opc项objitems.remove 4, lserverhandles, lerrors end ifset objitems = nothing end ifif not objtestgrp is nothing then 清除 opc组objgroups.remove testgrp set objtestgrp = nothingend ifif not objgroups is nothing then set objgroups = nothingend ifif not objserver is nothing thenif objserver.serverstate opcdisconnected then 断开 opc服务器 .objserver.disconnectend ifset objserver = nothing end ifend subprivate sub form_unload(cancel as integer) 调用 disconnect 子程序call disconnectclose filenoend subprivate sub tmupdate_timer()dim vtitemvalues() as variant dim lerrors() as longdim strbuf as string dim nwidth as integer dim nheight as integerdim ndrawheight as integer dim sglscale as singledim i as integer 同期读取syncread opccache, vtitemvalues, lerrors 棒图的表示for i = 1 to 4 数据的格式化if pauseflag(i) = 0 thenstrbuf = format(vtit
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高血压病人的饮食与护理
- 公司电工安全培训课件
- 了不起的0课件
- 《童年的发现》课件
- 颅脑损伤早期康复护理
- 公司水电安全培训课件
- 2025年标准安置房买卖合同范本
- 2025综合设备租赁合同书
- 透平式空压机启动讲解
- 月经期的个人护理
- 2025年中国保健电器具市场调查研究报告
- 电梯成品保护方案(3篇)
- 科技研发公司保密管理制度
- 医院科教相关管理制度
- 2025年江苏省农业融资担保有限责任公司招聘笔试参考题库附带答案详解
- 无锡信捷校招笔试题目及答案
- 2024第41届全国中学生物理竞赛预赛试题(含答案)
- (新版)中国心理卫生协会心理咨询师考试复习题库(浓缩400题)
- 南昌大学 2018-2019学年第二学期期中考试大学英语试卷
- 2025-2030中国妇科中成药市场营销趋势及投融资发展状况研究报告
- 艾滋病防治知识宣传
评论
0/150
提交评论