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



急问:关于com接收数据错误问题..........


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


急问:关于com接收数据错误问题..........[已结贴,结贴人:rose_886886]
发表于:2007-03-08 10:29:34 楼主

发送文本 :  11111111cr
正常asc值:49   49   49     49   49     49     49   49   67   82
接收asc值:49   49   177   49   177   49   177   49   67   82

当接收时  因为前边的数据都是相同  
第三位的数据自动在asc加了128   

为什么啊?......

如果发送文本不是连续相同  那么接收就正常..............

谢谢各位大虾的指点啊~~~~~~~~~~~~~~~~
发表于:2007-03-08 13:55:141楼 得分:0
...........................顶一下吧...................

这个问题很无奈...............................                 :(
发表于:2007-03-08 14:11:392楼 得分:0
我也帮顶一把
发表于:2007-03-08 14:30:503楼 得分:0
每次都一样么?换根线试一试   或者换下口子~
发表于:2007-03-08 15:00:094楼 得分:5
楼主能告诉我们,   发送数据的是什么程序?   接收数据的又是什么程序?

如果怀疑串口硬件有问题的话,   用网上现成的串口调试程序来做发送和接收,   这样能保证发送方程序和接收方程序本身没问题。

排除了串口硬件的问题可能性后,再排查发送程序和接收程序是否有问题。
发表于:2007-03-08 15:38:445楼 得分:0
恩  谢谢大家呀~~~~~~~~~~~~`  ^_^  超级感谢呀~~~~~~

发送是用  超级终端的

接受 是vb程序

private   sub   mscomm1_oncomm()

        dim   bbuffer()   as   byte
        dim   i   as   integer
   

        static   checki   as   integer
        select   case   me.mscomm1.commevent
                      case   comevreceive
                                bbuffer   =   me.mscomm1.input
                                for   i   =   0   to   ubound(bbuffer)
                                        bchar   =   bbuffer(i)
                                        select   case   bchar
                                                case   &h43                       'get   "c "
                                                        text1.text   =   text1.text   &   data
                                                        data   =   " "
                                                case   &h52
                                                        data   =   " "
                                                case   else      
                                                                data   =   data   &   cstr(bchar)
                                          end   select
                                next
        end   select

end   sub
发表于:2007-03-08 16:11:136楼 得分:15
你接收代码中未见mscomm控件属性inputmode的设置,按你代码看应为二进制接收:

option   explicit
dim   bchar
dim   data

private   sub   form_load()
        text1   =   " "
        mscomm1.commport   =   1
        mscomm1.portopen   =   true
        mscomm1.rthreshold   =   10
end   sub

private   sub   mscomm1_oncomm()
        dim   bbuffer()   as   byte
        dim   i   as   integer
        static   checki   as   integer
        select   case   me.mscomm1.commevent
        case   comevreceive
                me.mscomm1.inputmode   =   cominputmodebinary   '二进制接收
                bbuffer   =   me.mscomm1.input
                for   i   =   1   to   ubound(bbuffer)
                        bchar   =   bbuffer(i)
                        data   =   data   &   cstr(bchar)
                        text1.text   =   data
                next
        end   select
end   sub
发表于:2007-03-08 16:16:187楼 得分:0
刚才我没有粘贴  属性在这里...........


private   sub   form_load()
        'initial   the   mscomm 's   property
        me.mscomm1.commport   =   1
        me.mscomm1.rtsenable   =   true
        me.mscomm1.rthreshold   =   1
        me.mscomm1.inputmode   =   cominputmodebinary
        me.mscomm1.inputlen   =   15
        me.mscomm1.inbuffersize   =   15
        me.mscomm1.eofenable   =   true

        me.mscomm1.settings   =   "19200,e,8,2 "
        me.mscomm1.portopen   =   true

end   sub
发表于:2007-03-08 16:21:298楼 得分:0
在网上找个串口调试工具,   代替vb写的接收程序,   再测试一下。
猜测超级终端发送出来的数据可能有问题。
发表于:2007-03-08 16:29:079楼 得分:0
啊........谢谢你呀.......

不准下载东西的  秀峰大虾呀   就不能按你说的方法测了.........

好可惜  好不容易有个方法.........


让大家费心了呀......  继续求解中..........  :(
发表于:2007-03-08 18:20:2310楼 得分:0
根据你的全部代码,用串口调试精灵调试未见错误,估计你的超级终端有问题,给你一个经调试正常的简化串口调试精灵代码,用16进制编写命令于txtsend。
option   explicit
dim   inttime   as   integer
private   strsendtext   as   string         '发送文本数据
private   bytsendbyte()   as   byte         '发送二进制数据
private   blnreceiveflag   as   boolean
private   blnautosendflag   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
'字符表示的十六进制数转化为相应的整数,错误则返回     -1
function   converthexchr(str   as   string)   as   integer
        dim   test   as   integer
        test   =   asc(str)
        if   test   > =   asc( "0 ")   and   test   <=   asc( "9 ")   then
                test   =   test   -   asc( "0 ")
        elseif   test   > =   asc( "a ")   and   test   <=   asc( "f ")   then
                test   =   test   -   asc( "a ")   +   10
        elseif   test   > =   asc( "a ")   and   test   <=   asc( "f ")   then
                test   =   test   -   asc( "a ")   +   10
        else
                test   =   -1   '出错信息
        end   if
        converthexchr   =   test
end   function

'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
function   strhextobytearray(strtext   as   string,   bytbyte()   as   byte)   as   integer
        dim   hexdata   as   integer                     '十六进制(二进制)数据字节对应值
        dim   hstr   as   string   *   1                     '高位字符
        dim   lstr   as   string   *   1                     '低位字符
        dim   highhexdata   as   integer             '高位数值
        dim   lowhexdata   as   integer               '低位数值
        dim   hexdatalen   as   integer               '字节数
        dim   stringlen   as   integer                 '字符串长度
        dim   account   as   integer
        dim   n   as   integer
        '计数
        'txtsend   =   " "                                       '设初值
        hexdatalen   =   0
        strhextobytearray   =   0
        stringlen   =   len(strtext)
        account   =   stringlen   \   2
        redim   bytbyte(account)
        for   n   =   1   to   stringlen
                do                                                                                             '清除空格
                        hstr   =   mid(strtext,   n,   1)
                        n   =   n   +   1
                        if   (n   -   1)   >   stringlen   then
                                hexdatalen   =   hexdatalen   -   1
                                exit   for
                        end   if
                loop   while   hstr   =   "   "
                do
                        lstr   =   mid(strtext,   n,   1)
                        n   =   n   +   1
                        if   (n   -   1)   >   stringlen   then
                                hexdatalen   =   hexdatalen   -   1
                                exit   for
                        end   if
                loop   while   lstr   =   "   "
                n   =   n   -   1
                if   n   >   stringlen   then
                        hexdatalen   =   hexdatalen   -   1
                        exit   for
                end   if
                highhexdata   =   converthexchr(hstr)
                lowhexdata   =   converthexchr(lstr)
               
                if   highhexdata   =   -1   or   lowhexdata   =   -1   then           '遇到非法字符中断转化
                        hexdatalen   =   hexdatalen   -   1
                        exit   for
                else
                        hexdata   =   highhexdata   *   16   +   lowhexdata
                        bytbyte(hexdatalen)   =   hexdata
                        hexdatalen   =   hexdatalen   +   1
                end   if
        next   n
        if   hexdatalen   >   0   then                                                             '修正最后一次循环改变的数值
                hexdatalen   =   hexdatalen   -   1
                redim   preserve   bytbyte(hexdatalen)
        else
                redim   preserve   bytbyte(0)
        end   if
        if   stringlen   =   0   then                                                               '如果是空串,则不会进入循环体
                strhextobytearray   =   0
        else
                strhextobytearray   =   hexdatalen   +   1
        end   if
end   function

代码长,接下一答复。
发表于:2007-03-08 18:21:3411楼 得分:0
接前答复:
private   sub   cmdmanualsend_click()
        if   not   me.mscomm.portopen   then
                me.mscomm.commport   =   intport
                me.mscomm.settings   =   strset
                me.mscomm.portopen   =   true
        end   if
                call   ctrtimer_timer
        if   not   blnautosendflag   then
                me.mscomm.portopen   =   false
        end   if
end   sub
private   sub   cmdautosend_click()
        if   blnautosendflag   then
                me.ctrtimer.enabled   =   false
                if   not   blnreceiveflag   then
                        me.mscomm.portopen   =   false
                end   if
                me.cmdautosend.caption   =   "自动发送 "
        else
                if   not   me.mscomm.portopen   then
                        me.mscomm.commport   =   intport
                        me.mscomm.settings   =   strset
                        me.mscomm.portopen   =   true
                end   if
                me.ctrtimer.interval   =   inttime
                me.ctrtimer.enabled   =   true
                me.cmdautosend.caption   =   "停止发送 "
        end   if
        blnautosendflag   =   not   blnautosendflag
end   sub

private   sub   ctrtimer_timer()
        dim   longth   as   integer
        strsendtext   =   me.txtsend.text
        longth   =   strhextobytearray(strsendtext,   bytsendbyte())
        if   longth   >   0   then
                me.mscomm.output   =   bytsendbyte
        end   if
end   sub
'输入处理,处理接收到的字节流,并保存在全局变量
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
                txtasc   =   strascii   'ascii
                txthex   =   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
                txtadd   =   straddress   '地址
end   sub
private   sub   cmdreceive_click()
        if   blnreceiveflag   then
                if   not   blnreceiveflag   then
                        me.mscomm.portopen   =   false
                end   if
                me.cmdreceive.caption   =   "开始接收 "
        else
                if   not   me.mscomm.portopen   then
                        me.mscomm.commport   =   intport
                        me.mscomm.settings   =   strset
                        me.mscomm.portopen   =   true
                end   if
                me.mscomm.inputlen   =   0
                me.mscomm.inputmode   =   0
                me.mscomm.inbuffercount   =   0
                me.mscomm.rthreshold   =   1
                me.cmdreceive.caption   =   "停止接收 "
        end   if
        blnreceiveflag   =   not   blnreceiveflag
end   sub

private   sub   form_load()
        inthexwidth   =   8
        txtadd   =   " "
        txthex   =   " "
        txtasc   =   " "
        txtsend   =   " "
        txtadd.width   =   1335
        txthex.width   =   2535
        txtasc.width   =   1215
        '设置默认发送接收关闭状态
        blnautosendflag   =   false
        blnreceiveflag   =   false
        '接收初始化
        intreceivelen   =   0
        '默认发送方式为16进制
        'intoutmode   =   1
          '初始化串行口
        intport   =   2
        inttime   =   1000
        strset   =   "19200,e,8,2 "
        me.mscomm.inbuffersize   =   1024
        me.mscomm.outbuffersize   =   512
        if   not   me.mscomm.portopen   then
                me.mscomm.commport   =   intport
                me.mscomm.settings   =   strset
                me.mscomm.portopen   =   true
        end   if
        me.mscomm.portopen   =   false
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   mscomm_oncomm()
        dim   bytinput()   as   byte
        dim   intinputlen   as   integer
        select   case   me.mscomm.commevent
                case   comevreceive
                        if   blnreceiveflag   then
                                if   not   me.mscomm.portopen   then
                                        me.mscomm.commport   =   intport
                                        me.mscomm.settings   =   strset
                                        me.mscomm.portopen   =   true
                                end   if
                                '此处添加处理接收的代码
                                me.mscomm.inputmode   =   cominputmodebinary   '二进制接收
                                intinputlen   =   me.mscomm.inbuffercount
                                redim   bytinput(intinputlen)
                                bytinput   =   me.mscomm.input
                                call   inputmanage(bytinput,   intinputlen)
                                call   getdisplaytext
                                'call   display
                                if   not   blnreceiveflag   then
                                        me.mscomm.portopen   =   false
                                end   if
                        end   if
        end   select
end   sub

private   sub   display()
        txthex   =   " "
        txtasc   =   " "
        txtadd   =   " "
end   sub
发表于:2007-03-08 18:22:5612楼 得分:0
2段代码,放一个窗体的代码窗口。
发表于:2007-03-09 09:04:5313楼 得分:0
超级感谢   zdingyun大哥哦~~~~~~``  ^_^

刚看到信息  先回复下  等我去测试下哈~~~~~~~~~~~`

今天好心情呀~~~~~~~~~~~
发表于:2007-03-09 18:37:1214楼 得分:0
呵呵   很受用,多谢啦!
顺便顶一下。。。
发表于:2007-06-14 11:33:3015楼 得分:0
顶一下。。。


快速检索

最新资讯
热门点击