我有一个带有richtextbox的win表单,在我给出一些命令(如AT + DSCA和AT + RSSI)之后从串行COM端口读取这两个命令一个接一个地执行 收到的字符串如下: 对于+ dscan,字符串就像: ZC * | 0001950000008F8B | 0000 | PTv1.11 | ZE20S | ProBee-ZE ,我有以下vb.net代码来拆分此字符串并将其插入datagridview :前两个值。
Private Sub SalveazaData()
Dim Rows As String() = rtbComData.Text.Split(Environment.NewLine.ToCharArray())
For Each Row As String In Rows
If Not (Row = "AT+DSCAN" Or Row = "OK" Or Row = "") Then
Dim sAry As String() = Split(Row, "|")
Dim aRow As smdDataDataSet1.smdTableRow = SmdDataDataSet1.smdTable.NewsmdTableRow()
aRow.Model = sAry(0)
aRow.AdresaUnica = sAry(1)
aRow.StatusModul = "ACTIVE"
Try
SmdDataDataSet1.SearchAdrese.Rows.Add(sAry(1))
SmdDataDataSet1.smdTable.Rows.Add(aRow)
Catch ex As Exception
Dim u As String
u = SmdTableTableAdapter.UpdateInactivActiv()
End Try
End If
Next
对于第二个命令,AT + RSSI女巫的字符串如下:发件人:0000,父:FFFF,RSSI:0,LQI:255 不能在前一个字符串的同一行插入值255 !我只想在上面的字符串中插入值 - " 255"
答案 0 :(得分:0)
我理解了您的需求,解析来自COM端口的值并使用这些值在datagridview中显示。 //找出哪个索引具有此长字符串值
//“ProBee-ZE Sender:0000,Parent:FFFF,RSSI:0,LQI:255 Sender:0000,”
//例如我们有sAry(3)。
Dim tAry As String() = Split(sAry(3), ",")
//你会在tAry(3)中得到LQI:255,并且为获得255而做同样的过程
Dim uAry As String() = Split(tAry(3), ":")
//你会得到值为255的uAry(1) //你需要将这段代码放在for循环中。
For Each Row As String In Rows
If Not (Row = "AT+DSCAN" Or Row = "OK" Or Row = "") Then
Dim sAry As String() = Split(Row, "|")
Dim aRow As smdDataDataSet1.smdTableRow = SmdDataDataSet1.smdTable.NewsmdTableRow()
aRow.Model = sAry(0)
aRow.AdresaUnica = sAry(1)
aRow.StatusModul = "ACTIVE"
aRow.thecolumnwhichuwant = uAry(1)
Try
SmdDataDataSet1.SearchAdrese.Rows.Add(sAry(1))
SmdDataDataSet1.smdTable.Rows.Add(aRow)
Catch ex As Exception
Dim u As String
u = SmdTableTableAdapter.UpdateInactivActiv()
End Try
End If
Next