Italian Flusso dati seriale...

stefanoxjx

Active Member
Licensed User
Ciao a tutti, sto sviluppando un piccolo programma con B4J che dovrebbe prendere dei dati NMEA da un GPS e salvarli in un file.
Ho sviluppato praticamente tutto il programma, però mi trovo che quando vado a salvare i dati, questi risultano ingarbugliati e non come effettivamente sono stati scaricati.
Per esempio, questo è il flusso dati ricevuto:
$GPRMC,175644.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*15
$GPRMC,175645.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*14
$GPRMC,175646.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*17
$GPRMC,175647.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*16
$GPRMC,175648.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*19
$GPRMC,074818.000,A,4537.7456,N,01201.6455,E,9.71,0.00,030215,,*0E
$GPRMC,074823.000,A,4537.7425,N,01201.6086,E,21.38,0.00,030215,,*3F
$GPRMC,074830.000,A,4537.7290,N,01201.5378,E,27.21,0.00,030215,,*3A
$GPRMC,074836.000,A,4537.7303,N,01201.4739,E,27.21,0.00,030215,,*37
$GPRMC,074842.000,A,4537.7433,N,01201.4092,E,27.21,0.00,030215,,*36
$GPRMC,074848.000,A,4537.7561,N,01201.3460,E,27.21,0.00,030215,,*34
$GPRMC,074854.000,A,4537.7737,N,01201.2804,E,29.15,0.00,030215,,*3E
$GPRMC,074901.000,A,4537.8011,N,01201.2025,E,33.04,0.00,030215,,*33
$GPRMC,074907.000,A,4537.8267,N,01201.1277,E,34.98,0.00,030215,,*32
$GPRMC,074913.000,A,4537.8421,N,01201.0461,E,34.98,0.00,030215,,*33



$GPRMC,074919.000,A,4537.8547,N,01200.9623,E,33.04,0.00,030215,,*36
$GPRMC,074925.000,A,4537.8649,N,01200.8876,E,27.21,0.00,030215,,*39
$GPRMC,074932.000,A,4537.8700,N,01200.8463,E,5.83,0.00,030215,,*03
$GPRMC,074938.000,A,4537.8744,N,01200.8111,E,21.38,0.00,030215,,*3F
$GPRMC,074944.000,A,4537.8881,N,01200.7460,E,33.04,0.00,030215,,*32
$GPRMC,074950.000,A,4537.9069,N,01200.6621,E,38.87,0.00,030215,,*3E
$GPRMC,074956.000,A,4537.9220,N,01200.5696,E,36.93,0.00,030215,,*33
$GPRMC,075003.000,A,4537.9344,N,01200.4653,E,36.93,0.00,030215,,*30
$GPRMC,075009.000,A,4537.9411,N,01200.3724,E,36.93,0.00,030215,,*3B
$GPRMC,075015.000,A,4537.9458,N,01200.2823,E,36.93,0.00,030215,,*32
$GPRMC,075021.000,A,4537.9523,N,01200.1909,E,38.87,0.00,030215,,*39
$GPRMC,075027.000,A,4537.9597,N,01200.0980,E,38.87,0.00,030215,,*30
$GPRMC,075034.000,A,4537.9840,N,01159.9978,E,36.93,0.00,030215,,*3F
$GPRMC,075040.000,A,4538.0302,N,01159.9319,E,38.87,0.00,030215,,*31
$GPRMC,075046.000,A,4538.0811,N,01159.8737,E,38.87,0.00,030215,,*37



