VB编写的简单的上位机_第1页
VB编写的简单的上位机_第2页
VB编写的简单的上位机_第3页
VB编写的简单的上位机_第4页
VB编写的简单的上位机_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、一个简单的用 vb 编写的上位机程序自己想做一个简单上位机, 却发现相关的资料很少,于是在做出来之后把自己的这样一个简单程序上传,给新入门的朋友一个借鉴学习的资料。窗口布局用到的 mscomm 控件的基本属性(必用)commport :设置或返回串口通信的串口号(com 口)设置格式是: object.commport= value,其中 object 为控件名,如 mscomm1;value的合法值是整形的数据,例如1,2比如 mscomm1.commport=1(默认值),表示我们将用 com1 口进行通信com 口可以通过鼠标右击“我的电脑(计算机) ”图标,在弹出的快捷菜单中选择“管理

2、”,弹出如下图左边的对话框左键点击“设备管理器”可以看到上图右边对话框左键点击设备管理器可以看到如上图右边视图其中的就是当前连接到计算机的可用串口了settings :以字符串的形式设置或返回串口通信参数。设置格式:object.commport= value其中 object为控件名,如 mscomm1;value需为一字符串,由四个设置值组成其格式为:“波特率,奇偶校验,数据位,停止位”portopen : 设置或返回串口状态, 若其已经打开, 则返回 true, 否则返回 false设置格式: object.portopen= value其中 object 为控件名,如mscomm1;v

3、alue为一逻辑值,若为true,则打开,否则关闭注意:如果串口不是打开(关闭) ,而对它进行关闭(打开)将会出错rthreshold :该属性为一阀值。当接收缓冲区中字符数达到该值时,mscomm控件设置 commevent属性为 comevreceive, 并产生 oncomm事件。用户可在 oncomm事件处理程序中进行相应处理。若rthreshold属性设置为 0,则不产生 oncomm 事件。例如用户希望接收缓冲区中达到一个字符就接收一个字符,可将 rthreshold设置为 1。这样接收缓冲区中接收到一个字符,就产生一次oncomm事件。inputmode:设置或返回接收数据的类型

4、。设置格式: object.inputmode= value,其中 value的值有两个;cominputmodebinary 表示以二进制方式接收数据, input 属性返回值为一字符串cominputmodetext表示以字符方式接收数据,input 属性返回值为一字数组inputlen:设置或返回一次从接收缓冲区中读取字节数。设置格式: object. inputlen= value,value 为一个整形数据;当其设置为 0 时,表示一次读取接收缓冲区中的全部内容。这个属性在定字节读取中非常有用input:从接收缓冲区中读取数据并清空该缓冲区,该属性设计时无效,运行时只读。使用格式:

5、str=object.input,str为一字符串型变量或对象output:向发送缓冲区发送数据,该属性设计时无效,运行时只读。使用格式: object.output=value,value 为一字符串型变量或任意类型的数组commevent :这是一个非常重要的属性。该属性设计时无效,运行时只读。一旦串口发生通信事件或产生错误, 依据产生的事件和错误, mscomm控件为 commevent 属性赋不同的代码,同时产生oncomm 事件。用户程序就可在oncomm 事件处理程序中针对不同的代码, 进行相应的处理。comevreceive 接受到 rthreshold 个字符。该事件将持续产生

6、,直到用 input属性从接受缓冲区中读取并删除字符。其它的属性,需要时可以在网上查找相关资料(下面附几个网址)http:/ sub combo2_click()mscomm1.settings = combo2.text + ,n,8,1end subprivate sub command6_click()on error goto uerror:if command6.caption = 打开串口 thenmscomm1.portopen = truecommand6.caption = 关闭串口 shape1.fillcolor = rgb(0, 255, 0)elsemscomm1.p

7、ortopen = falsecommand6.caption = 打开串口 shape1.fillcolor = rgb(128, 128, 128)end ifexit subuerror:msg$ = 无效端口号 title$ = 串口调试 x = msgbox(msg$, 48, title$)end subprivate sub form_load()text1 = text3 = combo2.listindex = 0combo1.listindex = 0timer1.interval = val(text2)mscomm1.commport = combo1.listinde

8、x + 1mscomm1.rthreshold = 1mscomm1.inputlen = 0if option3 then mscomm1.inputmode = cominputmodebinaryend subprivate sub form_unload(cancel as integer)if mscomm1.portopen then mscomm1.portopen = falseend subprivate sub mscomm1_oncomm()dim bytreceived() as bytedim strbuff as stringselect casemscomm1.c

9、ommeventcasecomevreceiveif option4 thentext3 = text3 & mscomm1.inputelsestrbuff = mscomm1.inputbytreceived() = strbuffstrbuff = dim i as integerfor i = 0 to ubound(bytreceived)if len(hex(bytreceived(i)= 1 thenstrbuff = strbuff & 0 & hex(bytreceived(i)& elsestrbuff = strbuff & hex

10、(bytreceived(i)& end ifnexttext3 = text3 & strbuffend ifend selectend subprivate sub option3_click()mscomm1.inputmode = cominputmodebinaryend subprivate sub option4_click()mscomm1.inputmode = cominputmodetextend subprivate sub timer1_timer()dim sendbuff() as bytedim bytflag as stringdim strb

11、uff as stringdim i as integerdim flag as booleanif option2 thenmscomm1.output = text1.textelsefor i = 1 to len(ltrim(text1)bytflag = mid(text1, i, 1)if bytflag= 0 and bytflag = a and bytflag= a and bytflag = f thenflag = trueelseflag = falseexit forend ifnextif flag thenif len(ltrim(text1)mod 2 0 th

12、enstrbuff = 0 & ltrim(text1)elsestrbuff = text1end ifredim sendbuff(len(strbuff)/ 2 - 1)for i = 0 to ubound(sendbuff)sendbuff(i) = val(&h& mid(strbuff,2 * i + 1, 2)nextmscomm1.output = sendbuffelsemsg$ = 请输入 hex 格式数据: a-f、a-f、0-9title$ = 串口调试助手 x = msgbox(msg$, 48, title$)timer1.enabled

13、= falsecommand3.caption = 自动发送 end ifend ifend subprivate sub combo1_click()mscomm1.commport = combo1.listindex + 1end subprivate sub command1_click()text1 = end subprivate sub command2_click()dim sendbuff() as bytedim bytflag as stringdim strbuff as stringdim i as integerdim flag as booleantimer1.e

14、nabled = falseif mscomm1.portopen thenif option2 thenmscomm1.output = text1.textelsefor i = 1 to len(ltrim(text1)bytflag = mid(text1, i, 1)if bytflag = 0 and bytflag = a and bytflag = a and bytflag = f thenflag = trueelseflag = falseexit forend ifnextif flag thenif len(ltrim(text1)mod 2 0 thenstrbuf

15、f = 0 & ltrim(text1)elsestrbuff = text1end ifredim sendbuff(len(strbuff)/ 2 - 1)for i = 0 to ubound(sendbuff)sendbuff(i) = val(&h& mid(strbuff,2 * i + 1, 2)nextmscomm1.output = sendbuffelsemsg$= 请输入 hex 格式数据: a-f、a-f 、0-9title$ = 串口调试助手x = msgbox(msg$, 48, title$)end ifend ifelsemsg$ = 请打开串口 title$ = 串口调试助手x = msgbox(msg$, 48, title$)end ifend subprivate sub command3_click()if mscomm1.portopen thenif command3.caption = 自动发送 thentimer1.enabled = truetimer1.interval = val(

温馨提示

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

评论

0/150

提交评论