VB调用MATLAB,参数不可用
程序代码:
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 g As Variant Dim Buffer(3) As String '数据数组 Dim dbconn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim rsJin As New ADODB.Recordset Dim rs1 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" '设置通信口参数 = 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() '串口通信,采集 Select Case '串口事件 Case comEvReceive '接收到数据 For I = 0 To 3 Buffer(I) = MSComm1.Input Select Case I Case 0 UX1 = Val(Buffer(0)) Case 1 UY2 = Val(Buffer(1)) Case 2 UY1 = Val(Buffer(2)) Case 3 UX2 = 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, UY1, UY2, UX2) '调用matlab data1(num) = a1 filedata1(num) = Format$(data1(num), "0.0000") Tu1.Text = filedata1(num) data2(num) = b2 filedata2(num) = Format$(data2(num), "0.0000") 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) Call theGmj2.x3(1, g, a1, b2) 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 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 = 24 '设置接收一个字节产生Oncomm事件 Timer1.Enabled = True '时钟有效 Call MSComm1_OnComm '串口通信 End Sub Private Sub draw() Picture1.Cls Picture1.DrawWidth = 1 Picture1.BackColor = QBColor(15) Picture1.Scale (0, 1)-(200, -1) Picture2.Cls Picture2.DrawWidth = 1 Picture2.BackColor = QBColor(15) Picture2.Scale (0, 1)-(200, -1) 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(UX1)" MSFlexGrid1.TextMatrix(0, 2) = "S4(UX2)" MSFlexGrid1.TextMatrix(0, 3) = "S2(UY2)" MSFlexGrid1.TextMatrix(0, 4) = "S3(UY1)" 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