我目前正在开发一个涉及从serialport和tcp / ip发送和接收数据的项目。
每当我收到来自设备的消息时,我都会在richtextbox lastline中输出消息,然后相应地处理收到的消息,所有这些都正常工作,除非设备只在一行发送多个命令。
例如,设备以编程方式分配,一次发送1个命令,如下所示:
MSTAT01P0K0S0 ^
MSTAT02P1K1S1 ^
MSTAT03P0K0S0 ^
MSTAT04P1K1S1 ^
由于我一次收到1个命令,我可以将它附加到我的richtextbox lastline并轻松处理它。但有时有些情况会阻止设备一次发送1个命令,它将看起来像这样
MSTAT01P0K0S0 ^ MSTAT02P1K1S1 ^ MSTAT03P0K0S0 ^ MSTAT04P1K1S1 ^
我的richtextbox中没有4行代码,而是现在有4个命令的链接。这是我的问题。
问题:如何修改代码以处理richtextbox最后一行的命令?
Note:
我可以通过将M
指定为开头并将^
指定为结尾来确定我从设备收到的命令的开始和结束。
我有这段代码:
Dim Sched_Num As String = String.Empty
Dim KS_Status As String = String.Empty
Dim SSR_Status As String = String.Empty
Dim Photocell_Status As String = String.Empty
Dim matches As MatchCollection = Regex.Matches(events_process, "(M[^\^]+\^)")
For Each m As Match In matches
For Each c As Capture In m.Captures
Dim CSTAT_Cmd As Boolean = events_process Like "MSTAT??P?K?S?^*"
If CSTAT_Cmd = True Then
Sched_Num = events_process.Substring(5, 2)
Photocell_Status = events_process.Substring(8, 1)
KS_Status = events_process.Substring(10, 1)
SSR_Status = events_process.Substring(12, 1)
End If
Next
Console.WriteLine(Sched_Num & " " & Photocell_Status & " " & KS_Status & " " & SSR_Status)
Next
当它同时涉及1个命令时效果很好,问题是当设备发送命令块时。
如果收到消息:MSTAT01P0K0S0^
。它输出01 0 0 0
这是正确的!
如果收到消息:MSTAT01P0K0S0^MSTAT10P1K1S1
。它再次输出01 0 0 0
。 :(
答案 0 :(得分:2)
如果我了解您的要求,当您在内部For Each
内时,请使用Value
对象中的Capture
属性而不是{{1像这样:
events_process
结果:
Imports System
Imports System.Text.RegularExpressions
Public Module Module1
Public Sub Main()
Dim events_process = "MSTAT01P0K0S0^ MSTAT02P1K1S1^ MSTAT03P0K0S0^ MSTAT04P1K1S1^"
Dim Sched_Num As String = String.Empty
Dim KS_Status As String = String.Empty
Dim SSR_Status As String = String.Empty
Dim Photocell_Status As String = String.Empty
Dim matches As MatchCollection = Regex.Matches(events_process, "(M[^\^]+\^)")
For Each m As Match In matches
For Each c As Capture In m.Captures
Dim CSTAT_Cmd As Boolean = c.Value Like "MSTAT??P?K?S?^*"
If CSTAT_Cmd = True Then
Sched_Num = c.Value.Substring(5, 2)
Photocell_Status = c.Value.Substring(8, 1)
KS_Status = c.Value.Substring(10, 1)
SSR_Status = c.Value.Substring(12, 1)
Console.WriteLine(Sched_Num & " " & Photocell_Status & " " & KS_Status & " " & SSR_Status)
End If
Next
Next
End Sub
End Module
实际上,内部01 0 0 0
02 1 1 1
03 0 0 0
04 1 1 1
并不是必需的。您可以将外部For Each
与For Each
对象中的Value
属性一起使用
Match
结果:
Imports System
Imports System.Text.RegularExpressions
Public Module Module1
Public Sub Main()
Dim events_process = "MSTAT01P0K0S0^ MSTAT02P1K1S1^ MSTAT03P0K0S0^ MSTAT04P1K1S1^"
Dim Sched_Num As String = String.Empty
Dim KS_Status As String = String.Empty
Dim SSR_Status As String = String.Empty
Dim Photocell_Status As String = String.Empty
Dim matches As MatchCollection = Regex.Matches(events_process, "(M[^\^]+\^)")
For Each m As Match In matches
Dim CSTAT_Cmd As Boolean = m.Value Like "MSTAT??P?K?S?^*"
If CSTAT_Cmd = True Then
Sched_Num = m.Value.Substring(5, 2)
Photocell_Status = m.Value.Substring(8, 1)
KS_Status = m.Value.Substring(10, 1)
SSR_Status = m.Value.Substring(12, 1)
Console.WriteLine(Sched_Num & " " & Photocell_Status & " " & KS_Status & " " & SSR_Status)
End If
Next
End Sub
End Module