| 发表于:2007-10-21 12:03:212楼 得分:15 |
option explicit private declare sub sleep lib "kernel32" (byval dwmilliseconds as long) dim buffer() as byte dim lbljieshou as string dim leninput as string dim strdata as string dim strdata1 as string dim j as integer dim k as integer private sub command2_click() ch end sub private sub form_load() mscomm1.commport = 1 'com1串口 mscomm1.settings = "9600,n,8,1" '串口设置 mscomm1.inputmode = cominputmodebinary '采用二进制传输 mscomm1.inbuffercount = 0 '清空接受缓冲区 mscomm1.outbuffercount = 0 '清空传输缓冲区 mscomm1.inbuffersize = 512 '接收缓冲区大小 mscomm1.outbuffersize = 512 '发送缓冲区大小 mscomm1.rthreshold = 1 '产生mscomm事件 mscomm1.portopen = true '开串口 timer1.interval = 200 '200ms timer1使strdata清空 txtrev = "" end sub private sub mscomm1_oncomm() dim intinputlen as integer text1 = "" strdata1 = "" select case mscomm1.commevent case comevreceive mscomm1.inputlen = 0 intinputlen = mscomm1.inbuffercount redim buffer(intinputlen) buffer() = mscomm1.input '数据处理代码 call receive end select end sub public sub receive() dim i as integer for i = 0 to ubound(buffer) if len(hex(buffer(i))) = 1 then strdata = strdata & "0" & hex(buffer(i)) else strdata = strdata & hex(buffer(i)) end if next end sub private sub timer1_timer() command2_click strdata = "" end sub public function ch() dim j as integer for j = 1 to len(strdata) step 4 strdata1 = strdata1 & chr(val("&h" & mid(strdata, j, 4))) next j txtrev = strdata1 end function | | |
|