您的位置:程序门 -> vb -> 控件



还是串口接收数据的问题,谁在线 帮帮忙吧,急 实在没办法了。


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


还是串口接收数据的问题,谁在线 帮帮忙吧,急 实在没办法了。
发表于:2007-02-28 11:30:02 楼主
是这样的,例如串口以十六进制向我发送一个字节 "0a ",我用下面这段程序转换之后却不是 "0a ",我不知道错在哪里了。请高手指点。
    public   function   receive()   as   string  
              dim   m_strreceive()   as   byte
                do   while   mainform.comm.inbuffercount   =   0  
                        doevents
                  loop
                  m_strreceive   =   mscomm.input
                  receive=change_r(m_strreceive)
    end   function          

    public   function   change_r(   strs   as   variant)
            result()   as   byte
          result=strs
          for   i=lbound(result)   to   ubound(result)
                change_r=chang_r+str(result(i)+32)
          next   i
      end   function  
发表于:2007-02-28 11:40:041楼 得分:0
好像不用这样转换吧
发表于:2007-02-28 12:06:272楼 得分:0
其实就是怎样将收到的type数组还原为十六进制数
发表于:2007-02-28 14:15:263楼 得分:0
option   explicit
private   blnreceiveflag   as   boolean
private   intport   as   integer
private   strset   as   string
private   intreceivelen   as   integer
private   bytreceivebyte()   as   byte
private   strascii   as   string                       '设置初值
private   strhex   as   string
private   inthexwidth   as   integer
private   intline   as   integer
private   m   as   integer
private   straddress   as   string
'输入处理,处理接收到的字节流,并保存在全局变量
private   sub   inputmanage(bytinput()   as   byte,   intinputlenth   as   integer)
        dim   n   as   integer   '定义变量及初始化
        redim   preserve   bytreceivebyte(intreceivelen   +   intinputlenth)
        for   n   =   1   to   intinputlenth   step   1
                bytreceivebyte(intreceivelen   +   n   -   1)   =   bytinput(n   -   1)
        next   n
        intreceivelen   =   intreceivelen   +   intinputlenth
end   sub

'为输出准备文本,保存在全局变量
'总行数保存在intline
public   sub   getdisplaytext()
        dim   n   as   integer
        dim   intvalue   as   integer
        dim   inthighhex   as   integer
        dim   intlowhex   as   integer
        dim   strsinglechr   as   string   *   1
        dim   intaddress   as   integer
        dim   intaddressarray(8)   as   integer
        dim   inthighaddress   as   integer
        strascii   =   " "                         '设置初值
        strhex   =   " "
        straddress   =   " "
        '获得16进制码和ascii码的字符串
        for   n   =   1   to   intreceivelen
                intvalue   =   bytreceivebyte(n   -   1)
                if   intvalue   <   32   or   intvalue   >   128   then       '处理非法字符
                        strsinglechr   =   chr(46)                                 '对于不能显示的ascii码,
                else                                                                             '用 ". "表示
                        strsinglechr   =   chr(intvalue)
                end   if
                strascii   =   strascii   +   strsinglechr
                inthighhex   =   intvalue   \   16
                intlowhex   =   intvalue   -   inthighhex   *   16
                if   inthighhex   <   10   then
                        inthighhex   =   inthighhex   +   48
                else
                        inthighhex   =   inthighhex   +   55
                end   if
                if   intlowhex   <   10   then
                        intlowhex   =   intlowhex   +   48
                else
                        intlowhex   =   intlowhex   +   55
                end   if
                strhex   =   strhex   +   "   "   +   chr$(inthighhex)   +   chr$(intlowhex)
                if   (n   mod   inthexwidth)   =   0   then
                        strascii   =   strascii   +   chr$(13)   +   chr$(10)
                        strhex   =   strhex   +   chr$(13)   +   chr$(10)
                else
                end   if
        next   n
                text3   =   strascii   'ascii
                text2   =   strhex   '16进制
        '获得地址字符串
        intline   =   intreceivelen   \   inthexwidth
        if   (intreceivelen   -   inthexwidth   *   intline)   >   0   then
                intline   =   intline   +   1
        end   if
        '设置换行
        for   n   =   1   to   intline
                intaddress   =   (n   -   1)   *   inthexwidth
                inthighaddress   =   8
                intaddressarray(0)   =   intaddress
                for   m   =   1   to   inthighaddress
                        intaddressarray(m)   =   intaddressarray(m   -   1)   \   16
                next   m
                for   m   =   1   to   inthighaddress
                        intaddressarray(m   -   1)   =   intaddressarray(m   -   1)   -   intaddressarray(m)   *   16
                next   m
                for   m   =   1   to   inthighaddress
                        if   intaddressarray(inthighaddress   -   m)   <   10   then
                                intaddressarray(inthighaddress   -   m)   =   intaddressarray(inthighaddress   -   m)   +   asc( "0 ")
                        else
                                intaddressarray(inthighaddress   -   m)   =   intaddressarray(inthighaddress   -   m)   +   asc( "a ")   -   10
                        end   if
                        straddress   =   straddress   +   chr$(intaddressarray(inthighaddress   -   m))
                next   m
                straddress   =   straddress   +   chr$(13)   +   chr$(10)
        next   n
                text1   =   straddress   '地址
