下边这段代码,
Do Until MSComm1.InBufferCount = 24       '查询方式,等待接收24个字符
  DoEvents
  Loop
这句话没有的时候不会出现溢出,但是采到的数据每组之间隔一组0,有这句的时候会出现错误提示,这是为什么。
Private theGmj1 As myGmj.gmj
Private theGmj2 As mytu.tu
Dim num As Integer                     '采集数据的个数
Dim Data3(1000), Data4(1000), Data5(1000), Data6(1000) As Single
Dim data1(1000) As Single '采样电压数据的数值形式
Dim data2(1000) As Single
Dim filedata3(1000), filedata4(1000), filedata5(1000), filedata6(1000) As String
Dim filedata1(1000) As String
Dim filedata2(1000) As String
Dim I As Integer
Dim UX1 As Double
Dim UX2 As Double
Dim UY1 As Double
Dim UY2 As Double
Dim a1 As Variant
Dim b2 As Variant
    
Dim Buffer(3) As String '数据数组Dim dbconn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Form_Load()                '初始化If dbconn.State Then dbconn.Close
dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\data.mdb;Persist Security Info=False"  Set theGmj1 = New myGmj.gmj
  Set theGmj2 = New mytu.tu
  MSComm1.Settings = "9600,n,8,1"     '设置通信口参数
  MSComm1.CommPort = 4                 '使用COM4
  MSComm1.PortOpen = True               '打开通信口
  MSComm1.InputLen = 6                '设置Input一次从接收缓冲读取字节数为6
  MSComm1.InputMode = 0                  '以文本方式读数据
  MSComm1.OutBufferCount = 0          '清除发送缓冲区
  MSComm1.SThreshold = 6             '设置Output一次从发送缓冲区读取字节数为6
 Cmdget2.Enabled = True
  Cmdstop3.Enabled = True
  Cmdquit.Enabled = True
  Call tabinit
 End SubPrivate Sub MSComm1_OnComm()              '串口通信,采集
 
Do Until MSComm1.InBufferCount = 24       '查询方式,等待接收24个字符
  DoEvents
  LoopSelect Case MSComm1.CommEvent '串口事件
Case comEvReceive '接收到数据   For I = 0 To 3
   Buffer(I) = MSComm1.Input
   Select Case I
   Case 0
      UX1 = Val(Buffer(0))
      Case 1
      UX2 = Val(Buffer(1))
      Case 2
      UY1 = Val(Buffer(2))
      Case 3
      UY2 = Val(Buffer(3))
      End Select
      Next I
   
  Data3(num) = UX1
  filedata3(num) = Format$(Data3(num), "0.00")
  Text1.Text = filedata3(num)
  Data4(num) = UY1
  filedata4(num) = Format$(Data4(num), "0.00")
  Text2.Text = filedata4(num)
  Data5(num) = UY2
  filedata5(num) = Format$(Data5(num), "0.00")
  Text3.Text = filedata5(num)
  Data6(num) = UX2
  filedata6(num) = Format$(Data6(num), "0.00")
  Text4.Text = filedata6(num)
  Call theGmj1.gmj_gmj(2, a1, b2, UX1, UX2, UY1, UY2)      '调用matlab
  Call theGmj2.x3(1, g, a1, b2)
  data1(num) = a1
  filedata1(num) = Format$(data1(num), "0.00")
  Tu1.Text = filedata1(num)
  data2(num) = b2
  filedata2(num) = Format$(data2(num), "0.00")
  Tu2.Text = filedata2(num)
   MSFlexGrid1.TextMatrix(num, 1) = filedata3(num)
   MSFlexGrid1.TextMatrix(num, 2) = filedata6(num)
   MSFlexGrid1.TextMatrix(num, 3) = filedata5(num)
   MSFlexGrid1.TextMatrix(num, 4) = filedata4(num)
   MSFlexGrid1.TextMatrix(num, 5) = filedata1(num)
   MSFlexGrid1.TextMatrix(num, 6) = filedata2(num)
   
   Dim rsJin As New ADODB.Recordset
    If rsJin.State Then rsJin.Close
    rsJin.Open "select top 1 * from 数据保存", dbconn, adOpenKeyset, adLockPessimistic
    rsJin.AddNew
    rsJin.Fields("日期") = Date
    rsJin.Fields("时间") = Time
    rsJin.Fields("传感器1") = filedata3(num)
    rsJin.Fields("传感器4") = filedata6(num)
    rsJin.Fields("传感器2") = filedata5(num)
    rsJin.Fields("传感器3") = filedata4(num)
    rsJin.Fields("alpha") = filedata1(num)
    rsJin.Fields("beta") = filedata2(num)
    rsJin.Update
    Dim rs1 As New ADODB.Recordset
