Vb.net VB中定时器值的计算
我在代码中使用计时器。我正在设置从arduino接收串行数据的1s定时器。现在,我想用定时器每1分钟写入一次excel文件。就个人而言,它工作得很好。每1秒写入excel。但我想每1分钟写一次。 下面我发布了计时器代码。设置为1s。当1s超过增量计数器并在文本框上显示计数值时 若计数值达到60意味着1分钟。然后写入excel工作表,否则继续递增 这是我的部分代码Vb.net VB中定时器值的计算,vb.net,vba,timer,counter,Vb.net,Vba,Timer,Counter,我在代码中使用计时器。我正在设置从arduino接收串行数据的1s定时器。现在,我想用定时器每1分钟写入一次excel文件。就个人而言,它工作得很好。每1秒写入excel。但我想每1分钟写一次。 下面我发布了计时器代码。设置为1s。当1s超过增量计数器并在文本框上显示计数值时 若计数值达到60意味着1分钟。然后写入excel工作表,否则继续递增 这是我的部分代码 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim counter As Integer
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim value As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
Dim s As New Series
s.Points.AddXY(1000, k)
Chart1.Series.Add(s)
Count_val.Text = counter
If counter = 6 Then
Dim headerText = ""
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
headerText = "Date& time ,value, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim y As String = DateAndTime.Now()
Dim x As String = y + "," + value
outFile.Write(x)
End Using
End If
Catch ex As Exception
End Try
End Sub
在计时器中,勾选“将计数器声明为静态”。使用Dim时,值范围是本地的,返回后将丢失。这意味着在每一个滴答声中,计数器始终为零。Static即使在返回后也会保留该值
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static counter As Integer = 0 'Initial value
counter += 1 'increase for every tick
Try
...
...
If counter = 60 Then
...
...
End If
Catch ex As Exception
MsgBox("Error in Timer1_Tick: " & ex.Message)
End Try
End Sub
或者有没有办法计算被调用的时间计时器的数量?>?计数器是一个永远不会得到值的变量。另外,不要做:Catch ex作为异常结束,尝试空Catch。@TimSchmelter怎么做。我发现计时器不是从串行代码中获取数据的。使用计时器,我可以写入CSV文件,以获得设置的间隔数。@γηράσκωδ'αείπολλάΔι如何仅获取日期。如果我现在调用DateAndTime,它不会给出第二个值。