"Strings" with WC32

rossw

Active Member
I have a chunk of code I'd like to re-use, because the same code can be used for adjusting 18 different process variables.
The fly in the ointment is, I want to pass a different string (str1, str2, str3, etc) in to the routine.
 
While the compiler is happy to accept
 
   set ram10 str1
and
   set ram10 str2
etc etc
 
if I then try to do this:
 
  print1 ram10
 
it prints the value "1", not the string. So the compiler isn't "clever" enough to pass a pointer to the string, and clearly a 4-byte variable isn't long enough to hold a full string, can anyone think of an elegant way to achieve it?
 
I DON'T consider an "elegant" solution to be:
 
    tsteq ram10 1
    print1 str1
    tsteq ram10 2
    print1 str2
....
    tsteq ram10 18
    print1 str18
 
 
 
Unfortunately, no comments on this.. so can I try for another thing that's causing me problems?
 
I want to display a line of text, something like:
 
  INLET:    33.6C
  OUTLET: 56.1C
 
I can do
 
PRINT1 "INLET " T1 "C"
But of course, it does not show T1 as "33.6", it shows as "336", which is no help.
The compiler also (wrongly) strips the trailing space from "INLET ", so the temperature is hard against the word "INLET", and does not let me align text for neat displays.
 
I can print a space by printing the character 128, eg:
 
PRINT1 "VALUE" 128 RAM3  - the 128 prints as a blank space, but I can add no more text due to the 3 parameter limit.
 
How can I print more complex information on each line of the display?
 
Eg, 
  DIV T1 10 RAM1      (degrees)
  MOD T1 10 RAM2   (tenths of degrees)
  PRINT1 "INLET" 128 RAM1 "." RAM2 "C"
 
It would almost be enough (now) to get away with:
 1. fixing the trailing-space removal (and leading spaces if it does the same thing)
 2. adding some command to "interpret" numbers as if they are in tenths.
 
(Perhaps   PRINT10 and PRINT20 - that would automatically add a decimal point before the last digit of any RAM/VAR/Tx/etc)
 
Ross,
 
Since inside this CPU, there is no floating pointer, it is harder for this to accomplish. But we can add an opcode to do formatting, like C program style, to allow up to 32 bit number being formatted to a string, then print that out.
 
What size string buffer should we allocate to best assist you for this WC32 print function?
 
BTW, WC32 with USB key inserted on its USB bus allows user to store and retrieve variables by
SAVE "myvaribl" T1
then later on
LOAD "myvaribl" RAM1
where myvaribl can be any 8 bytes string.
 
However, since there is no floating point calculation, we still need to add a formatting command to do the trick you wanted.
 
Hi Wayne - yes, I recognise there is not floating point, but fixed-point should be easily achievable.
We already work  in "tenths of degrees" with the temperature sensors, being able to display them to this precision in a human-readable way would be highly desirable.
So merely displaying a number with an implied decimal would achieve that.
 
A string buffer of as little as 16 bytes would be enough for my immediate purposes, since I don't like the display horizontal scrolling in my current application - but that may not hold true for everyone, or indeed for me in a later project :)  Lets say "Anything over 16 bytes would be a bonus, and anything longer than the UM216 display can handle would be a waste" :)
 
