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.

NULL character handling


Kruggerand Jun 19, 2015 03:02 PM

I would like to know if there are any tips on how to import a string of serial data into a CR3000 datalogger if the incoming string contains NULL characters interspersed throughout it or alternatively how to convert hex characters back into ASCII characters in CRBASIC.
I am outputting the serial command “$GET DS DC” from the CR3000 datalogger to the device to be controlled successfully.
The information returned from the device is of the structure “25390,24098,scc/min,02,010,28.0,C,757,mmHg,00.0,C,,,11:26,06/17/15, DEFINER 220,,”
However, only the first part of the string (“25390,24098,scc/min”) becomes recognized as the whole string by the datalogger because there is a NUL character after every alphanumeric part in the string.
I know the whole string is received by the datalogger because when I query out the individual parts of the string using ASCII() function I can view each piece in hex format.
So in order to capture the entire incoming string as ASCII characters , I would need either
a) To input the entire string such that the NULL characters are ignored.
b) To find a way to convert hex characters back to ASCII characters in CRBASIC ( I couldn’t find any function to do that).


thommark Jun 19, 2015 03:22 PM

You might take a look at the SerialInRecord or SerialInBlock command. These might be options that will allow you to read the entire string received from your device. If your returned data is always the same number of bytes then SerialInBlock should work. But SerialInRecord allows you to choose the start and end word for the data you want to store.


Kruggerand Jun 19, 2015 04:43 PM

Thanks for the info.

I was using SerialInBlock but with no luck.

My syntax was

SerialOutBlock (COMPRT, "$GET DS DC" & CHR (13) & CHR (10),12)
Delay (1,4,2)
SerialInBlock(COMPRT,DEFINERstring,108)


I'll try SerialInRecord as you suggested to see if it will work


JDavis Jun 19, 2015 09:17 PM

All string functions stop processing when they reach the first null character. It is the nature of null terminated string data formats.

Below is a program example of replacing nulls in a string with a parseable character like space.


Public Unchanged As String, NoNulls As String
Dim k As Long
Const MaxLength = 4 'Maximum length of the data in your string

'Main Program
BeginProg
Scan (1,Sec,0,0)
'Load both strings with the same data, which contains a null character
MoveBytes (Unchanged,0,&h31320033,0,4)
MoveBytes (NoNulls,0,&h31320033,0,4)
'Loop to find and replace null characters
For k = 1 To MaxLength
If ASCII(NoNulls(1,1,k)) = 0 Then
MoveBytes (NoNulls,k-1,&h20000000,0,1) 'Replace null with a space. MoveBytes uses a 0 based index.
EndIf
Next k

NextScan
EndProg


Kruggerand Jun 23, 2015 12:58 PM

Yes I see the strategy you are taking. I will try it.

Thanks!


Kruggerand Jun 25, 2015 08:30 PM

I got the string inputted successfully now but there is one odd quirk still remaining.

Instead of replacing the NULL with a space, the coding replaces the NULL with the numeral 2 or numeral 3.


Here is my version of the coding involved,

MoveBytes (Unchanged, 0, DEFINERstring,0,108)
MoveBytes (NoNulls, 0, DEFINERstring,0,108)
For k =1 To MaxLength
If ASCII(NoNulls (1,1,k)) = 0 Then
MoveBytes(NoNulls,k-1,Unchanged,0,1)
EndIf
Next k

-------------------------------------------------------

My original input line is;

13119,13119, scc/min NUL, 01,010,29.9,C,754,mmHg NUL,00.0,
C,,,12:50,06/25/15,DEFINER NUL 220, Base NUL, 129658, 2.09, DEFINER


and after replacing the NULLs with the above coding I get;

13119,13119, scc/min3, 01,010,29.9,C,754,mmHg3,00.0,
C,,,12:50,06/25/15,DEFINER2220, Base2, 129658, 2.09, DEFINER


Any ideas what is causing this behaviour?


JamesMc Jun 6, 2018 08:52 PM

I am having the same problem with the example code as posted, NULLS are being replaced with a '3'.

      'Remove NUL chars from string
      'Sample code from https://www.campbellsci.com/forum?forum=1&l=thread&tid=2475
      'Load both strings with the same data, which contains a null character
      MoveBytes (MApCO2_Rx,0,&h31320033,0,4)
      MoveBytes (NoNulls,0,&h31320033,0,4)
      'Loop to find and replace null characters
      For k = 1 To 2500
        If ASCII(NoNulls(1,1,k)) = 0 Then
          MoveBytes (NoNulls,k-1,&h20000000,0,1) 'Replace null with a space. MoveBytes uses a 0 based index.
        EndIf
      Next k

 Further reading of the MoveBytes made this example code more clear to me.  I have solved my problem.  Thanks

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