$GPRMC,075052.000,A,4538.1311,N,01159.8151,E,36.93,0.00,030215,,*35
$GPRMC,075058.000,A,4538.1787,N,01159.7593,E,34.98,0.00,030215,,*38
$GPRMC,075104.000,A,4538.2237,N,01159.7046,E,33.04,0.00,030215,,*32
$GPRMC,075111.000,A,4538.2579,N,01159.6286,E,33.04,0.00,030215,,*34
$GPRMC,075117.000,A,4538.2688,N,01159.5460,E,34.98,0.00,030215,,*30
$GPRMC,075123.000,A,4538.2778,N,01159.4598,E,34.98,0.00,030215,,*3E
$GPRMC,075129.000,A,4538.2850,N,01159.3905,E,21.38,0.00,030215,,*30
$GPRMC,075135.000,A,4538.2857,N,01159.3464,E,19.43,0.00,030215,,*37
$GPRMC,075142.000,A,4538.2753,N,01159.2687,E,33.04,0.00,030215,,*39
$GPRMC,075148.000,A,4538.2636,N,01159.1816,E,38.87,0.00,030215,,*34
$GPRMC,075154.000,A,4538.2487,N,01159.0873,E,38.87,0.00,030215,,*33
$GPRMC,075200.000,A,4538.2349,N,01158.9978,E,34.98,0.00,030215,,*34
$GPRMC,075206.000,A,4538.2262,N,01158.9331,E,17.49,0.00,030215,,*30
$GPRMC,075218.000,A,4538.2285,N,01158.9035,E,13.60,0.00,030215,,*3E
$GPRMC,075224.000,A,4538.2151,N,01158.8611,E,23.32,0.00,030215,,*3E
e questo è parte del contenuto del file salvato:

$GPRMC,114831.000,A,4537.8518,N,01200.9690,E,34.98,
0.00,030215,,*3A
$GPRMC,114837.000,A,4537.8380,N,01201.0573,E,36.93,0.00,030215,,*34
$GPRMC,1
844.000,A,4537.8145,N,01201.1560,E,36.93,0.00,030215,,*38
$GPRMC,114850.000,A,4537.7885,N,01201.2353,E,34.98,0.00,030215,,
*3B
$GPRMC,114856.000,A,4537.7639,N,01201.3086,E,31.10,0.00,030215,,*3B
$GPRMC,114902.000,A,
4537.7458,N,01201.3851,E,33.04,0.00,030215,,*3B
$GPRMC,114908.000,A,4537.7307,N,01201.4637,E,3
3.04,0.00,030215,,*35
$GPRMC,114914.000,A,4537.7263,N,01201.5430,E,33.04,0.00,030215,,*3F
$GPRM
C,114921.000,A,4537.7426,N,01201.6298,E,27.21,0.00,030215,,*3B
$GPRMC,114927.000,A,4537.7458,N
$GPRMC,175644.000,V,4537.7565,N,01201.6674,E,0.00,0.00
GPRMC,175645.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*14
$GPRMC,175646.000,V,4537.756
01.6674,E,0.00,0.00,260115,,*17
$GPRMC,175647.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,

