Android Question Problem with parsing. Trim data line by line

milanjo

Member
Licensed User
Longtime User
Hello
I'm communicating by usb hub with TinyG
I use serial library.
I should get data line by line in json so i want to parse some data (positions etc) into variable from data which i get in json.
I use this :

B4X:
Sub Astreams_NewData (Buffer() As Byte)
   
    If EditTextgcodevystup.Text.Length > 10000 Then EditTextgcodevystup.Text = ""

'it will delete output text when length of data is more than 10000

    Dim prijate = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(prijate)
    'spracuje
   
    Dim parser As JSONParser
    parser.Initialize(prijate)
    Dim root As Map = parser.NextObject
    Dim sr As Map = root.Get("sr")
    Dim vel As Double = sr.Get("vel")
    Dim posx As Double = sr.Get("posx")
    label1.Text = "  X = "&posx
'set text with position of x
    iks = posx
'set variable which contains position of x
    EditTextgcodevystup.Text = EditTextgcodevystup.Text & prijate & "*"
'put data which i get into my output edittext
    EditTextgcodevystup.SelectionStart = EditTextgcodevystup.Text.Length   
End Sub


Where : EditTextgcodevystup - edittext with output data
prijate - string containing data which i get
iks - variabla containing position x (get from parsing ,,prijate,, data)
label1 - if graphical part of aplication which is showing and x position like for example (X = 27)

Like i write i should get data line by line (everytime prijate should contain one line of data in json format)
To i see what data i get (in how part i get it) i adding an start (*) after each part of data which i get so the
output should be like :

B4X:
...somedata...*
...other data...*
...blablabla...*
...other....*
...something...*


But my output see like :

B4X:
...somedata...*
...other data...*
...blablabla...*
...other...*...
...so*mething...
...otherthing*...
...blablabla...*

Where isn't the star still in the end of line where should be if i get data line be line -> that means data doesn't coming to me every time line by line
But if i wan't to parse that data it must come to parse line by line in json to valid parse.

So what i should to to separate that coming data line by line and send to parse (parser.initialize(prijate)) only full line with json (only one line) that meand data from one clrf to other clrf(new line) like ( CLRF somedata which i want to parse CLRF)
Thank's for help.

Have a nice day :)
 

milanjo

Member
Licensed User
Longtime User
Thank's, sorry for late respond. It work like a charm :) It solve problem with i have again thank's but other problem happense (that problem was too before when i'm not using asynstreams text class). Problem is the data somethimes doesn't come to me in the correct order. For example i'm should get location which is changing and then ok (in reality i getting json code which i parsing that's only example to explain my problem). I should look like that :
B4X:
1
2
3
4
5
6
7
8
9
10
OK
I get that about 8 from 10 times (it should like i up write it)
But about 2times from 10 (it seems to happening randomly)
i get this (which is bad) :
B4X:
1
2
3
4
5
6
10
OK
7
8
9
OR this and etc. :
B4X:
1
2
3
4
5
8
9
10
OK
6
7
And i'm parsing incomming numbers (positions from json which i get)
and if i get it in correct order i should have in my variable in this case number 10 which agrees with true location.
But if the mistake (error) happense i will have in my variable in the second case number 9 or in the third case number 7 but thats the problem because device which i communicating is on the location 10.

Please help me how to fix it, can json parser do that mistake? (i don't remeber when i'm not parsing with json parser that happening).

Sorry for my english i'm from slovakia :) Thank's for support, have a great day :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
WHERE do you get this? Can you post example code which shows this behaviour?. Without seeing any code it´s just guessing what we can do to help
 
Upvote 0

milanjo

Member
Licensed User
Longtime User
Erel thank's thats my problem, somethimes json parser show to me and error message (cannot parse some numbers - i will post you log), and then it started to coming in incorrect order :) i will post json parsing script and when it happens thank you.
 
Upvote 0

milanjo

Member
Licensed User
Longtime User
I sent this to the device

B4X:
G1 Y1 F350

