B4J Question SOLVED : Composing string

besoft

Active Member
Licensed User
Longtime User
hi,
Now I need your help. I have the following code:

B4X:
Sub AStream2_NewData (Buffer2() As Byte)
    Dim s2 As String = BytesToString(Buffer2, 0, Buffer2.Length, "UTF8")
    If s2.Length > 5 Then n2 = st.Right(s2,5)
    Log ("L2:" & n2)
End Sub

Sub AStream3_NewData (Buffer3() As Byte)
    Dim s3 As String = BytesToString(Buffer3, 0, Buffer3.Length, "UTF8")
    If s3.Length > 5 Then n3 = st.Right(s3,5) 
    Log ("L3:" & n3)

End Sub

Sub TC1_Tick
Dim niv As String
niv = n2 & n3
Log (niv)
niv = ""
End Sub

N2 and N3, are string format 0191 and 2345 ... so four-digit number.

Expecting string composed in the form as: 01912345

What is wrong in this code? It is always compose strings worked without problems.

It is a console application.
 

sorex

Expert
Licensed User
Longtime User
are you sure stream2 received data?

and why do you check for a length of 5 when you expect a 4 digit number?
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
2017-03-269-16-57.jpg


Thanks for the answers

Through the serial port I read two sensors for distance measurement. In the picture you can see that I get information about stream2 received information (L2, L3). I get the data every few milli seconds.

I would like to put the data in the form of 12345678, sequentially excluding L2 and L3.

and why do you check for a length of 5 when you expect a 4 digit number?
Because I remove unnecessary data. I need only these four numbers.

If I do this in code:
B4X:
Log(n2)
Log(n3)
Log (n2 & n3)

I get this:
B4X:
L2:1496
L3:1519
1496
1519
1519
L2:1494
L3:1520
L2:1493
L2:1493
L3:1523
L2:1497
L3:1523
L2:1497
L3:1523
1497
1523
1523
L2:1495
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, but still... the IF check doesn't make sense. shouldn't it be >4 or >=5 then ?

if it ain't no match n2 or n3 doesn't update at all since you read it in s2 and s3
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
It could have been ...> 4 or >= 5

Occasional happens in the stream arrive incomplete information. the correct data is in the form of R01258, it happens that I get only R01.
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
Now it looks like:

B4X:
Buffer2: 52313438370D
L2:1487
Buffer3: 52313730310D
L3:1701
Buffer2: 52313438360D52313438360D
L2:1486
Buffer3: 52313730320D
L3:1702
1486
1702
1702
 
Upvote 0
Top