SWrite

(Engine-Level Function)

Description: Performs a formatted write of text or binary data to a pre-existing stream and returns the number of data items not written.
Returns: Numeric
Usage: Script Only.
Function Groups: Stream and Socket
Related to: BuffWrite | FileSize | Format | FWrite | GetStreamLength | Print | PrintLine | Redirect | Save | SRead | StreamEnd
Format: SWrite(Stream, Format, V1, V2, ...)
Parameters:  
Stream
Required. A stream as returned from BuffStream or FileStream.
Format

Required. Any text expression giving the format of how the values (Vn parameters) are to be written. This format is similar, but not identical, to the C language format string for the printf function, whereby each of the Vn parameters in the statement is assigned to a % format specification in the order in which each appears in the list.

Note that like a standard text string, these format specifiers must also be enclosed by double quotes.

If a format specification appears for which there are no remaining V parameters, the format specification characters themselves are output to the stream exactly as they appear in the Format.

For the % format specifications, the following form applies (where the [ ] indicates optional elements):

%[-][+][SPACE][#][width][.precision]type

where

% (percent sign) is mandatory;

- (minus sign)  (optional) causes the data to be left justified within the field (for binary types b and ASCII character types c, this option is ignored);

+ (plus sign) (optional) causes positive numbers to be prefaced with a + sign (negative numbers are unaffected). This allows easy alignment of positive and negative numbers in a printed column of numbers. For binary types b and non-numerical types, this option is ignored;

space represents the single space character, and is similar to the [ + ] option but places a single space rather than a plus sign in front of positive numbers (negative numbers are still unaffected). This allows alignment of a column of numbers without having to show all signs. For binary types b and non-numerical types, this option is ignored;

# (hash mark) When used with the o , x , or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.

width is a number that specifies the minimum number of characters to output. Numbers that require more characters than specified by the width value are truncated on output. If the number of characters in the number or string is less than width, blanks will be added to the left or right, depending upon whether the output is left or right justified (i.e. whether the [ - ] option has been specified) until the width is reached. For binary types b and ASCII character types c, this option is ignored;

precision has a different meaning for each of the type options as follows:

  • Integer types d, l, u, o, x, and X precision specifies the minimum number of digits to output. If the number contains fewer digits, leading zeroes will be added to the left of the number. If precision is 0, omitted, or if the decimal point appears without a number following it, the precision defaults to 1. The number is not truncated.
  • Floating point types e and E precision specifies the number of digits after the decimal point. The last digit is rounded. The default precision in this case is 6. If the precision is 0 or if the decimal point appears without a number following it, no decimal point appears in the output.
  • Floating point type f precision specifies the number of digits after the decimal point. The last digit is rounded. The default precision is 0. If the precision is explicitly 0, no decimal point is output. If a decimal point is output, at least one digit will be placed before the decimal point.
  • Floating point types g and G precision specifies the maximum number of significant digits to be output. If no precision is specified, all significant digits are written.
  • String type s precision specifies the maximum number of characters of the string to be output. If the string contains more characters than specified by the precision, the string is truncated and only the first characters are written. If the precision is not specified, all of the string characters are output.
  • Byte type c  The precision option is ignored.
  • Binary type b  The precision option is ignored.

type is mandatory. The type specification must be one of those listed below.

Note: The case of the letter is important. Specifying a character for the type that is not in this list will result in all the characters following the % up to that point to be output exactly as they appear in the Format string.

Type

Meaning

nb

Binary format, where n is a number indicating the type of value (see below).

c

Single character (byte values from 0 to 255)

d

Signed decimal integer

e

Signed exponential; exponent key is "e".

E

Signed exponential; exponent key is "E".

f

Signed floating point.

g

e or f format, whichever is shorter.

G

E or f format, whichever is shorter.

h

Handle to a window.

i

Signed decimal integer.

o

Unsigned octal integer.

p

Pointer to a buffer.

s

Text string.

t Writes a utf-8 string as used by OPC UA binary TCP:
a 32-bit signed count of the length (-1 for an invalid string, 0 for an empty string), then the string

u

Unsigned decimal integer.

x

Unsigned hex integer using "abcdef". (See following note.)

X

Unsigned hex integer using "ABCDEF".

When writing negative values using either x or X format codes, the output will use a minimum of 32 bits because that is the smallest possible size to display the full information. Defining a format string such as "%4X" will not reduce this to 16 bit output for negative values.

nb,  Binary type For the format specification of %nb, where n specifies the type of number, n must be a single digit from one of the following choices. All are low-byte-first.

n

Value Type

0

Byte, unsigned

1

Signed short integer (2 bytes)

2

Signed long integer (4 bytes)

3

IEEE single precision float (4 bytes)

4

<obsolete>

5

IEEE double precision float (8 bytes)

6

<obsolete>

7

Unsigned short integer (2 bytes)

8

Unsigned long integer (4 bytes)

9 Writes a time in Microsoft FILETIME format, which contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

 

Note: Other options such as width and precision do not apply to the b type.

c,  Byte type:  This type is not representative of a single character in a string, but rather, represents single bytes. Input values (the Vn parameter to which this format specification applies) must be integers in the range of 0 to 255. Strings are not acceptable input values. Note that the %c format specifier behaves differently when used in an output statement such as BuffWrite than when used in an input statement, such as BuffRead. Some UTF-8 characters can require multiple bytes. Values from 0-127 are consistent between the traditional ASCII encoding and the UTF-8 encoding.

d,  Signed decimal integer:

e,  Signed exponential:  Exponent key is "e"

E,  Signed exponential:  Exponent key is "E"

f,   Signed floating point

g,  e or f formats:  Whichever is shorter

G,  E or F formats:  Whichever is shorter

h,  Window handle type:  This type is used for building structures to be handed to DLLs and should be used by advanced users only.

p,  Buffer pointer type:  This type is also used for building structures to be handed to DLLs and should be used by advanced users only.

s,  Text string type: 

t,  Writes a utf-8 string as used by OPC UA binary TCP:
a 32-bit signed count of the length (-1 for an invalid string, 0 for an empty string), then the string

u,  Unsigned decimal integer,

x,  Unsigned hex integer using "abcdef"

X,  Unsigned hex integer using "ABCDEF"

SWrite(Strm, "%z92", "Something \to\ be escaped");

Gives the output: Something \\to\\ be escaped

Plain text  Text in the Format parameter is written exactly as it appears, with three exceptions:

  • Percentage sign (%)  Because format specifications for the Vn parameters are indicated by a percentage sign, to include an actual percentage sign as part of the Format parameter, precede it with a backslash character (i.e. \%).
  • Backslash character (\)  Because this is used to indicate special control characters such as line feed, carriage return, and form feed, to write a backslash as part of the Format parameter, use two backslash characters (i.e. \\).
  • Quotation marks (")  The entire test string is delimited by quotation marks, so to include a set of quotation marks as part of the Format parameter, use a set of quotations marks (i.e. "").

Control characters  In order to encode certain control characters as part of the Format parameter, one of two methods may be used. The first is to use a backslash character followed by one of the single character codes listed below to produce the desired result (notice that the letters must be lower case):

Code

Meaning

\b

Backspace

\f

Form feed

\n

Line feed

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

 

\nnn  In addition to the above predefined codes, \nnn may be used, where nnn is a three digit integer in the range of 0 to 255 specifying a certain ASCII character. If the number contains less than three digits, the leading spaces must be padded with zeroes; this is not the case with the previously listed single character control characters. For example, to include the one byte ASCII character G in the output, you could place its decimal equivalent of 71 in the Format string as \071.

znnn Escape character where nnn is the 3-digit ASCII code
For example: SWrite(Strm, "%z92", "Something \to\ be escaped");

 Gives the output: Something \\to\\ be escaped

Offset is any numeric expression giving the starting buffer position in bytes for the write, starting at 0.

V1, V2, ...
Required. Any expressions giving the values to be output in the form described by the Format parameter. Each of the Vn parameters is evaluated and written in the order in which each appears in the parameter list. The way in which they are formatted is dictated by the % format specifications. V1 is formatted by the first % sequence in the Format parameter, V2 by the second, and so on. If there are more V parameters than % sequences in the Format string, the remainder are ignored. If there are fewer V parameters than % sequences in the Format string, the remaining % sequences are written literally without any translation.
Comments:

If using this function in any code that may write to a USB-to-Serial device, you must make allowances for deficiencies in those devices as follows: the device will ignore everything following the text in the first %s. All text to be written must be in a single parameter passed to a single %s.

SWrite(Strm, "%s\r", "AT"); 
  { Only the "AT" is received by a USB-to-Serial device }
SWrite(Strm, "%s%s", "AT", \CR); 
  { Only the "AT" is received by a USB-to-Serial device }
SWrite(Strm, "%s", Concat("AT", \CR)); 
  { this will succeed if written to a USB-to-Serial device }

 

You cannot write to a read-only file. You may use GetFileAttribs and SetFileAttribs to get/set the read-only attribute.
If one of the values to be written is outside of the range of the type indicated by the format specifier, a "0" is written.
If the value to be written is invalid, nothing is written for most format specifiers, except for %nb, which will write a "0" in the place of the invalid. Please note that an invalid output does not prevent the execution of the SWrite function.

This function returns the number of Vn parameters not written to the stream. A 0 return value indicates success. Variables that contain invalid values that were not written due to their invalidity do not increment this count. An invalid return value indicates an error with one of the parameters.

As of version 12.0, VTScada expects all strings to be encoded using UTF-8.

Examples:

If MatchKeys(2, "W");
[
  b = MakeBuff(16, 65) { Create buffer; fill it with A's },
  buff = BuffStream(b) { Create the stream },
  SWrite(buff { Write to the stream },
         "A=%d B=%4.2d C=%5.2f\r\n%s\r\n" { Format },
         1, 2, 2/3, "Hello World" { Output values });
]

This statement would write the following two lines of text to the stream buff :

A=1 B= 02 C= 0.67
Hello World

If you replaced the third line of the script with the following:

SWrite(buff { Write to the stream },
       "\072\105\n%c%c%c" { Format },
       77, 111, 109 { Output values });

The following two lines would instead be written to buffer stream buff:

Hi
Mom