Mscomm 在 50ms 刷新的时候出错,高分寻求答案
enix 2005-11-03 05:20:50 在程序里加入了 timer ,让它50ms 重复执行,结果出现错误。
代码如下,请高手指点迷津
----------------------------------------------------------
Private Sub Command1_Click()
Unload Me
With Port_set.MSComm1
If .PortOpen = True Then
.PortOpen = False
End If
End With
End Sub
Private Sub Command2_Click()
With Port_set.MSComm1
Timer1.Enabled = True
Text9.Text = Now
'Add Net Recordset
Dim strsql As String
strsql = "select top 0 * from ad_temp"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strsql
Adodc1.Refresh
Adodc1.Recordset.AddNew
Dim str As String
'Send #01(& vbkeyenter) to Comport
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
str = .Input
If Left(str, 1) <> "+" Or Left(str, 1) <> "-" Then
str = Right(str, 57)
Else
str = .Input
End If
'Read String per 7
Text1.Text = CVar(Left(str, 7))
Text2.Text = CVar(Right(Left(str, 14), 7))
Text3.Text = CVar(Right(Left(str, 21), 7))
Text4.Text = CVar(Right(Left(str, 28), 7))
Text5.Text = CVar(Right(Left(str, 35), 7))
Text6.Text = CVar(Right(Left(str, 42), 7))
Text7.Text = CVar(Right(Left(str, 49), 7))
Text8.Text = CVar(Right(Left(str, 56), 7))
'插入数据库
Adodc1.Recordset!channel1 = Text1.Text
Adodc1.Recordset!channel2 = Text2.Text
Adodc1.Recordset!channel3 = Text3.Text
Adodc1.Recordset!channel4 = Text4.Text
Adodc1.Recordset!channel5 = Text5.Text
Adodc1.Recordset!channel6 = Text6.Text
Adodc1.Recordset!channel7 = Text7.Text
Adodc1.Recordset!channel8 = Text8.Text
Adodc1.Recordset!date_time = Text9.Text
'Adodc1.Recordset!ad_time = Text10.Text
Adodc1.Recordset.Update
End With
End Sub
Private Sub Form_Load()
With Port_set.MSComm1
.InBufferSize = 512
.InputLen = 0
.PortOpen = True
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
'Text9.Text = .CommPort
Text9.Text = Now
'Text10.Text = Time
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
With Port_set.MSComm1
If .PortOpen = True Then
.PortOpen = False
End If
End With
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = True
Timer1.Interval = 50
With Port_set.MSComm1
If .PortOpen = True Then
Timer1.Enabled = True
Text9.Text = Now
'Add Net Recordset
Dim strsql As String
strsql = "select top 0 * from ad_temp"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strsql
Adodc1.Refresh
Adodc1.Recordset.AddNew
Dim str As String
'Send #01(& enter) to Comport
.Output = Chr(35) & Chr(48) & Chr(49) & Chr(13)
str = .Input
If Left(str, 1) <> "+" Or Left(str, 1) <> "-" Then
str = Right(str, 57)
Else
str = .Input
End If
'每七位作为一个频道
'数据 +0.0001+0.0002+0.0011-0.0012+0.0008+0.0011-0.0011+0.0007
Text1.Text = CVar(Left(str, 7))
Text2.Text = CVar(Right(Left(str, 14), 7))
Text3.Text = CVar(Right(Left(str, 21), 7))
Text4.Text = CVar(Right(Left(str, 28), 7))
Text5.Text = CVar(Right(Left(str, 35), 7))
Text6.Text = CVar(Right(Left(str, 42), 7))
Text7.Text = CVar(Right(Left(str, 49), 7))
Text8.Text = CVar(Right(Left(str, 56), 7))
'Insert into Database
Adodc1.Recordset!channel1 = Text1.Text
Adodc1.Recordset!channel2 = Text2.Text
Adodc1.Recordset!channel3 = Text3.Text
Adodc1.Recordset!channel4 = Text4.Text
Adodc1.Recordset!channel5 = Text5.Text
Adodc1.Recordset!channel6 = Text6.Text
Adodc1.Recordset!channel7 = Text7.Text
Adodc1.Recordset!channel8 = Text8.Text
Adodc1.Recordset!date_time = Text9.Text
Adodc1.Recordset.Update
End If
End With
End Sub