下邊這段代碼,
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 Sub
Private Sub MSComm1_OnComm() '串口通信,采集
Do Until MSComm1.InBufferCount = 24 '查詢方式,等待接收24個字符
DoEvents
Loop
Select 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 Select
End Sub
Private Sub Cmdget2_Click() '連續采集
MSComm1.RThreshold = 1 '設定接收一個位元組產生Oncomm事件
Timer1.Enabled = True '時鐘有效
Call MSComm1_OnComm '串口通信
End Sub
Private 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 I
End Sub
Private Sub Command1_Click()
dbconn.Execute "delete from 資料保存"
End Sub
Private Sub Cmdstop3_Click() '使時鐘無效,停止采集
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False '關閉通信口
End If
Timer1.Enabled = False
End Sub
Private 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 Sub
Public 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 Sub
Private Sub Cmdquit_Click() '關閉程式
Unload Me
End Sub
Private Sub Timer1_Timer()
MSComm1.Output = MSComm1.Input
End Sub
uj5u.com熱心網友回復:
因為你寫了 DoEvents如果此時又收到資料,那么它會被DoEvents呼叫。
導致無限遞回。
Private inOnComm As Boolean
Private Sub MSComm1_OnComm()
If inOnComm Then Exit Sub
inOnComm = True
...
inOnComm = False
End Sub
你可以用這樣的代碼防止OnComm被重入。
uj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
MSComm1.RThreshold = 24 '*接收到 24 個位元組再觸發 OnComm*轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/130452.html
標籤:VB基礎類
上一篇:關于MSChart 控制元件
