我昨天去接一个仪器,仪器是用来测量直径的,
当往com口发送字母 “f“后,仪器会往com口
发送大量的数据。
例如:  ”12.034 12.056。。“
我写了一段程序是从com口读取这个数据串,并且把
数据分离出来。(com口数据是时刻在变的)Private Sub Command13_Click()
   com.CommPort = 1                '假定为com1口
   com.Settings = "4800,n,8,1"     '设置参数
   com.OutBufferCount = 0          '清空输出寄存器
   com.InBufferCount = 0           '清空输入寄存器
   com.InputMode = comInputModeText '设置接收数据格式为文本
   com.PortOpen = True             '打开com口
   com.Output = "f"
End Sub
private Sub Timer1_timer()
   dim value_temp ,data as string 
   if com.input<>"" then
      value_temp=com.input
         ............(分离程序)
   End If
End Sub程序在  if com .....处设置断点,单句执行,程序表现正常。
但是如果全启动的话,读出内容始终为0。
不知道是否变量定义错了,因为com口确实有数据,但是value_temp
却为空。请帮助!!!

解决方案 »

  1.   

    不要用 Timer 事件。用 msComm 控件的 OnComm 事件。Private Sub MSComm_OnComm ()
       If MSComm1.CommEvent = comEvReceive Then
          value_temp=com.input
             ............(分离程序)
       End If
    End Sub  
      

  2.   

    对!MSComm_OnComm ,可以用
    select case MSComm1.CommEvent 
    case comEvReceive
    ''''''''end select
      

  3.   

    你只要用timer读value_temp的值就可以了,不用读input
      

  4.   

    用Timer事件和OnComm 事件都可以
    private Sub Timer1_timer()
      dim value_temp ,data as string    value_temp=com.input           '串口读一次后就清空缓冲区了
       if value_temp<>"" then
           ............(分离程序)
       End If
    End Sub
      

  5.   

    对!MSComm_OnComm ,可以用
    select case MSComm1.CommEvent 
    case comEvReceive
    ''''''''end select同意上面的方法,比较简单
    不过你数据量多的时候,进onComm事件后最好延迟一段时间后再读
      

  6.   

    我在本机联一条com跳线,然后2,3短路
    测试上面的ONcomm事件,但是好像不发生。
    Private Sub Command13_Click()
       dim i as integer
       com.CommPort = 1                '假定为com1口
       com.Settings = "4800,n,8,1"     '设置参数
       com.OutBufferCount = 0          '清空输出寄存器
       com.InBufferCount = 0           '清空输入寄存器
       com.InputMode = comInputModeText '设置接收数据格式为文本
       com.PortOpen = True             '打开com口
       for i=1 to 1000
          com.Output = i
       next
    End Sub
    Private Sub MSComm_OnComm ()
       If MSComm1.CommEvent = comEvReceive Then
          value_temp=com.input
             ............(分离程序)
       End If
    End Sub  
      

  7.   

    是否必须设置属性Sthreshold=0?
      

  8.   

    Sub Command13_Click()
       dim i as integer
       com.CommPort = 1                '假定为com1口
       com.Settings = "4800,n,8,1"     '设置参数
       com.OutBufferCount = 0          '清空输出寄存器
       com.InBufferCount = 0           '清空输入寄存器
       com.InputMode = comInputModeText '设置接收数据格式为文本
       com.RThreshold = 1'这个设定有信息就触发
       com.PortOpen = True             '打开com口
       for i=1 to 1000
          com.Output = i
       next
    End Sub
    Private Sub MSComm_OnComm ()
       If MSComm1.CommEvent = comEvReceive Then
          value_temp=com.input
             ............(分离程序)
       End If
    End Sub  
      

  9.   

    我发现一个关于属性rthreshold不好的地方,就是当属性值设置为1之后
    如果com口里面数据是12的时候,再读取的时候会把1和2分开。这样就要
    用程序去判断。
      

  10.   

    private Sub Timer1_timer()
       dim value_temp ,data as string 
       if com.input<>"" then        <--- com每次读取会清空缓冲
          value_temp=com.input      <--- 这里再次读取就为空了 
             ............(分离程序)
       End If
    End Sub改为:
    private Sub Timer1_timer()
    dim value_temp as string
    dim data       as string 
       value_temp=com.input
       if value_temp<>"" then       
             ............(分离程序)
       End If
    End Sub
      

  11.   

    延时不够吧,用TIMER很好的,主动权比较强,我所有的通讯程序都是TIMER的,很稳定,没有任何问题。