- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先我要说这是我在 VBForums 上的第一篇帖子,我已经潜伏了一段时间。我是 VB 的新手,学习它只是为了完成我的论文。所以请原谅我的无知。
我正在尝试做的是从另一个程序生成的日志文件中读取数据。我需要将该文件中的特定行导入 VB 以用于其他算法。此日志文件将不断更新,因此需要尽可能实时读取。
如果重要的话,日志文件是从与另一软件通信的 USB 设备生成的。
这是一个示例日志:
`
08:57:00.932 COM12
08:57:00.935 COM11
08:57:00.935 COM10
08:57:00.936 COM9
08:57:00.936 COM8
08:57:00.937 COM7
08:57:00.938 COM6
08:57:00.939 COM5
08:57:00.939 COM4
08:57:00.998 --> 0108000304FF0000
08:57:01.007 <-- 0108000304FF0000
TRF7970A EVM
08:57:01.014 **** COM Port found! ****
08:57:05.009 --> 010A0003041001210000
08:57:05.009 COM4
08:57:05.173 <-- 010A0003041001210000
Register write request.
08:57:05.173 --> 010C00030410002101020000
08:57:05.280 <-- 010C00030410002101020000
Register write request.
08:57:05.280 --> 010B000304140601000000
08:57:05.715 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:05.716 --> 010A0003041001210000
08:57:05.822 <-- 010A0003041001210000
Register write request.
08:57:05.822 --> 010C00030410002101020000
08:57:05.929 <-- 010C00030410002101020000
Register write request.
08:57:05.929 --> 010B000304140601000000
08:57:06.364 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:06.365 --> 010A0003041001210000
08:57:06.470 <-- 010A0003041001210000
Register write request.
08:57:06.470 --> 010C00030410002101020000
08:57:06.576 <-- 010C00030410002101020000
Register write request.
08:57:06.576 --> 010B000304140601000000
08:57:07.011 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:07.012 --> 010A0003041001210000
08:57:07.117 <-- 010A0003041001210000
Register write request.
08:57:07.117 --> 010C00030410002101020000
08:57:07.223 <-- 010C00030410002101020000
Register write request.
08:57:07.223 --> 010B000304140601000000
08:57:07.658 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:07.659 --> 010A0003041001210000
08:57:07.764 <-- 010A0003041001210000
Register write request.
08:57:07.764 --> 010C00030410002101020000
08:57:07.870 <-- 010C00030410002101020000
Register write request.
08:57:07.870 --> 010B000304140601000000
08:57:08.305 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:08.306 --> 010A0003041001210000
08:57:08.411 <-- 010A0003041001210000
Register write request.
08:57:08.411 --> 010C00030410002101020000
08:57:08.517 <-- 010C00030410002101020000
Register write request.
08:57:08.517 --> 010B000304140601000000
08:57:08.952 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:08.952 --> 010A0003041001210000
08:57:09.058 <-- 010A0003041001210000
Register write request.
08:57:09.058 --> 010C00030410002101020000
08:57:09.164 <-- 010C00030410002101020000
Register write request.
08:57:09.164 --> 010B000304140601000000
08:57:09.585 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]
[AD87851A000007E0,7F]
[,40][,40]D
08:57:09.586 --> 010A0003041001210000
08:57:09.692 <-- 010A0003041001210000
Register write request.
08:57:09.692 --> 010C00030410002101020000
08:57:09.798 <-- 010C00030410002101020000
Register write request.
08:57:09.798 --> 010B000304140601000000
08:57:10.233 <-- 010B000304140601000000
ISO 15693 Inventory request.
[,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40][,40]D
08:57:10.234 --> 010A0003041001210000
08:57:10.340 <-- 010A0003041001210000
Register write request.
08:57:10.340 --> 010C00030410002101020000
08:57:10.446 <-- 010C00030410002101020000
Register write request.
`
我需要能够得到这条线:[AD87851A000007E0,7F]
只是“AD87851A000007E0”,然后能够在 VB 中使用该字符串。这是一个会变化的十六进制代码。
我已经能够从日志文件中读取,但没有产生有效的输出。
这是我一直在尝试使用的代码:
Function ReadData(ByRef keyword As String) As IEnumerable(Of String)
Dim result = New List(Of String)
Using reader = New StreamReader("C:\rfid-reader.log")
Dim line As String = reader.ReadLine()
Dim take = False
Do While line IsNot Nothing
If line.StartsWith("[") Then
take = False
End If
If String.Equals("[" + keyword + ",", line) Then
take = True
End If
If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then
result.Add(line)
End If
line = reader.ReadLine()
Label1.Text = line
Loop
End Using
Return result
End Function
老实说,我不确定代码是否无法正常工作,或者我只是在从函数中获取字符串方面做错了什么?该函数会按原样打印到 Label1.Text 吗?
我也试过这样调用函数:
Dim items = ReadData("AD87851A000007E0")
Label1.Text = Convert.ToString(items)
这导致 Label1.Text 变为“System.Collections.Generic.List`1[System.String]”
Imports System.IO
公开课Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim items = ReadData("AD87851A000007E0")
Label1.Text = String.Join(Environment.NewLine, items)
End Sub
Function ReadData(ByRef keyword As String) As IEnumerable(Of String)
Dim result = New List(Of String)
Using reader = New StreamReader("C:\rfid-reader.log")
Dim line As String = reader.ReadLine()
Dim take = False
Do While line IsNot Nothing
take = False ' resetting take = false to avoide printing all lines
If line.StartsWith("[" + keyword + ",") Then
take = True
End If
If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then
result.Add(line)
End If
line = reader.ReadLine()
Loop
End Using
Return result
End Function
下课
编辑 2_______________________________________________
迄今为止的新代码:
Imports System.IO
公开课Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim items = ReadData("AD87851A000007E0") ' Calling the specific hex code from the function. Advancement needed to get ALL hex codes.
' Dim items1 As String = Convert.ToString(items)
' Dim items2 As String = ""
' Dim k As Long
' k = InStrRev(items, ",")
' If k > 0 Then items2 = Left$(items, k)
Label1.Text = String.Join(Environment.NewLine, items) 'Printing function result to Label1
End Sub
'Function to search log file for a specific hex code. Removes not needed portions of the string. Advancements needed:
'Find ALL hex codes
'Ignore repeats until a reset is set true
'Store each hex code found in it's own variable for use in algorithms
Function ReadData(ByRef keyword As String) As IEnumerable(Of String)
Dim result = New List(Of String)
Using reader = New StreamReader("C:\rfid-reader.log")
Dim line As String = reader.ReadLine()
Dim take = False
Do While line IsNot Nothing
take = False ' resetting take = false to avoide printing all lines
If line.StartsWith("[" + keyword + ",") Then
take = True
End If
If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then
line = line.Remove(0, 1) 'removing the extra parts of the line
line = line.Remove(16, 4)
result.Add(line) 'adding the valid result
End If
line = reader.ReadLine()
Loop
End Using
Return result
End Function
下课
Imports System.IO
公开课Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim items = ReadData("[") ' Calling the specific hex code from the function. Advancement needed to get ALL hex codes.
Label1.Text = String.Join(Environment.NewLine, items) 'Printing function result to Label1
End Sub
'Function to search log file for a specific hex code. Removes not needed portions of the string. Advancements needed:
'Find ALL hex codes
'Ignore repeats until a reset is set true
'Store each hex code found in it's own variable for use in algorithms
Function ReadData(ByRef keyword As String) As IEnumerable(Of String)
Dim result = New List(Of String)
Using reader = New StreamReader("C:\rfid-reader.log")
Dim line As String = reader.ReadLine()
Dim line1 As String = "COOKIES"
Dim line2 As String = "COOKIES1"
Dim line3 As String = "COOKIES2"
Dim line4 As String = "COOKIES3"
Dim line5 As String = "COOKIES4"
Dim line6 As String = "COOKIES5"
Dim line7 As String = "COOKIES6"
Dim line8 As String = "COOKIES7"
Dim line1found = False
Dim line2found = False
Dim line3found = False
Dim line4found = False
Dim line5found = False
Dim line6found = False
Dim line7found = False
Dim line8found = False
Dim take = False
Do While line IsNot Nothing
take = False ' resetting take = false to avoide printing all lines
If line.StartsWith(keyword) And Not line.StartsWith("[,4") And Not line.StartsWith("[z,") Then
take = True
End If
If line.Contains(line1) Or line.Contains(line2) Or line.Contains(line3) Or line.Contains(line4) Or line.Contains(line5) Or line.Contains(line6) Or line.Contains(line7) Or line.Contains(line8) Then 'attempting to ignore duplicates
take = False
End If
If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then
line = line.Remove(0, 1) 'removing the extra parts of the line
line = line.Remove(16, 4)
result.Add(line) 'adding the valid result
If line1 <> line And Not line1found Then 'assining results to variables for duplicate elimiation
line1 = line
line1found = True
ElseIf line2 <> line And line1 <> line And Not line2found Then
line2 = line
line2found = True
ElseIf line <> line3 And line <> line2 And line1 <> line And Not line3found Then
line3 = line
line3found = True
ElseIf line <> line4 And line <> line3 And line <> line2 And line <> line1 And Not line4found Then
line4 = line
line4found = True
ElseIf line <> line5 And line <> line4 And line <> line3 And line <> line2 And line <> line1 And Not line5found Then
line5 = line
line5found = True
ElseIf line6 <> line And line5 <> line And line4 <> line And line3 <> line And line2 <> line And line1 <> line And Not line6found Then
line6 = line
line6found = True
ElseIf line7 <> line And line6 <> line And line5 <> line And line4 <> line And line3 <> line And line2 <> line And line1 <> line And Not line7found Then
line7 = line
line7found = True
ElseIf line8 <> line And line7 <> line And line6 <> line And line5 <> line And line4 <> line And line3 <> line And line2 <> line And line1 <> line And Not line8found Then
line8 = line
line8found = True
End If
End If
line = reader.ReadLine()
Loop
End Using
Return result
End Function
下课
最佳答案
您的代码有几个问题。第一个问题是您使用的是 String.Equals
而不是 line.StartsWith
:
If String.Equals("[" + keyword + ",", line) Then
take = True
End If
您会注意到,在上面的代码中,如果整行与字符串“[AD87851A000007E0,”完全匹配,它只会设置 take = True
。但是您要查找的行不等于该字符串——它只是以该字符串开头。因此,您应该将该代码更改为:
If line.StartsWith("[" + keyword + ",") Then
take = True
End If
接下来,您将在循环读取每一行之后设置 Label1.Text = line
,无论该行是否匹配。您正在做的事情会起作用,但可能不会以您想要的方式起作用。按照您现在的方式,它会用读取的每一行覆盖标签的整个文本内容。正如我所说,它对每一行都这样做,而不仅仅是匹配的行。由于您正忙于循环,在您完成之前屏幕不会真正刷新,所以最后,它所做的只是显示文件中的最后一行。
如果你想让它显示所有匹配的行,你需要把它移到 If take...
block 中,你需要把这些行连接在一起,像这样:
If take And Not String.IsNullOrEmpty(line) And Not line.StartsWith("'") Then
result.Add(line)
Label1.Text = Label1.Text & Environment.NewLine & line
End If
但是,这仍然不是一个很好的方法。将 UI 代码与您的业务逻辑混合从来都不是一个好主意,因此您在调用 ReadData
之后设置标签的本能是正确的想法。但是,您不能使用 Convert.ToString
来显示列表的内容。正如您所发现的,这只会显示数据类型的名称。为此,您需要将列表中的字符串连接成一个字符串,如下所示:
Label1.Text = String.Join(Environment.NewLine, items)
我使用 Environment.NewLine
作为分隔符,但您可以使用任何您想要的分隔符。例如,如果你想在一行中显示它们,你可以使用 ", "
作为分隔符。
关于vb.net - 高级文本搜索并将特定字符串从文件导入 VB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15088359/
对于相当简单的表结构,即。人员、标准和 PersonCriteria(组合表),我现在设置了一个查询,选择所有符合所有选定条件的人。 此时查询本身看起来像这样: SELECT p.PersonID
我在使用高级 SQL 查询时遇到了一些问题,而且我已经有很长时间没有使用 SQL 数据库了。我们使用 MySQL。 背景: 我们将使用两个表: “交易表” 表:expire_history +----
我找不到错误。也许你可以帮助我:我的代码如下: var data = {"product":[{"config":[{"id":"1","price":"100","sku":"1054879634
我有一个列表列表的列表(最后一个列表并不重要) data = [[[['f', 0], 'C'], [['X', 0], 'X']], [[['s', 1], 'X'], [['X', 0], 'X'
我想准备将使用表格的 session ,并在另一个网站上将新项目添加到 session 中。 默认.cs string[] tab = new string[100];
我知道有一些像: Bubble sort Insertion sort Shell sort Merge sort Heapsort Quicksort Bucket sort Radix sort
像https://softwareengineering.stackexchange.com/questions/150616/return-random-list-item-by-its-weigh
我正在开发一个 posix 脚本 (Linux),它获取一个网页,将内容存储在一个变量中并查找字符串“SUCCESS”。如果找到字符串,则不执行循环内容,如果没有找到字符串,则反复执行循环,直到找到为
我不确定这个问题是否已在其他地方得到解答,而且我似乎无法通过谷歌找到任何不是“Hello World”示例的内容...我正在使用 C# .NET 4.0 进行编码。 我正在尝试开发一个控制台应用程序,
我创建了一个房地产网站,我希望按照列表的最后更新和完整性对列表进行排序。所以我一直想弄清楚如何结合最近更新的列表按mysql中的字段(completion_score)进行排序。完成分数将采用 1
只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似。很多场合下需要把同一
之前的章节中,我们学习了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法从 XML 文档中取回数据 本章节我们将继续学习其它重要的 XML DOM
我对我尝试编写的 SQL 查询有疑问。 我需要从数据库中查询数据。该数据库除其他外,还包括以下 3 个字段: Account_ID #, Date_Created, Time_Created 我需要编
我正在使用非常激进的视频压缩,例如 -crf 51 .我将其用于“艺术”效果,因此从普通视频压缩的角度来看,我所做的可能没有意义。 到目前为止,我只使用了非常基本的压缩控制,只使用了 -crf。或 -
我真的在学习 lucene 和 ravendb 上的绳索 - 我在 Raven 中有以下文档 - { "InternalEvent": { "Desec": "MachineInfo: 1
通常 grep 命令用于显示包含指定模式的行。有没有办法在包含指定模式的行之前和之后显示 n 行? 这可以使用awk来实现吗? 最佳答案 是的,使用 grep -B num1 -A num2 在匹配之
我搜索了高低,并尝试了几个小时来操纵似乎适合的各种其他查询,但我没有快乐。 我试图加入 Microsoft SQL Server 2005 中的几个表,其中一个示例是: Company Table (
我有一个如下所示的 XML 文件: teacher1Name
我将如何在 CF 中创建此语句? 显然括号不起作用,但说明了我想要完成的工作。这是什么语法? 编辑: 好的,我了解如何使用 EQ 等等。我有点匆忙地发布了这个。我的问题是关于括号。以这种方式使用它们
主要问题:我需要使用具体对象结构对任何对象结构进行类型扩展。 我在 VS Code 中测试的默认值。 我的解决方案: /** @template A @typedef {{[Ki in keyof A
我是一名优秀的程序员,十分优秀!