Binary Text Functions

The Binary Text functions format binary data packets for use with the TCPDevice function. These functions can be used to send Modbus TCP packets to a Modbus TCP server device. When using these functions to send Modbus packets, the TCPDevice's function Packet Type parameter must be set to Modbus.

Parameter

Description

BGetDouble(Binary, Offset, [Byte/Word Order])

Returns a double-precision floating-point value in a Binary structure as a single-precision result.

Binary

Reference to a Binary, Device, or a Query structure, returned from a BStringf, TCPDevice or QueryDevice function (respectively).

Offset

Specifies the Offset, in bytes.

Byte/Word Order

Specifies the byte order for words and bytes read by the function.

0 = Big Endian (default)

1 = Little Endian

2 = Big Endian with 16-bit word swapping

3 = Little Endian with 16-bit word swapping

BGetFloat(Binary, Offset,  [Byte/Word Order])

Returns a floating-point value in a Binary structure.

Binary

Reference to a Binary, Device, or a Query structure, returned from a BStringf, TCPDevice or QueryDevice function (respectively).

Offset

Specifies the Offset, in bytes.

Byte/Word Order

Specifies the byte order for words and bytes read by the function.

0 = Big Endian (default)

1 = Little Endian

2 = Big Endian with 16-bit word swapping

3 = Little Endian with 16-bit word swapping

BGetInt(Binary, Offset, Bytes, [Sign], [Byte/Word Order])

Returns an integer value in a Binary structure.

Binary

Reference to a Binary, Device, or a Query structure, returned from a BStringf, TCPDevice or QueryDevice function (respectively).

Offset

Specifies the Offset, in bytes.

Bytes

1, 2, or 4.

Sign

0 = signed (default)

1 = unsigned

Byte/Word Order

Specifies the byte order for words and bytes read by the function.

0 = Big Endian (default)

1 = Little Endian

2 = Big Endian with 16-bit word swapping

3 = Little Endian with 16-bit word swapping

BGetString(Binary, Offset, Bytes, [Byte Swap])

Returns a string in a Binary structure.

Binary

Reference to a Binary, Device, or a Query structure, returned from a BStringf, TCPDevice or QueryDevice function (respectively).

Offset

Specifies the Offset, in bytes.

Bytes

Length to be extracted.

Byte Swap

Specifies the byte order written by the function. Due to the Modbus implementation, it may be necessary to swap adjacent bytes of a string when reading or writing to a PLC over Modbus.

Note: The byte swapping used in this function is not related to Big Endian or Little Endian byte swapping. For example, when enabled, if the input string from the Binary parameter was "BADC\0" ("0" is the string terminating NULL value), the result output would be "ABCD\0".

0 = do not swap string bytes (default)

1 = swap string bytes

BLen(Binary)

 

Returns the length of a Binary structure. Binary must reference either a Device structure or a Query structure, returned from a TCPDevice or QueryDevice function (respectively).

BStringf(Byte/Word Order, Format-String, Value1, [Value2, ...])

Returns a Binary structure that contains a binary string constructed using a specified format.

 

Byte/Word Order

Specifies the byte order for words and bytes written by the function.

Note: The Byte/Word Order parameter only applies to numeric types, and does not affect the encoding of binary structures or string types.

0 = Big Endian (default)

1 = Little Endian

2 = Big Endian with 16-bit word swapping

3 = Little Endian with 16-bit word swapping

Format-String

 

Available Format-string parameters:

Parameter Description

%c

1 byte

%h

2 byte

%d

4 byte

%f

4 byte float

%e 8 byte float

%b

Binary structure

%s String
%~s String is to be byte-swapped
Note:
  • The format-string parameters are case-sensitive.
  • The format-string must be enclosed in quotes (").

Value

Specifies a variable length list of values to encode as Binary (Value1, Value2, ...); each Value will be converted to the type specified by the Format-String parameter.

Binary Text Functions Example

This example uses two In-Sight systems named SYSTEM0 and SYSTEM1. SYSTEM0 is the Modbus Client, and SYSTEM1 is the Modbus Server.

SYSTEM0 contains the following functions in its spreadsheet:

A2=TCPDevice("SYSTEM1",502,0,4,1000)

B2=QueryDevice($A$0,A2,0,0,0,0,0,6,0,3,20,4,0,1)

A3=BLen(B2)

A4=BGetInt(B2,9,2)

A5=BGetString(B2,10,1)

SYSTEM1 contains the following value:

F4=65

When the spreadsheet on SYSTEM0 executes:

  • The TCPDevice function in cell A2 establishes a TCP/IP Modbus connection with SYSTEM1.
  • The QueryDevice function in cell B2 sends a Modbus Read Multiple Registers command to SYSTEM1, specifying that the data contained in F4 is to be imported by SYSTEM0 (in binary format).

The results returned in the spreadsheet of SYSTEM0 are as follows:

  • A3=11, which is the length of the binary data packet imported from the Modbus Server, SYSTEM1.
  • A4=65, which is the integer value contained in cell F4 on SYSTEM1.
  • A5="A", which is the integer value in F4 interpreted as a string.
Note: In this example, a TCPDevice function is not used on SYSTEM1, the Modbus Server, because In-Sight automatically responds to any Modbus commands that it receives.

The actual data packets sent/received by the SYSTEM0 in the example above are:

Data

Command Packet

00

 

00

 

00

 

00

 

00

 

06

Length (6 bytes)

00

 

03

Function (Read Multiple Registers)

20

04

Ref# (cell-address = F4)

00

01

Word count (0001)

Data

Response Packet

00

 

00

 

00

 

00

 

00

 

05

Length (5 bytes)

00

 

03

Function (Read Multiple Registers)

02

Byte count (2 bytes)

00

65

Result value (00 65 in decimal; Modbus uses Big endian)