Strange string degradation

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Strange string degradation

Post by sturgessb »

Evening all

Not really how to title this one other than I have a string which seems to get confused over time. Im sure there is a simple explanation for this, but can't figure it out.

So I have the following code to read a GPS NMEA stream coming in.


Code: Select all

Do	
				Call GetQueue(com3RXQueue, GPS_byte, 1)
				If GPS_byte = Asc("$") Then
					GPS_bytearrayi = 0
				End if
				GPS_bytearrayi = GPS_bytearrayi + 1
				
				GPS_bytearray(GPS_bytearrayi) = GPS_byte
				
				If GPS_byte = Asc(CRLF) Then
					GPS_string = MakeString(MemAddress(GPS_bytearray), GPS_bytearrayi)
					console(GPS_string)
					exit do
				End if
Loop

The data appears to be coming in ok and If I replace the GPS_string = MakeString line with a simple console.write(GPS_byte), then I get good clean reliable data coming out of the console log.

However, when I try to store the complete string in the GPS_string variable it starts off ok but after a few seconds starts loosing data, until i just get the occasional console output every few seconds. Very odd.

Here is example log from the console, as you will see it starts off fine and then after a few seconds (data is coming in at 1hz), it just starts to loose it.


Code: Select all

$PMTK011,MTKGPS*08

$PMTK010,001*2E

$PGACK,101*42

$PGACK,202*42

$GPGGA,232445.262,,,,,0,0,,,M,,M,,*48

$GPGSA,A,1,,,,,,,,,,,,,,,*1E

$GPRMC,232445.262,V,,,,,0.00,0.00,300513,,,N*49

$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32

$GPGGA,232446.262,,,,,0,3,,,M,,M,,*48

$GPGSA,A,1,,,,,,,,,,,,,,,*1E

$GPRMC,232446.262,V,,,,,0.77,179.63,300513,,,N*40

$GPVTG,179.63,T,,M,0.77,N,1.42,K,N*3F

$GPGGA,232447.262,5237.2472,N,00116.7721,E,1,5,2.15,20.7,M,47.0,M,,*6E

$GPGSA,A,3,32,22,03,06,19,,,,,,,,2.35,2.15,0.94*01

$GPRMC,232447.262,A,5237.2472,N,00116.7721,E,4.21,138.47,300513,,,A*63

$GPVTG,138.47,T,,M,4.21,N,7.81,K,A*3D

$GPGGA,232448.262,5237.2476,N,00116.7633,E,1,6,1.41,22.4,M,47.0,M,,*67

$GPGSA,A,3,32,22,03,06,11,19,,,,,,,1.69,1.41,0.94*09

$GPRMC,232448.262,A,5237.2476,N,00116.7633,E,3.72,129.08,300513,,,A*60

$GPVTG,129.08,T,,M,3.72,N,6.89,K,A*3E

$GPGGA,232450.000,5237.2478,N,00116.7619,E,1,5,1.41,21.6,M,47.0,M,,*6C

$GPGSA,A,3,32,22,03,06,11,,,,,,,,1.71,1.41,0.97*0B

$GPGSV,4,1,13,19,73,155,,11,57,272,27,22,45,065,28,03,44,154,29*73

$GPGSV,4,2,13,01,37,265,,06,30,142,28,33,27,200,,28,23,318,*77

$GPGSV,4,3,13,14,23,106,,32,21,201,25,18,16,049,,09,04,315,*70

$GPGSV,4,4,13,08,03,284,*4E

$GPRMC,232450.000,A,5237.2478,N,00116.7619,E,2.42,122.95,300513,,,A*64

$GPVTG,122.95,T,,M,2.42,N,4.49,K,A*3D

$GPGGA,232451.000,5237.2479,N,00116.7609,E,1,7,1.13,22.7,M,47.0,M,,*6A

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232451.000,A,5237.2479,N,00116.7609,E,1.76,121.57,300513,,,A*6C

$GPVTG,121.57,T,,M,1.76,N,3.26,K,A*3A

$GPGGA,232452.000,5237.2482,N,00116.7611,E,1,7,1.13,21.9,M,47.0,M,,*69

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232452.000,A,5237.2482,N,00116.7611,E,1.10,108.66,300513,,,A*6B

