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



串口通讯遇到接收问题(接收十六进制编码).


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


串口通讯遇到接收问题(接收十六进制编码).
发表于: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
发表于:2007-04-23 09:32:231楼 得分:0
用16进制表示数据,其16进制数的字符范围为00-ff,每两字符0-f表示一接收字节:
private   sub   mscomm1_oncomm()
        on   error   resume   next
        dim   bytesreceived()   as   byte
        dim   buffer   as   string
        dim   hdata   as   string
        dim   i   as   integer
        select   case   mscomm1.commevent
                case   comevreceive                               '接收十六进制数据。并以十六进制显示
                        mscomm1.inputlen   =   0
                        mscomm1.inputmode   =   cominputmodebinary     '设置当前以二进制数接收数据
                        buffer   =   mscomm1.input                                     '接收数据至字符串中
                        bytesreceived()   =   buffer                                 '将数据转入byte中
                        for   i   =   0   to   ubound(bytesreceived)             '显示结果以十六进制显示
                                if   len(hex(bytesreceived(i)))   =   1   then
                                        hdata   =   hdata   &   "0 "   &   hex(bytesreceived(i))
                                else
                                        hdata   =   hdata   &   hex(bytesreceived(i))
                                end   if
                                txtreceive.text   =   hdata               '最后将结果后入txtreceive中
                                mscomm1.outbuffercount   =   0             '清除发送缓冲区
                                mscomm1.inbuffercount   =   0               '清除接收缓冲区
                        next
        end   select
end   sub
发表于:2007-04-23 11:03:172楼 得分:0
private   sub   cmdrechex_click()
on   error   goto   handler
dim   bytesreceived()   as   byte
        dim   receivelen       as   byte
        dim   inbuffer()       as   byte
        dim   hdata   as   string
        dim   i   as   integer
        select   case   mscomm1.commevent
                case   comevreceive
                mscomm1.inputlen   =   0
                receivelen   =   mscomm1.inbuffercount
                redim   inbuffer(receivelen   -   1)   as   byte
                inbuffer   =   mscomm1.input
                        bytesreceived()   =   inbuffer                                 '将数据转入byte中
                        for   i   =   0   to   ubound(bytesreceived)             '显示结果以十六进制显示
                                if   len(hex(bytesreceived(i)))   =   1   then
                                        hdata   =   hdata   &   "0 "   &   hex(bytesreceived(i))
                                else
                                        hdata   =   hdata   &   hex(bytesreceived(i))
                                end   if
                                txtreceive.text   =   hdata
                                mscomm1.inbuffercount   =   0               '清除接收缓冲区
                        next
        end   select
handler:
        if   err.number   =   9   then   resume   next
end   sub


快速检索

最新资讯
热门点击