Writing a variable to USB and reading it back unfortunately still doesn't help with the number of items on a print-line, unless I can "print" to a variable (up to 3 items), then "print" to another variable (another 3 items), then print TWO items - each being the three previous ones (making 6!). But we don't have a way to do that. Also, since my displays are constantly rolling through a list of process variables (most calculated - like the calculated megajoules, or instantaneous kilowatts) - I would wear out a flash drive fairly quickly. (Even if a USB has a life of a million write cycles, I'd burn that up in less than 2 weeks)
 
We are thinking in a line like MATLAB sprintf function with only one 32bit INT being formatted, to decimal, or hex, or octal.  That can be expanded to others later based on demands.  Since PRINT1 and PRINT2 allows two different strings, in addition to a formatted string buffer variable in RAM, total length is 16, I think keeping the buffer length at 16 would be okay.  We will see how to get that into code for you to test.
 
Ross,
 
We plan to add this command
SPRINTF    PRINTBUF  "%xF"   x
where
PRINTBUF can be PRINTBUF1 or PRINTBUF2 are 16 bytes string buffer only for WC32.
"%xF"   where x can be 1,2,3, and 4 to indicate how many digits formatted floating number will be 0.1, 0.001. 0.0001, or 0.00001
x can be any temp sensor ID, VAR or RAM ID.
 
Say you have temp sensor reading T1  == 331, SPRINTF PRINTBUF1 "%1F"   T1, it will format temperature into string 33.1
 
Do you think that would be okay for you?
 
CAI_Support said:
We plan to add this command

SPRINTF    PRINTBUF  "%xF"   x
where
PRINTBUF can be PRINTBUF1 or PRINTBUF2 are 16 bytes string buffer only for WC32.
"%xF"   where x can be 1,2,3, and 4 to indicate how many digits formatted floating number will be 0.1, 0.001. 0.0001, or 0.00001
 
Are you missing one there? You go from 0.1 to 0.001 and miss 0.01 ?? (I think that would be a bad move)
 
 
CAI_Support said:
x can be any temp sensor ID, VAR or RAM ID.
Say you have temp sensor reading T1  == 331, SPRINTF PRINTBUF1 "%1F"   T1, it will format temperature into string 33.1
Do you think that would be okay for you?
 
That looks like a great start.
Will the sprintf command concatenate/append to the buffer, so with repeated commands we can build up a string?
Can we print a literal?
Eg, if I wanted to print, say "T1=56.3 set=60.0", where the variables are the "1" in T1, the value 56.3 and the value 60.0
(because the next pass through the loop, I'll want to display T2, its value and setpoint, and T3, etc)
 
Ross,
 
It does support 0.1, 0.01, 0.001, and 0.0001 up to four digits of floating number formatting, once it is released to you.
We do not plan to make the full C function sprintf in the PLC, rather only make it easy for people who does not know much about programming.
To concatenate strings, we would rather add another command, so that users can easily grasp how to use those commands.
 
To make these commands work, we will need to have RAM based string storage, since like you pointed out earlier, EEPROM based storage will not last for frequent write operations. We plan to add two print buffers in WC32 firmware, each 16 bytes long. Would you think that is enough for the needs?
 
STRCAT will be added with up to 3 parameters.  The first parameter will be the RAM based string buffer PRINTBUF1 or PRINTBUF2
the 2nd and optional 3rd parameter will be appended to the first parameter referenced buffer.
 
Please note SPRINTF will wipe out the buffer, STRCAT will not wipe out the buffer.
 
CAI_Support said:
STRCAT will be added with up to 3 parameters.  The first parameter will be the RAM based string buffer PRINTBUF1 or PRINTBUF2
the 2nd and optional 3rd parameter will be appended to the first parameter referenced buffer.
 
Please note SPRINTF will wipe out the buffer, STRCAT will not wipe out the buffer.
 
Great. Now, one more question then... how do I clear a buffer ready to start?
I don't want to SPRINTF to it to clear it, if that is going to start a string with a formatted number.
 
Eg, how exactly will I set say buffer1 to
    "Setpoint = 56.3 degrees"
 
   ** THING TO CLEAR PRINTBUF1
   strcat printbuf1 "Setpoint = "     <--- is this going to be legal/valid?
   sprintf printbuf1 "%1F" ram1     <--- won't this just clear printbuf1, so I lose the "Setpoint = " that was there?
   strcat printbuf1 " degrees"
   print1 printbuf1                          <-- actually send string to display line 1
 
Edit:
 
   Or, will sprintf also take some limited text, like:
    sprintf printbuf1 "Setpoint = %1F degrees" ram1
 
(How then, would I use stringcat to concatenate something else to make a line say:
  "T1=xx.x    set=yy.y"  with two formatted numbers in it?
 
CAI_Support said:
You can
sprintf printbuf1 "%1F" T1
print1 "Setpoint ="  printbuf1 "degrees"
 
OK, that gets me a single formatted variable embedded in some text, but it doesn't give me the flexibility to have more than one, like the example I cited:
 
  "V1: set x.x, now y.y" - where the "1", "x.x" and "y.y" are all variable?? We're ahead... but only just.
 
Ross,
 
The PLC command only takes up to three parameters, so the line you wanted to format must be in three string buffers.  I think we can make that '%' not from first position, but still each time only can format one variable.
 
Hi Ross,
 
We sent you another testing firmware, please try it out and provide us with your feedback directly.  Thanks.
 
We just sent another version WC32 firmware to Ross to test out.  This version support both SPRINTF and STRCAT, only limited to "%xF" where x is 0 to 4.
We added some bound checking for string handling, so that longer than 16 bytes will be truncated at the end.
If you are interested in testing this firmware, please send us a private message.
 
Back
Top