$GPVTG,108.66,T,,M,1.10,N,2.05,K,A*33

$GPGGA,232453.000,5237.2481,N,00116.7612,E,1,7,1.13,22.5,M,47.0,M,,*67

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232453.000,A,5237.2481,N,00116.7612,E,0.91,110.04,300513,,,A*6F

$GPVTG,110.04,T,,M,0.91,N,1.69,K,A*3F

$GPGGA,232454.000,5237.2481,N,00116.7616,E,1,7,1.13,23.1,M,47.0,M,,*61

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232454.000,A,5237.2481,N,00116.7616,E,0.72,95.17,300513,,,A*5F

$GPVTG,95.17,T,,M,0.72,N,1.33,K,A*03

$GPGGA,232455.000,5237.2483,N,00116.7618,E,1,7,1.13,22.8,M,47.0,M,,*64

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPGSV,4,1,13,19,73,155,25,11,57,272,28,22,45,065,27,03,44,154,29*74

$GPGSV,4,2,13,01,37,265,,06,30,142,28,33,27,200,,28,23,318,19*7F

$GPGSV,4,3,13,14,23,106,,32,21,201,25,18,16,049,,09,04,315,*70

$GPGSV,4,4,13,08,03,284,*4E

$GPRMC,232455.000,A,5237.2483,N,00116.7618,E,0.57,95.17,300513,,,A*55

$GPVTG,95.17,T,,M,0.57,N,1.06,K,A*02

$GPGGA,232456.000,5237.2482,N,00116.7617,E,1,7,1.13,22.6,M,47.0,M,,*67

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232456.000,A,5237.2482,N,00116.7617,E,0.34,95.17,300513,,,A*5D

$GPVTG,95.17,T,,M,0.34,N,0.63,K,A*05

$GPGGA,232457.000,5237.2482,N,00116.7621,E,1,7,1.13,22.6,M,47.0,M,,*63

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232457.000,A,5237.2482,N,00116.7621,E,0.41,95.17,300513,,,A*5B

$GPVTG,95.17,T,,M,0.41,N,0.76,K,A*03

$GPGGA,232458.000,5237.2482,N,00116.7622,E,1,7,1.13,22.6,M,47.0,M,,*6F

$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01

$GPRMC,232458.000,A,5237.2482,N,00116.7622,E,0.49,95.17,300513,,,A*5F

$GPVTG,95.17,T,,M,0.49,N,0.90,K,A*03

$GPGGA,232459.000,5237.2481,N,00116.7625,E,1,7,1.12,22.6,M,47.0,M,,*6B



$GPVTG,22.50,T,,M,0.60,N,1.11,K,A*0F






$GPGSV,4,4,13,08,03,284,*4E


$GPVTG,24.15,T,,M,0.59,N,1.09,K,A*0B




$GPVTG,24.15,T,,M,0.58,N,1.08,K,A*0B


$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01


$GPVTG,352.27,T,,M,0.70,N,1.30,K,A*39


$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.13,0.88*01


$GPVTG,353.90,T,,M,0.87,N,1.62,K,A*3B


$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.12,0.88*00


$GPVTG,351.75,T,,M,0.98,N,1.83,K,A*33


$GPGSA,A,3,28,32,22,03,06,11,19,,,,,,1.43,1.12,0.88*00




$GPGSV,4,4,13,08,03,284,*4E


$GPVTG,352.87,T,,M,1.08,N,1.99,K,A*3E




$GPVTG,342.72,T,,M,0.95,N,1.76,K,A*31




$GPVTG,339.84,T,,M,0.83,N,1.54,K,A*33




$GPVTG,350.93,T,,M,0.94,N,1.73,K,A*39




$GPVTG,340.81,T,,M,1.01,N,1.87,K,A*3D






$GPGSV,4,4,13,08,03,284,*4E


$GPVTG,343.65,T,,M,1.11,N,2.06,K,A*3F




$GPVTG,341.71,T,,M,1.09,N,2.03,K,A*34






$GPVTG,327.92,T,,M,1.13,N,2.10,K,A*30




