您的位置:程序门 -> vb -> 基础类



发送和接收十六进数.发00h接收不到


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


发送和接收十六进数.发00h接收不到[已结贴,结贴人:ywk41]
发表于:2008-01-17 16:03:35 楼主
我的也是同样问题,是发送和接收十六进数.发00h接收不到!  
代码如下:  
private       sub       command1_click() '发送数据  
    dim       buffer                       as       variant  
    dim       strsend                   as       string  
    dim       sj()       as       byte  
    dim       i       as       integer  
redim       sj(len(text1.text)       /       2       -       1)  
                for       i       =       0       to       len(text1.text)       -       1       step       2  
                            sj(i       /       2)       =       val("&h"       &       mid(text1.text,       i       +       1,       2))  
                next  
                mscomm1.output       =       sj  

                                strdata       =       ""  
   
end       sub  


private       sub       mscomm1_oncomm()  

'...通讯事件发生  

dim       indata       as       variant  

dim       bte(0)       as       byte  

dim       intinputlen       as       integer  

dim       i       as       integer  


select       case       mscomm1.commevent  

case       comevreceive       '...有接受事件发生  

            '此处添加处理接收的代码  
                                                me.mscomm1.inputmode       =       cominputmodebinary       '二进制接收  
                                                intinputlen       =       me.mscomm1.inbuffercount  
                                                redim       bytinput(intinputlen)  
                                                bytinput       =       me.mscomm1.input  
                                                'jieshou  
                                                for       i       =       0       to       ubound(bytinput)  
                                                            if       len(hex(bytinput(i)))       =       1       then  
                                                                            strdata       =       strdata       &       "0"       &       hex(bytinput(i))       '&       "       "       '建议       &       "       "       不要用  
                                                            else  
                                                                            strdata       =       strdata       &       hex(bytinput(i))       '&       "       "       '建议       &       "       "       不要用  
                                                            end       if  
                                                next  
                                text2.text       =       strdata  


end       select  
                                                mscomm1.inbuffercount       =       0  
                                                mscomm1.outbuffercount       =       0  

end       sub  
发表于:2008-01-18 09:43:531楼 得分:5
贴子中的代码存在错误,不能接收.  
1.       bte(0)是何变量名,接收代码中用的是bytinput()byte动态数组      
2.       mscomm1属性rthreshold必需>   0,方能产生oncomm事件  
3.       必需注意程序中变量的准确定义  
代码修改如下:      
vbscript code
option explicit dim sj() as byte dim strdata as string private sub command1_click() '发送数据 dim buffer as variant dim strsend as string dim i as integer redim sj(len(text1.text) / 2 - 1) for i = 0 to len(text1.text) - 1 step 2 sj(i / 2) = val("&h" & mid(text1.text, i + 1, 2)) next mscomm1.output = sj strdata = "" end sub private sub form_load() mscomm1.settings = "9600,n,8,1" mscomm1.rthreshold = 1 mscomm1.portopen = true text1 = "001234567890ff00" end sub private sub mscomm1_oncomm() '通讯事件发生 dim indata as variant dim bytinput() as byte dim intinputlen as integer dim i as integer select case mscomm1.commevent case comevreceive '...有接受事件发生 '此处添加处理接收的代码 mscomm1.inputmode = cominputmodebinary '二进制接收 intinputlen = mscomm1.inbuffercount redim bytinput(intinputlen) bytinput = mscomm1.input 'jieshou for i = 0 to ubound(bytinput) if lenhex(bytinput(i))) = 1 then strdata = strdata & "0" & hex(bytinput(i)) else strdata = strdata & hex(bytinput(i)) end if next text2.text = strdata end select mscomm1.inbuffercount = 0 mscomm1.outbuffercount = 0 end sub


快速检索

最新资讯
热门点击