If rs1.State Then rs1.Close
    rs1.Open "select top 30 * from 数据保存 order by 时间 desc", dbconn, adOpenKeyset, adLockPessimistic   num = num + 1
   If num > 199 Then Call renew
  Call draw
  Call tabinit
  MSComm1.InBufferCount = 0
End SelectEnd SubPrivate Sub Cmdget2_Click()           '连续采集
MSComm1.RThreshold = 1                '设置接收一个字节产生Oncomm事件
Timer1.Enabled = True                 '时钟有效
Call MSComm1_OnComm                   '串口通信
End SubPrivate Sub draw()
  Picture1.Cls
  Picture1.DrawWidth = 1
  Picture1.BackColor = QBColor(15)
  Picture1.Scale (0, 3.14)-(200, -3.14)
  Picture2.Cls
  Picture2.DrawWidth = 1
  Picture2.BackColor = QBColor(15)
  Picture2.Scale (0, 3.14)-(200, -3.14)
  
  For I = 1 To num - 1
    X1 = (I - 1): Y1 = data1(I - 1)
    X2 = I: Y2 = data1(I)
    Picture1.Line (X1, Y1)-(X2, Y2), QBColor(0)
    
    X1 = (I - 1): Y1 = data2(I - 1)
    X2 = I: Y2 = data2(I)
    Picture2.Line (X1, Y1)-(X2, Y2), QBColor(0)
  
  Next IEnd SubPrivate Sub Command1_Click()
dbconn.Execute "delete from 数据保存"
End SubPrivate Sub Cmdstop3_Click()          '使时钟无效,停止采集
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False             '关闭通信口
End If
Timer1.Enabled = False
End SubPrivate Sub renew()
  If num = 0 Then Exit Sub
  MSFlexGrid1.Clear
  Picture1.Cls
  For I = 0 To num - 1
   data1(I) = 0
    data2(I) = 0
  Next I
  num = 0
  Call tabinit
End SubPublic Sub tabinit()
MSFlexGrid1.Cols = 7
MSFlexGrid1.Rows = 200 + 1
MSFlexGrid1.Col = 0
For I = 1 To 200
MSFlexGrid1.Row = I: MSFlexGrid1.TextMatrix(I, 0) = " " + Str$(I)
Next I
 MSFlexGrid1.TextMatrix(0, 1) = "S1"
 MSFlexGrid1.TextMatrix(0, 2) = "S4"
 MSFlexGrid1.TextMatrix(0, 3) = "S2"
 MSFlexGrid1.TextMatrix(0, 4) = "S3"
 MSFlexGrid1.TextMatrix(0, 5) = "α"
 MSFlexGrid1.TextMatrix(0, 6) = "β"
End SubPrivate Sub Cmdquit_Click()                       '关闭程序
Unload Me
End SubPrivate Sub Timer1_Timer()
MSComm1.Output = MSComm1.Input
End Sub

解决方案 »

  1.   

    因为你写了 DoEvents 
    如果此时又收到数据,那么它会被DoEvents调用。导致无限递归。Private inOnComm As Boolean
    Private Sub MSComm1_OnComm()
    If inOnComm Then Exit Sub
    inOnComm = True
    ...
    inOnComm = False
    End Sub
    你可以用这样的代码防止OnComm被重入。
      

  2.   

    Private Sub Form_Load()                '初始化If dbconn.State Then dbconn.Close
    dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\data.mdb;Persist Security Info=False"  Set theGmj1 = New myGmj.gmj
      Set theGmj2 = New mytu.tu
      MSComm1.Settings = "9600,n,8,1"     '设置通信口参数
      MSComm1.CommPort = 4                 '使用COM4
      MSComm1.PortOpen = True               '打开通信口
      MSComm1.InputLen = 6                '设置Input一次从接收缓冲读取字节数为6
      MSComm1.InputMode = 0                  '以文本方式读数据
      MSComm1.OutBufferCount = 0          '清除发送缓冲区
      MSComm1.SThreshold = 6             '设置Output一次从发送缓冲区读取字节数为6
      MSComm1.SThreshold = 24             '*接收到 24 个字节再触发 OnComm*
      Cmdget2.Enabled = True
      Cmdstop3.Enabled = True
      Cmdquit.Enabled = True
      Call tabinit
     End Sub
      

  3.   

    MSComm1.RThreshold = 24             '*接收到 24 个字节再触发 OnComm*