| 发表于:2007-04-22 23:07:31 楼主 |
我在使用mscomm通讯控件以十六进制码发送没问题,但在写接收时,有问题:对于数字正常,但对于接收 "a-f "的十六制数时,不正常. private sub form_load() dim instring as string ' 使用 com1。 mscomm1.commport = 2 ' 9600 波特,无奇偶校验,8 位数据,一个停止位。 mscomm1.settings = "9600,n,8,1 " ' 当输入占用时, ' 告诉控件读入整个缓冲区。 mscomm1.inputlen = 0 ' 打开端口。 mscomm1.portopen = true end sub '以下过程为发送十六进制编码过程 '请高手写出以下的过程的反过程,也就是接收十六进制编码 private sub cmdoutput_click() dim outbuffer() as byte dim tem as variant dim e as integer dim q as integer dim lenoftext as integer lenoftext = len(txtsum.text) \ 2 - 1 redim outbuffer(lenoftext) if mscomm1.portopen = false then mscomm1.portopen = true end if q = 0 for e = 1 to len(txtsum.text) step 2 tem = mid(txtsum.text, e, 2) outbuffer(q) = val( "&h " & tem) debug.print val( "&h " & tem) q = q + 1 next mscomm1.output = outbuffer mscomm1.portopen = false 'mscomm1.output end sub '以下过程想写接收十六进制编码过程,但对于 'a-f '的编码不正常.请高手指点. private sub cmdrechex_click() 'on error goto hander dim e as integer dim receivelen as byte dim inbuffer() as byte receivelen = mscomm1.inbuffercount redim inbuffer(receivelen - 1) as byte if mscomm1.portopen = false then mscomm1.portopen = true end if inbuffer = mscomm1.input dim i as byte dim strcontent as string for i = 0 to 2 * (receivelen - 1) + 1 debug.print inbuffer(i) strcontent = strcontent + hex(inbuffer(i)) next i txtinput.text = strcontent mscomm1.inbuffercount = 0 'handler: 'if err.number = 9 then resume next end sub |
|
|
|
|