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



mscomm控件编写的串口程序问题,请高手看看程序问题,高分相送


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


mscomm控件编写的串口程序问题,请高手看看程序问题,高分相送
发表于:2007-01-28 17:01:49 楼主
我是一个初学者,写了一个数据采集程序测试单片机,使用周立功的tks仿真机,不只为什么,联络可以成功,但用定时器读数据时只能读取前3个通道的数,之后程序便停止下来,请高手帮小弟看一下原因,一定高分相送,谢谢
由定时器发送数据,每隔2s发送一次,pc和单片仿真机机波特率相同
命令为16进制0x01为开始字节
.commport   =   1
.settings   =   "9600,n,8,1 "
.inbuffersize   =   20
.outbuffersize   =   20
.inputmode   =   cominputmodebinary
.inputlen   =   1
.sthreshold   =   0
.inbuffercount   =   0
.outbuffercount   =   0
.rthreshold   =   1
public   outtemp(5)   as   byte   //接收数组
outtemp(0)   =   &h1  
outtemp(1)   =   &h2
outtemp(2)   =   &h30
outtemp(3)   =   &h30
outtemp(4)   =   &h3
private   sub   mscomm1_oncomm()
dim   buffer   as   variant
samflag   =   false
with   mscomm1
  select   case   .commevent
  case   comevreceive
    buffer   =   .input
    intemp(1)   =   buffer(0)
    if   intemp(1)   =   &h2   then
            .rthreshold   =   0               '关闭oncomm事件接收
    do
          doevents
      loop   until   .inbuffercount   > =   1
            buffer   =   .input
            intemp(2)   =   buffer(0)
    if   intemp(2)   =   &h1e   then     //如果是联络帧,五个字节
          label1.caption   =   "连接成功 "
              do
          doevents
      loop   until   .inbuffercount   > =   3//一直到接收完数据
      commok   =   true
    end   if//联络部分
    if   intemp(2)   =   &h6   then//要求重发数据
        do
          doevents
      loop   until   .inbuffercount   > =   3
    end   if
    if   intemp(2)   =   &h1f   then   '是数据   数据帧11字节
      do
          doevents
      loop   until   .inbuffercount   > =   8
              buffer   =   .input
              intemp(3)   =   buffer(0)
              buffer   =   .input
              intemp(4)   =   buffer(0)
              buffer   =   .input
              buffer   =   .input
              indata(0)   =   buffer(0)
              buffer   =   .input
              indata(1)   =   buffer(0)
              buffer   =   .input
              indata(2)   =   buffer(0)
              buffer   =   .input
              indata(3)   =   buffer(0)
              buffer   =   .input
              indata(4)   =   buffer(0)
              buffer   =   .input
              samflag   =   true//一组数据接受完毕
              end   if
              end   if
        .rthreshold   =   1
  case   else
  end   select
  end   with
end   sub
定时器2s
private   sub   timer3_timer()
  if   chnlcnt   > =   8   then
        chnlcnt   =   1
        timer3.enabled   =   false
        samflag   =   false
      exit   sub
    end   if
outtemp(3)   =   chnlcnt
mscomm1.output   =   outtemp   //采集命令outtemp(3)为通道号
do
          doevents
loop   until   samflag
samflag   =   false
chnlcnt   =   chnlcnt   +   1
end   sub
发表于:2007-01-28 19:45:121楼 得分:0
代码修改如下,调试时见实时错误 '8018 ',只有端口打开时,操作有效.
option   explicit
private   outtemp(5)   as   byte   '//接收数组
private   chnlcnt   as   integer
private   samflag   as   boolean
private   sub   form_load()
mscomm1.commport   =   1
mscomm1.settings   =   "9600,n,8,1 "
mscomm1.inbuffersize   =   20
mscomm1.outbuffersize   =   20
mscomm1.inputmode   =   cominputmodebinary
mscomm1.inputlen   =   1
mscomm1.sthreshold   =   0
mscomm1.inbuffercount   =   0
mscomm1.outbuffercount   =   0
mscomm1.rthreshold   =   1
outtemp(0)   =   &h1
outtemp(1)   =   &h2
outtemp(2)   =   &h30
outtemp(3)   =   &h30
outtemp(4)   =   &h3

end   sub

