我有一个包含以下数据的文本文件:
02/26/16 11:36:06.33,0.530, ,934
02/26/16 11:36:06.34,0.540, ,935
02/26/16 11:36:06.34,0.540, ,932
02/26/16 11:36:06.37,0.560, ,935
02/26/16 11:36:06.37,0.570, ,934
02/26/16 11:36:06.38,0.580, ,933
02/26/16 11:36:06.41,0.610, ,932
02/26/16 11:36:06.42,0.610, ,933
02/26/16 11:36:06.43,0.630, ,933
02/26/16 11:36:06.44,0.640, ,932
02/26/16 11:36:06.46,0.660, ,931
02/26/16 11:36:06.47,0.670, ,931
02/26/16 11:36:06.49,0.690, ,933
02/26/16 11:36:06.49,0.690, ,931
02/26/16 11:36:06.51,0.710, ,934
02/26/16 11:36:06.53,0.720, ,927
02/26/16 11:36:06.54,0.740, ,932
02/26/16 11:36:06.55,0.750, ,932
02/26/16 11:36:06.57,0.770, ,933
02/26/16 11:36:06.59,0.790, ,929
我希望将逗号加载到数组之后加载日期(即02/26/16)时间(即11:36:06)和最后一个数据(即934)。
如果最后一个数据有5个连续值在预设MAX范围内。价值和最低。值,将显示一条消息及其对应的日期和时间。我如何将这些值读入数组并在vb.net中进行比较?
大多数字符的行是
02/29/16 12:24:02.25,10.000, ,1044
错误的行是
0202/29/16 12:23:32.35,10.110, ,916
02/29/1602/29/16 12:23:25.87,3.630, ,912
02/29/16 12:24:04.84,12.02/29/16 12:24:05.87,13.620, ,1041
02/29/16 12:23:56.45,4.200, ,02/29/16 12:23:57.12,4.870, ,1041
02/29/16 102/29/16 12:23:27.42,5.180, ,913
02/29/16 12:23:48.80,26.560, ,502/29/16 12:23:49.12,26.880, ,503
答案 0 :(得分:0)
不是将值读入数组,而是将它们添加到List中更容易 - 如果你想做其他事情,列表会更强大。
在表单中添加一个backgroundworker控件,调用CheckFileData
和一个名为Timer1
的定时器控件
在下面的代码中,有一个名为btnStartChecking
的按钮的处理程序和一个名为btnStopChecking
的按钮的处理程序,您可以添加这些按钮或重命名自己的按钮。或者,如果您更喜欢其他方式,请告诉我。
表单上还有一个标签,在我的代码中是label1
,但您可以将其更改为您想要的任何内容 - 这会显示数据检查程序的状态,并由Timer1
<更新/ p>
Imports System.Globalization
Imports System.IO
Imports System.Threading
Public Class Form1
'Your Filename
Const fileName As String = "C:\Users\Dad\Documents\Visual Studio 2015\Projects\readDataFromLiveFile\readDataFromLiveFile\TextFile1.txt"
'The required date format
Dim dateFormat As String = "MM/dd/yy hh:mm:ss.ff"
'The range you want to specify goes here
Dim Min As Single = 300
Dim Max As Single = 950
'Structure to make handling of the data easier
Structure DataLine
Public [Date] As String
Public Data1 As String
Public Data2 As String
Public Data3 As String
Public Validated As Boolean
End Structure
'02/26/16 11:36:06.59,0.790, ,929
Private Function GetLastFiveLines() As List(Of String)
Dim lines() As String = IO.File.ReadAllLines(fileName)
Dim lastline As Integer = lines.GetUpperBound(0)
Dim templist As New List(Of String)
If lines.GetUpperBound(0) > 4 Then
templist.Add(lines(lastline - 4))
templist.Add(lines(lastline - 3))
templist.Add(lines(lastline - 2))
templist.Add(lines(lastline - 1))
templist.Add(lines(lastline - 0))
End If
Return templist
End Function
'02/26/16 11:36:06.59,0.790, ,929
'TODO Try to parse a line and discard it if the data isn's valid
Private Function SplitLine(line As String) As DataLine
Dim tempDataLine As New DataLine
Dim result As DateTime
With tempDataLine
.Validated = False
Dim temp() As String
'split the line into it's fields
temp = Split(line, ","c)
'If there Then are more than four fields then mark the resulting dataline as not validated and exit the function
If temp.GetUpperBound(0) > 3 Then
.Validated = False
Return tempDataLine
End If
'if the date can't be parsed, mark as not validated and exit function
'If Not DateTime.TryParse(temp(0), culture, styles, result) Then
If Not DateTime.TryParseExact(temp(0), "MM/dd/yy hh:mm:ss.ff", New CultureInfo("en-us"), DateTimeStyles.None, result) Then
.Validated = False
Return tempDataLine
Else
.Date = result.ToString
.Data1 = temp(1)
.Data2 = temp(2)
.Data3 = temp(3)
.Validated = True
Return tempDataLine
End If
End With
Return tempDataLine
End Function
Private Sub CheckFileData_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles CheckFileData.DoWork
Dim lastFiveLines As New List(Of String)
Dim tempdata As Double
Do
Dim s As New Stopwatch
s.Start()
Dim insideRangeCount As Integer = 0
'get last 5 lines of file
lastFiveLines = GetLastFiveLines()
'try to split them into a DataLine Structure and check the data values
For Each line As String In lastFiveLines
Dim tempDataline As DataLine = SplitLine(line)
'If the line has been validated
If tempDataline.Validated = True Then
'check if the last data item is within the specified range
tempdata = CDbl(tempDataline.Data3)
Select Case tempdata
Case Min To Max
insideRangeCount += 1
Case Else
insideRangeCount = 0
End Select
End If
Next
'If 5 or more items have been found, popup a message box and stop checking
' You can remove the cancelasync command if you wish and change what happens
'if 5 items in a row are found by changing the code within this If statement
If insideRangeCount >= 5 Then
Dim messageString As String = String.Format("Notification: 5 consecutive samples are with the range: {0} to {1}", Min, Max)
insideRangeCount = 0
MessageBox.Show(messageString)
CheckFileData.CancelAsync()
End If
'keep going till the universe goes cold or until someone tells the background worker to stop
s.Stop()
Dim p As Long = s.ElapsedMilliseconds
Loop Until CheckFileData.CancellationPending
e.Cancel = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If CheckFileData.IsBusy Then
label1.text = "Checking"
Else
label1.text = "Idle"
End If
End Sub
Private Sub btnStartChecking_Click(sender As Object, e As EventArgs) Handles btnStartChecking.Click
CheckFileData.RunWorkerAsync()
End Sub
Private Sub btnStopChecking_Click(sender As Object, e As EventArgs) Handles btnStopChecking.Click
CheckFileData.CancelAsync()
End Sub
End Class