I get and parse this
B4X:
{"r":{"gc":"G1Y1F350","f":[1,0,11,6233]}}
{"sr":{"posy":0.001,"vel":10.94 `,"momo":1,"stat":5}}
{"qr":27}
{"sr":{"posy":0.373,"vel":350.00}}
{"sr":{"posy":0.940,"vel":251.56}}
{"sr":{"posy":1.000,"vel":0.00,"stat":3}}
{"qr":28}

Everything is ok but i do this again more time until one time it crash(my problem happens) - an that dialog show to me

B4X:
An error has occurred in sub:main_astreams2_newtext
(java line: 458)
java.lang.NumberFormatException: Invalid double:
"10.00 `1"
Continue?

After that another dialog shows

B4X:
An error has occurred in sub:main_astreams2_newtext
(java line: 453)
org.json.JSONException: Unterminated object at character 17 of {"sr":{"posy":10{"sr":{"posy":10.390}}
Continue?

(I in both cases click on continue-yes)

I sended to device
B4X:
G1 Y11 F110
(it doesnt mather what is it after Y, and after F, that problem happens randomly)

and i get this
B4X:
{"r":{"gc":"G1Y11F110","f":[1,0,12,8199]}}
{"qr":27}
{"sr":{"posy":10.027,"vel":101.20}}
{"sr":{"posy":10.920}}
{"sr":{"posy":11.000,"vel":0.00,"stat":3}}
{"qr":28}
{"sr":{"posy":10.563}}
{"sr":{"posy":10.207,"vel":110.00}}
.746}}

You can see incorrect order (correct order is in the first output which i post) and something missing on the last line
I parse data with that :

label2.text - contain value of y for example if y=2 then label2.text = "Y = 2"
ypsilon - variable which contains value of y for example if y=2 then ypsilon = "2"
EditTextgcodevystup - edit text with data which i get from the device

B4X:
Sub astreams2_NewText(Text As String)
   
    If EditTextgcodevystup.Text.Length > 10000 Then EditTextgcodevystup.Text = "" 
    Dim prijate As String
    prijate = Text
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        If prijate.Contains("{" & QUOTE & "sr" & QUOTE & ":{" & QUOTE & "posx" & QUOTE & ":") Then
        If prijate.Contains("}}") Then
        Dim parser As JSONParser
        parser.Initialize(prijate)
        Dim root As Map = parser.NextObject
        Dim sr As Map = root.Get("sr")
        'Dim vel As Double = sr.Get("vel")
        Dim posx As Double = sr.Get("posx")
        label1.Text = "  X = "&posx
        iks = posx
        'Msgbox(iks, "prislo")
        End If
        End If
    'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
        If prijate.Contains("{" & QUOTE & "sr" & QUOTE & ":{" & QUOTE & "posy" & QUOTE & ":") Then
        If prijate.Contains("}}") Then
        Dim parser As JSONParser
        parser.Initialize(prijate)
        Dim root As Map = parser.NextObject
        Dim sr As Map = root.Get("sr")
        'Dim vel As Double = sr.Get("vel")
        Dim posy As Double = sr.Get("posy")
        label2.Text = "  Y = "&posy
        ypsilon = posy
        'Msgbox(iks, "prislo")
        End If
        End If
    'ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
        If prijate.Contains("{" & QUOTE & "sr" & QUOTE & ":{" & QUOTE & "posz" & QUOTE & ":") Then
        If prijate.Contains("}}") Then
        Dim parser As JSONParser
        parser.Initialize(prijate)
        Dim root As Map = parser.NextObject
        Dim sr As Map = root.Get("sr")
        'Dim vel As Double = sr.Get("vel")
        Dim posz As Double = sr.Get("posz")
        label3.Text = "  Z = "&posz
        zet = posz
        'Msgbox(iks, "prislo")
        End If
        End If
    'odomkni buttony
        If prijate.Contains("{"&QUOTE&"qr"&QUOTE&":28}") Then
        zmenbuttony("zapnut")
        End If
       
    'konec
   
    EditTextgcodevystup.Text = EditTextgcodevystup.Text & prijate & CRLF & "*"
    EditTextgcodevystup.SelectionStart = EditTextgcodevystup.Text.Length   
End Sub
 
Upvote 0
Top