0
我正在VB 2010中開發一個應用程序,其中我正在通過串行端口向PLC發送命令並從PLC接收字符串中的數據。從PLC收到從動態字符串分割固定長度的字符串Vb.net
我已刪除了字符串的不必要的內容,並獲得最終的字符串中ReturnStr如下:
Dim WriteStr, ChkWrite As String
Dim ReadStr, ReturnStr, ChkRecd, ChkCalc As String
Dim ChkComp As Integer
WriteStr = String.Concat("01", cmd, Add, Points)
ChkWrite = Check(WriteStr)
Try
sp.WriteLine(":" & WriteStr & ChkWrite & vbCr)
ReadStr = sp.ReadLine()
Catch ReadErr As Exception
Return "0"
End Try
ChkRecd = ReadStr.Substring((((CInt("&H" & Points)) * 4) + 7), 2)
ChkCalc = Check(ReadStr.Substring(1, ((Len(ReadStr) - 4))))
ChkComp = String.Compare(ChkRecd, ChkCalc)
If ChkComp <> 0 Then
Return "0"
End If
ReturnStr = (ReadStr.Substring(7)).Remove(CInt("&H" & (Points)) * 4)
Return ReturnStr
ReturnStr返回字符串像006600D000C9006D0013B003A00014C00349,我想分成一組每個4個字符。
我的查詢是ReturnStr可能包含無限長度的數據(以4的倍數),所以我怎麼去從每個子這樣的字符串,顯示值分割字符串標籤的形式是這樣的:
lblPt1.Text = CInt("&h" & (ReturnStr.Substring(0, 4)))
lblPt2.Text = CInt("&h" & (ReturnStr.Substring(4, 4)))
lblPt3.Text = CInt("&h" & (ReturnStr.Substring(8, 4)))
lblPt4.Text = CInt("&h" & (ReturnStr.Substring(12, 4)))
lblPt5.Text = CInt("&h" & (ReturnStr.Substring(16, 4)))
lblPt6.Text = CInt("&h" & (ReturnStr.Substring(20, 4)))
lblPt7.Text = CInt("&h" & (ReturnStr.Substring(24, 4)))
lblPt8.Text = CInt("&h" & (ReturnStr.Substring(28, 4)))
有[很少](http://stackoverflow.com/questions/8774392/how-to-split-a-string-by-x-amount-of-characters)[例子](http:///stackoverflow.com/questions/7376987/how-to-split-a-string-into-a-fixed-length-string-array)[in](http://stackoverflow.com/questions/30385540/how-do -i-split-a-string-every-7-charecter-in-vb?noredirect = 1&lq = 1)該網站。 –