$GPVTG,326.46,T,,M,1.08,N,2.00,K,A*33




$GPVTG,326.16,T,,M,1.15,N,2.13,K,A*38






$GPGSV,4,4,13,08,03,284,*4E


$GPVTG,323.90,T,,M,1.19,N,2.21,K,A*3E




$GPVTG,327.74,T,,M,1.03,N,1.92,K,A*30




$GPVTG,334.17,T,,M,1.02,N,1.89,K,A*3C




$GPVTG,337.48,T,,M,1.04,N,1.93,K,A*38




$GPVTG,336.83,T,,M,1.01,N,1.87,K,A*3E






$GPGSV,4,4,13,08,03,284,*4E


$GPVTG,336.03,T,,M,0.96,N,1.79,K,A*38




$GPVTG,335.44,T,,M,0.96,N,1.78,K,A*39




$GPVTG,323.39,T,,M,0.99,N,1.84,K,A*38




$GPVTG,324.69,T,,M,0.99,N,1.83,K,A*3D




$GPVTG,326.79,T,,M,0.82,N,1.53,K,A*39
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

interestingly, if i just do this....

Code: Select all

console(MakeString(MemAddress(GPS_bytearray), GPS_bytearrayi))
rather than saving inside the GPS_string variable, it outputs fine without any degradation.

but if i do this it fails after a few seconds...

Code: Select all

GPS_string = MakeString(MemAddress(GPS_bytearray), GPS_bytearrayi)
console(GPS_string)
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

What version of the ZBasic Compiler are you using? This is a native mode target, correct?
- Don Kinzer
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

Hi Don

Using v1.6.2 IDE with 4.0.3 compiler. target is zx328nu

Thanks

Ben
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

have tried the latest compiler and same issue present.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Here is a self-contained test case that is similar to the code that you provided. It uses a separate task to populate a queue with artificial GPS data and then the main task processes the data. Compiled with v4.1.7 for the ZX-24n it seems to work flawlessly. Could you try this on the ZX-328nu and if it works OK try to modify it so that it fails?

Code: Select all

Dim q(1 to 100) as Byte
Dim taskStack(1 to 100) as Byte

Sub Main()
    Dim cnt as Byte
    Dim data(1 to 80) as Byte

    ' prepare the queue, start the data task
    Call OpenQueue(q)
    CallTask dataTask, taskStack
    Sleep(0.0)
    
    ' pull GPS data from the queue and display it
    cnt = 0
    Do
        Do While StatusQueue(q)
            Dim b as Byte
            Call GetQueue(q, b, 1)
            If (b = &H0d) Then
                ' a full line has been received
                Dim s as String
                s = MakeString(MemAddress(data), cnt)
                console(s)
                cnt = 0
            Else
                If (b = Asc("$")) Then
                    ' reset the counter on receipt of the lead-in character
                    cnt = 0
                End If
                
                ' store the data
                If &#40;cnt < SizeOf&#40;data&#41;&#41; Then
                    cnt = cnt + 1
                    data&#40;cnt&#41; = b
                End If
            End If
        Loop
        Call Sleep&#40;1.0&#41;
    Loop
End Sub

Sub console&#40;ByRef s as String&#41;
    Console.WriteLine&#40;s&#41;
End Sub

'
'' dataTask
'
' This task puts artificial GPS data in the queue for processing.
'
Sub dataTask&#40;&#41;
    Do
        If Not StatusQueue&#40;q&#41; Then
            Call PutQueueStr&#40;q, "$GPGGA,232447.262,5237.2472,N,00116.7721,E,1,5,2.15,20.7,M,47.0,M,,*6E " & Chr&#40;&H0d&#41;&#41;
        End If
        Call Sleep&#40;1.0&#41;
    Loop
End Sub
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

For the benefit of someone who might read this thread later, the source of the problem turned out to be "programmer error". A string (that could have been several characters long) was being assigned to a BoundedString variable that was defined as having a 1-byte capacity. With BoundedString variables, the coder must ensure that a string being assigned does not exceed the defined capacity of the bounded string. If this is not done, the characters beyond the capacity overwrite whatever follows the variable in memory.
- Don Kinzer
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

PEBKAC!
Post Reply