private   sub   mscomm1_oncomm()
dim   buffer   as   variant
samflag   =   false
with   mscomm1
  select   case   .commevent
  case   comevreceive
    buffer   =   .input
    intemp(1)   =   buffer(0)
    if   intemp(1)   =   &h2   then
            .rthreshold   =   0               '关闭oncomm事件接收
    do
          doevents
      loop   until   .inbuffercount   > =   1
            buffer   =   .input
            intemp(2)   =   buffer(0)
    if   intemp(2)   =   &h1e   then     '//如果是联络帧,五个字节
          label1.caption   =   "连接成功 "
              do
          doevents
      loop   until   .inbuffercount   > =   3   '//一直到接收完数据
      commok   =   true
    end   if   '//联络部分
    if   intemp(2)   =   &h6   then   '//要求重发数据
        do
          doevents
      loop   until   .inbuffercount   > =   3
    end   if
    if   intemp(2)   =   &h1f   then   '是数据   数据帧11字节
      do
          doevents
      loop   until   .inbuffercount   > =   8
              buffer   =   mscomm1.input
              intemp(3)   =   buffer(0)
              buffer   =   mscomm1.input
              intemp(4)   =   buffer(0)
              buffer   =   mscomm1.input
              buffer   =   mscomm1.input
              indata(0)   =   buffer(0)
              buffer   =   mscomm1.input
              indata(1)   =   buffer(0)
              buffer   =   mscomm1.input
              indata(2)   =   buffer(0)
              buffer   =   mscomm1.input
              indata(3)   =   buffer(0)
              buffer   =   mscomm1.input
              indata(4)   =   buffer(0)
              buffer   =   mscomm1.input
              samflag   =   true   '//一组数据接受完毕
              end   if
              end   if
        mscomm1.rthreshold   =   1
  case   else
  end   select
  end   with
end   sub
'定时器2s
private   sub   timer3_timer()
  if   chnlcnt   > =   8   then
        chnlcnt   =   1
        timer3.enabled   =   false
        samflag   =   false
      exit   sub
    end   if
outtemp(3)   =   chnlcnt
mscomm1.output   =   outtemp   '//采集命令outtemp(3)为通道号
do
          doevents
loop   until   samflag
samflag   =   false
chnlcnt   =   chnlcnt   +   1
end   sub   3
    end   if
    if   intemp(2)   =   &h1f   then   '是数据   数据帧11字节
      do
          doevents
      loop   until   .inbuffercount   > =   8
              buffer   =   .input
              intemp(3)   =   buffer(0)
              buffer   =   .input
              intemp(4)   =   buffer(0)
              buffer   =   .input
              buffer   =   .input
              indata(0)   =   buffer(0)
              buffer   =   .input
              indata(1)   =   buffer(0)
              buffer   =   .input
              indata(2)   =   buffer(0)
              buffer   =   .input
              indata(3)   =   buffer(0)
              buffer   =   .input
              indata(4)   =   buffer(0)
              buffer   =   .input
              samflag   =   true   '//一组数据接受完毕
              end   if
              end   if
        .rthreshold   =   1
  case   else
  end   select
  end   with
end   sub
定时器2s
private   sub   timer3_timer()
  if   chnlcnt   > =   8   then
        chnlcnt   =   1
        timer3.enabled   =   false
        samflag   =   false
      exit   sub
    end   if
outtemp(3)   =   chnlcnt
mscomm1.output   =   outtemp   '//采集命令outtemp(3)为通道号
do
          doevents
loop   until   samflag
samflag   =   false
chnlcnt   =   chnlcnt   +   1
end   sub
发表于:2007-01-28 20:59:352楼 得分:0
你是说把变量改为私有的,不过我的程序是把输出outtemp   定义在一个全局模块中的,
为什么只能采三个通道的数,程序就不行了,
发表于:2007-01-28 21:46:013楼 得分:0
你的timer控件定的时间常数是多少?
发表于:2007-01-28 22:03:214楼 得分:0
感觉程序好像还不全阿。。你里面的intemp和indata2个数组定义了没有?
发表于:2007-01-29 09:11:055楼 得分:0
这只是其中关键的地方,有些不太相关的没贴上,定时器是2s,
发表于:2007-01-29 09:44:016楼 得分:0
timer时间设置为2000ms,如设置为2则有可能来不及反应。
发表于:2007-01-29 11:06:497楼 得分:0
我设的是2s   两秒钟
发表于:2007-01-29 12:08:338楼 得分:0
timer控件的属性interval设置值是以毫秒为单位,所以2s必须设置为2000
发表于:2007-01-29 12:24:519楼 得分:0
呵呵,这些我知道的,现在问题是,2s的时间应该没问题,
发表于:2007-01-29 12:26:2910楼 得分:0
为什么数据总是接受3组,就停了,

发表于:2007-01-31 18:39:0211楼 得分:0
检查你mscomm控件的波特率是多少,而且应与下位机一致.你接收命令的字长是多少,每字节按10个数据位(二进制).假如波特率为9600,则每秒可传送960个ascii码的信息.2秒理论上可接收1920个ascii码.如果接收数据单条不到100字长,应将timer控件的interval设置的小些,为100到200毫秒为宜.
发表于:2007-02-01 15:16:5912楼 得分:0
谢谢大家热心帮助,问题已解决了,下位机程序考虑不周导致的,感谢zdingyun的热心帮忙,给他加上50分


快速检索

最新资讯
热门点击