Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Indexing a pointer to String inside a function


dontunderstand Nov 4, 2022 11:47 AM

Hi,

I'm trying to implement a function to calculate a NMEA checksum (XOR of all the bytes of the string, excluding the opening $ and the closing *). I'm trying to do this by passing the argument as a pointer to the string in question, but I'm having trouble indexing the string once inside the function.

I have this code:

 

'This doesn't work. Len(!InputString) returns 23, but the contents are 0 apart from the initial "P"
Function CalculateCheckSum(InputString As String!) As String Dim Index Dim CheckSumNumber As Long Dim CheckSumString As String Erase(CheckSumString) CheckSumNumber = 0 For Index = 1 To Len(!InputString) Step 1 CheckSumNumber = CheckSumNumber XOR ASCII(!InputString 1,1,Index)) Next Index CheckSumString = HEX(CheckSumNumber) Return CheckSumString EndFunction

BeginProg
Public String1 As String = "P,3,WA,3,25,0.41,1.00,0"
Public CheckSum1 As String
CheckSum1 = CalculateCheckSum(@String1)
EndProg

When I ran this I noticed that CheckSum1 was calculated as 0x50, which is incorrect for this particular string. 0x50 happens to be the ASCII code for "P", so then I went and individually checked all the bytes in the string inside the function, and noticed that apart from the initial "P", everything else was zero. However, the function Len(!InputString) returns 23, which is the correct length of the input string. I then created a test string inside the function, copied the contents of the pointer into it, and calculated the checksum of the test string instead, and this time it worked. However, this defeats the purporse of passing the data as a pointer to begin with. Is there something I'm missing?

'This works. Len(TestString) returns 23 and all the data is correct
Function CalculateCheckSum(InputString As String!) As String Dim Index Dim TestString As String * 200 Dim CheckSumNumber As Long Dim CheckSumString As String MoveBytes(TestString,0,!InputString,0,200) Erase(CheckSumString) CheckSumNumber = 0 For Index = 1 To Len(TestString) Step 1 CheckSumNumber = CheckSumNumber XOR ASCII(TestString(1,1,Index)) Next Index CheckSumString = HEX(CheckSumNumber) Return ("*" & CheckSumString) EndFunction

BeginProg
Public String1 As String = "P,3,WA,3,25,0.41,1.00,0"
Public CheckSum1 As String
CheckSum1 = CalculateCheckSum(@String1)
EndProg

 Thanks


dontunderstand Nov 7, 2022 12:03 PM

Forgot to mention I'm using a CR300 datalogger with PC400.

Log in or register to post/reply in the forum.