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



关于串口调试精灵与vb6的问题


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


关于串口调试精灵与vb6的问题
发表于:2007-01-05 08:26:16 楼主


 

串口设置:
强制m100   –off:02     30   30   37   31   30   34   36   34   03   39   39
强制m100-on:     02     30   30   37   30   30   34   36   34   03   39   38
 
所应该注意的是:命令后不可加h且应点选串口调试精灵“十六进制发送”

此上是我用串口调试精灵发送命令到plc(可编程控制器)的命令格式;
并调试成功。

后来我用vb6的mscomm控件做上位机,用mscomm发送,不知何故,调试不了。

vb程序如下:其他参数都设定正确。

mscomm1.output= "02     30   30   37   31   30   34   36   34   03   39   39 "

mscomm1.output= "02&h     30&h   30&h   37&h   31&h   30&h   34&h   36&h   34&h   03&h   39&h   39&h "

请问各位高手,串口调试精灵中的16进制发送,用mscomm.output如何写?


plc-vb调试程序:
 


发表于:2007-01-05 23:01:361楼 得分:0
一般这类工具的“十六进制发送”方式都是把普通字符串十六进制数据(如12   34)分字节按高低位转化为十进制的数据(如18   52)然后到一个byte数组里再进行发送的。。。
例子在网上很多。。。
发表于:2007-01-09 10:45:292楼 得分:0
public   function   sendhexdatastring()   as   boolean
        dim   senddata   as   string
        dim   buf()   as   byte
        dim   m,   n,   p   as   long
        dim   i,   j,   k   as   long,   cs
       
        on   error   resume   next

        sendhexdatastring   =   false
        senddata   =   "029c "
       
        'senddata   =   strsenddata             'trim(text2.text)     '½«òª•¢ëíµäîä±¾¿òäúµä¿õ¸ñ襳ý£¬²¢çò¸³¸øsenddata
        if   frmmain.mscomm1.portopen   =   false   then
                frmmain.mscomm1.portopen   =   true
                'msgbox   "çë´ò¿ª´®¿ú ",   vbokonly,   "´íîó "
                'exit   function
        end   if


        if   len(senddata)   =   0   then     'èç¹ûîä±¾¿òäúã»óðêý¾ý
                msgbox   "çëêäèëêý¾ý ",   vbokonly,   "´íîó "
                exit   function
        end   if


        'if   check2.value   =   0   then     'èç¹ûã»óðñ¡ôñêç•ñòô16½øöæ•¢ëí
                'p   =   len(senddata)
                'redim   buf(p   -   1)   as   byte
                'for   i   =   1   to   p
                '         buf(i   -   1)   =   asc(mid(senddata,   i,   1))   '½«ã¿¸ö×ö•ûòôascâë•¢ëí
                'next   i
        'else         'èç¹ûñ¡ôñòô16½øöæ•¢ëí
                senddata   =   replace(senddata,   "   ",   " ")
                senddata   =   replace(senddata,   vbcrlf,   " ")
                p   =   len(senddata)   \   2   -   1
                redim   buf(p)   as   byte
                for   i   =   0   to   p
                        buf(i)   =   clng( "&h "   &   mid(senddata,   i   *   2   +   1,   2))   'òô16½øöæêý•¢ëí
                next   i
        'end   if

        'í¨¹ý´®¿ú•¢ëíêý¾ý
        frmmain.mscomm1.inputmode   =   cominputmodebinary
        frmmain.mscomm1.output   =   buf()
       
        'sleep   (100)
        'procgetdata
       
        sendhexdatastring   =   true
       
        exit   function

'³ö´í´¦àí
doerr:
        if   err.number   =   13   then
                msgbox   "êý¾ý¸ñê½²»¶ô ",   vbokonly,   "´íîó "
        else
                msgbox   err.description
        end   if
end   function
发表于:2007-01-09 10:47:123楼 得分:0
乱码是中文注释,不影响的
第1种是发送ascii,第2种是发送hex
发表于:2007-01-13 00:37:424楼 得分:0
'**********************************
'字符表示的十六进制数转化为相应的整数
'错误则返回     -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                     '计数
               
        strtestn   =   " "                                       '设初值
        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-01-13 00:38:095楼 得分:0
把十六进制转换后发送
发表于:2007-01-13 21:06:576楼 得分:0
用byte数组就行了,不用搞很复杂。
发表于:2007-01-17 12:25:077楼 得分:0
form1.mscomm1.settings   =   "9600,n,8,1 "

    aa(0)   =   2
    aa(1)   =   1
    xordata   =   0   xor   aa(1)
    aa(2)   =   change(text1.text)
    xordata   =   xordata   xor   aa(2)
    aa(3)   =   change(text2(0).text)
    xordata   =   xordata   xor   aa(3)
    aa(4)   =   change(text2(1).text)
    xordata   =   xordata   xor   aa(4)
    aa(5)   =   change(text2(2).text)
    xordata   =   xordata   xor   aa(5)
    aa(6)   =   change(text3(0).text)
    xordata   =   xordata   xor   aa(6)
    aa(7)   =   change(text3(1).text)
    xordata   =   xordata   xor   aa(7)
    aa(8)   =   change(text3(2).text)
   
    xordata   =   xordata   xor   aa(8)
   
   
    aa(9)   =   change(text3(3).text)
    xordata   =   xordata   xor   aa(9)
    aa(10)   =   3
    xordata   =   xordata   xor   aa(10)
    aa(11)   =   xordata
    aa(12)   =   14
   
   

   
    form1.mscomm1.inputmode   =   0
    form1.mscomm1.output   =   aa
以十六进制发送,上述代码中的aa中的元素换成你所要发的数据即可
发表于:2007-01-18 13:23:098楼 得分:0
太感动了     看到这么多人的帮助   我也贴个     后来解决了

private   sub   command1_click()

dim   bdata(1   to   12)   as   byte
bdata(1)   =   &h2
bdata(2)   =   &h30
bdata(3)   =   &h30
bdata(4)   =   &h37
bdata(5)   =   &h31
bdata(6)   =   &h30
bdata(7)   =   &h34
bdata(8)   =   &h36
bdata(9)   =   &h34
bdata(10)   =   &h3
bdata(11)   =   &h39
bdata(12)   =   &h39
mscomm1.output   =   bdata
end   sub


private   sub   command2_click()
dim   bdata(1   to   12)   as   byte
bdata(1)   =   &h2
bdata(2)   =   &h30
bdata(3)   =   &h30
bdata(4)   =   &h37
bdata(5)   =   &h30
bdata(6)   =   &h30
bdata(7)   =   &h34
bdata(8)   =   &h36
bdata(9)   =   &h34
bdata(10)   =   &h3
bdata(11)   =   &h39
bdata(12)   =   &h38

mscomm1.output   =   bdata
end   sub

private   sub   command3_click()
unload   me
end   sub

private   sub   form_load()
mscomm1.portopen   =   true

end   sub


测试过       可以的
发表于:2007-01-18 13:25:539楼 得分:0
再次谢谢   大家   我是做工业自动化的plc   组态   计算机是我的第2专业
发表于:2007-01-25 21:24:0310楼 得分:0
在使用mscomm控件时需注意接收是以ascii码或二进制码  
详细可参阅“人名邮电出版社”的“visual   basic   串口通讯实例导航”一书。
其中第1章串口调试精灵的代码是非常好的代码,完全能修改后用于工业控制.它包含了ascii码和二进制方式(16进制显示)的接收和发送.
myideaer答复的代码是其一部分.


快速检索

最新资讯
热门点击