Control Solutions is your source for LonWorks I/O.
Tutorial #9

Direct Modbus Access (for Advanced Users)

Modbus RTU Holding Register Read/Write

Holding registers in remote Modbus RTU devices connected via the RS-485 port may be read or written. These procedures are independent of any register mapping defined in the server mapping tables. The syntax for Modbus read/write is:

mod_read ( <uint> , <register> , <var> );
mod_write ( <uint> , <register> , <var> );
mod_swap ( <var> );

The <unit> is also referred to as Slave ID, and must be 1 to 247. Register numbers count up from 1. The variable may be a single variable, and array, or a structure. Modbus registers are defined by that protocol as 16-bit data entities. The number of registers that will be read or written, starting at <register>, will be the variable size divided by 2. To read a single 16-bit register, you must use either int16 or uint16 as the data type. To read a double register, use int or float. To read multiple registers, use an array.

The order of registers in double register pairs, as used to make up a floating point value, is not standardized. Therefore, the mod_swap function will swap the two registers contained in the 4-byte variable named to compensate for differences between the devices.

Host to Network Conversion (and Network to Host Conversion)

Modbus and LonWorks protocols are by definition "big endian" meaning the most significant byte of a multi-byte data element comes first. Some processors are "little endian", sometimes also referred to as "Intel style", including the AddMe III co-processor. Therefore data must be converted from "host" to "network" format before being sent, and converted from "network" to "host" format when received. The following functions are used to convert 2-byte and 4-byte data elements:

htonl ( <var> );
hton ( <var> );
ntohl ( <var> );
ntoh ( <var> );

The "hton" functions convert host to network. The "htonl" converts a "long" or 4-byte value (either integer or floating point), and "hton" converts a 2-byte value (int16 or uint16). The "ntoh" functions convert network to host, 4-byte or 2-byte. The content of the named variable is converted and left stored in the same variable.

Host/network conversion is done automatically by PL/i for single variables. However, any time you use an array or structure as the target of network access (either Modbus or LonWorks), you must manually convert the fields as necessary. Double Modbus registers may require both hton/ntoh and mod_swap.

Remember: Call "hton" before network calls, and call "ntoh" after network calls.

In the following example, the first four assignment statements will work, but the last one will cause an "Unexpected types" error. The target of the assignment may be any type, but the operands of the expression must be integer or floating point.