end   sub
private   sub   cmdreceive_click()
        if   blnreceiveflag   then
                if   not   blnreceiveflag   then
                        me.mscomm1.portopen   =   false
                end   if
                me.cmdreceive.caption   =   "开始接收 "
        else
                if   not   me.mscomm1.portopen   then
                        me.mscomm1.commport   =   intport
                        me.mscomm1.settings   =   strset
                        me.mscomm1.portopen   =   true
                end   if
                me.mscomm1.inputlen   =   0
                me.mscomm1.inputmode   =   0
                me.mscomm1.inbuffercount   =   0
                me.mscomm1.rthreshold   =   1
                me.cmdreceive.caption   =   "停止接收 "
        end   if
        blnreceiveflag   =   not   blnreceiveflag
end   sub

private   sub   form_load()
        inthexwidth   =   8
        text1   =   " "
        text2   =   " "
        text3   =   " "
        text1.width   =   1335
        text2.width   =   2535
        text3.width   =   1215
        strset   =   "9600,n,8,1 "
        intport   =   1
        mscomm1.commport   =   intport
        mscomm1.settings   =   strset
end   sub

private   sub   cmdclear_click()
        dim   byttemp(0)   as   byte
        redim   bytreceivebyte(0)
        intreceivelen   =   0
        call   inputmanage(byttemp,   0)
        call   getdisplaytext
        call   display
end   sub

private   sub   mscomm1_oncomm()
        dim   bytinput()   as   byte
        dim   intinputlen   as   integer
        select   case   me.mscomm1.commevent
                case   comevreceive
                        if   blnreceiveflag   then
                                if   not   me.mscomm1.portopen   then
                                        me.mscomm1.commport   =   intport
                                        me.mscomm1.settings   =   strset
                                        me.mscomm1.portopen   =   true
                                end   if
                                '此处添加处理接收的代码
                                me.mscomm1.inputmode   =   cominputmodebinary   '二进制接收
                                intinputlen   =   me.mscomm1.inbuffercount
                                redim   bytinput(intinputlen)
                                bytinput   =   me.mscomm1.input
                                call   inputmanage(bytinput,   intinputlen)
                                call   getdisplaytext
                                'call   display
                                if   not   blnreceiveflag   then
                                        me.mscomm1.portopen   =   false
                                end   if
                        end   if
        end   select
end   sub

private   sub   display()
        text2   =   " "
        text3   =   " "
        text1   =   " "
end   sub


快速检索

最新资讯
热门点击