您的位置:程序门 -> vb -> 资源



mscomm二进制形式接收数据时的中文显示问题


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


mscomm二进制形式接收数据时的中文显示问题[已结贴,结贴人:yym_808]
发表于:2007-10-19 21:23:27 楼主
mscomm1.rthreshold   =   1
mscomm1.inputmode   =   cominputbinary
发送中文时的代码为:  
    dim   byte()   as   byte
   
    byte   =   strconv(txtsend.text,vbfromunicode)
    mscomm1.output   =   byte

在mscomm_oncomm的comevreceive事件中接收:  
    dim   buffer   as   variant
    dim   inbyte()     as   byte

    buffer   =   mscomm1.input
    inbyte   =   buffer

    txtrev.text   =   txtrev.text   &   strconv(inbyte,vbunicode)

如果是以上的接收方式的话,则在接收文本框内能显示发送的中文,但是现在由于串口还要接收其它的内容,因此把接受的程序改成:
    dim   indata   as   integer

    buffer   =   mscomm1.input
    inbyte   =   buffer
    for   i=0   to   ubound(inbyte)
            indata   =   inbyte(i)
            call   mscomm1_receive(indata)
    next   i

请问各位这里的mscomm1_receive(byval   indata   as   integer)因该怎样写才能在文本框上显示接收到的中文呢?
发表于:2007-10-20 19:40:291楼 得分:0
关注
发表于: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
发表于:2007-10-21 20:28:163楼 得分:0
请问timer的作用是什么呢?ch()又是什么意思呢?
还烦请zdingyun解释下哦
发表于:2007-10-22 08:30:374楼 得分:0
ch()函数的作用是将16进制转换成汉字。
由于mscomm在接收数据时的oncomm事件会发生多次,直接调用ch()函数会使见到的汉字不连贯。如汉字数据是“如果是以上的接收方式的话”,直接调用ch()函数,在txtrev文本框内显示“如果是以如果是以上的接收如果是以上的接收方式的话”,不符合要求。
所以采用timer来控制读取正确的数据“如果是以上的接收方式的话”。
发表于:2007-10-22 09:08:175楼 得分:5
dim   lastbyte   as   integer

private   sub   mscomm1_receive(byval   indata   as   integer)
dim   mychar   as   integer
    if   lastbyte   then  
          mychar   =   lastbyte   *   256   +   indata
          lastbyte   =   0
          txtrev   =   txtrev   &   chr(mychar)
    else
          if   indata   >   127   or   indata   <   0   then
                lastbyte   =   indata
          else
                txtrev   =   txtrev   &   chr(indata)
          end   if
    end   if
end   sub
发表于:2007-10-25 16:36:336楼 得分:0
多谢各位,我已经解决了,代码是这样写的:
option   explicit

dim   ch_byte()   as   byte
dim   ch   as   integer

sub   mscomm1_receive(byval   indata   as   integer)
        ...
        if   ch=0   then
                if   indata> 128   then
                      ch_byte(0)   =   indata
                      ch   =   ch+1
                else
                      txtrev.text   =   txtrev.text   &   chr(indata)
                end   if
        else
                  ch_byte(1)   =   indata
                  txtrev.text   =   txtrev.text   &   strconv(ch_byte,vbunicode)
                  ch   =   0
        end   if
end   sub


快速检索

最新资讯
热门点击