$GPRMC,175648.000,V,4537.7565,N,01201.6674,E,0.00,0.00,260115,,*19
$GPRMC,074818.000,A,4537.
56,N,01201.6455,E,9.71,0.00,030215,,*0E
$GPRMC,074823.000,A,4537.7425,N,01201.6086,E,21.38,0.00
15,,*3F
$GPRMC,074830.000,A,4537.7290,N,01201.5378,E,27.21,0.00,030215,,*3A
$GPRMC,074836.000,A
303,N,01201.4739,E,27.21,0.00,030215,,*37
$GPRMC,074842.000,A,4537.7433,N,01201.4092,E,27.21,
15,,*36
$GPRMC,074848.000,A,4537.7561,N,01201.3460,E,27.21,0.00,030215,,*34
$GPRMC,074854.000,A
37,N,01201.2804,E,29.15,0.00,030215,,*3E
$GPRMC,074901.000,A,4537.8011,N,01201.2025,E,33.04,0.00
15,,*33
$GPRMC,074907.000,A,4537.8267,N,01201.1277,E,34.98,0.00,030215,,*32
$GPRMC,074913.000,
RMC,074919.000,A,4537.8547,N,01200.9623,E,33.04,0.00,030215,,*36
$GPRMC,074925.0
8649,N,01200.8876,E,27.21,0.00,030215,,*39
$GPRMC,074932.000,A,4537.8700,N,01200.8463,E,5.83,0.
,,*03
$GPRMC,074938.000,A,4537.8744,N,01200.8111,E,21.38,0.00,030215,,*3F
$GPRMC,074944.000,
200.7460,E,33.04,0.00,030215,,*32
$GPRMC,074950.000,A,4537.9069,N,01200.6621,E,38.87,0.00,0302
56.000,A,4537.9220,N,01200.5696,E,36.93,0.00,030215,,*33
$GPRMC,075003.000,A,4537.9344,N,01200.4
,0.00,030215,,*30
$GPRMC,075009.000,A,4537.9411,N,01200.3724,E,36.93,0.00,030215,,*3B
$GPRMC,07
5015.000,A,4537.9458,N,01200.2823,E,36.93,0.00,030215,,*32
$GPRMC,075021.000,A,4537.9523,N,01200
.87,0.00,030215,,*39
$GPRMC,075027.000,A,4537.9597,N,01200.0980,E,38.87,0.00,030215,,*30
$GPRMC
.000,A,4537.9840,N,01159.9978,E,36.93,0.00,030215,,*3F
$GPRMC,075040.000,A,4538.0302,N,01159.93
.00,030215,,*31
$GPRMC,075046.000,A,4538.0811,N,01159.8737,E,38.87,0.00,030215,,*37
$GPRMC,075052.000,A,4538.1311,N,01159.8151,E,36.93,0.00,030215,,*35
$GPRMC,075058.000,A,4538.1787
98,0.00,030215,,*38
$GPRMC,075104.000,A,4538.2237,N,01159.7046,E,33.04,0.00,030215,,*32
$GPRM
C,075111.000,A,4538.2579,N,01159.6286,E,33.04,0.00,030215,,*34
$GPRMC,075117.000,A,4538.2688,N,
01159.5460,E,34.98,0.00,030215,,*30
$GPRMC,075123.000,A,4538.2778,N,01159.4598,E,34.98,0.00,0302

$GPRMC,075129.000,A,4538.2850,N,01159.3905,E,21.38,0.00,030215,,*30
$GPRMC,075135.000,A,4538.
N,01159.3464,E,19.43,0.00,030215,,*37
$GPRMC,075142.000,A,4538.2753,N,01159.2687,E,33.04,0.00,0
$GPRMC,075148.000,A,4538.2636,N,01159.1816,E,38.87,0.00,030215,,*34
$GPRMC,075154.000,A,4538.2
159.0873,E,38.87,0.00,030215,,*33
$GPRMC,075200.000,A,4538.2349,N,01158.9978,E,34.98,0.00,03021
,*34
$GPRMC,075206.000,A,4538.2262,N,01158.9331,E,17.49,0.00,030215,,*30
$GPRMC,075218.000,A,45
1158.9035,E,13.60,0.00,030215,,*3E
Il programma non fa altro che controllare su new_data se i dati ricevuti iniziano per $GPRMC e
in tal caso prende la riga e la inserisce in una "list":
B4X:
Sub AStream_NewData (Buffer() As Byte)
   Dim S As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")

   If S.Contains("$GPRMC") Then   GPRMCString.Add(S)

End Sub
e alla fine salva la "list" in un file:
B4X:
File.WriteList(File.DirApp, DateTime.now & ".txt", GPRMCString)
Penso di aver capito che il problema sta nel fatto che quando AStream_NewData riceve dei dati, non è detto che sia per forza una riga intera e quindi io vado ad inserire nella list una mezza riga o una riga e mezza o giù di li.
Ora, la mia domanda è...
Se ho capito bene cosa genera l'anomalia, secondo voi qual'è la via migliore per ovviare a questo problema?

Che ne pensate?
Grazie.
Ciao.
 

stefanoxjx

Active Member
Licensed User
In alternativa, chiedo se c'è modo di leggere il contenuto di una textarea riga per riga.
Grazie.

Stefano
 

stefanoxjx

Active Member
Licensed User
Trovato, era banalmente textarea.text.
Non legge proprio riga per riga, ma per quello mi posso arrangiare :)
 
Top