Tutorial
PL/i Programming Language for i.CanDoIt
This set of pages will provide brief tutorials about programming in PL/i. This language is more simple than C, but more structured than Basic. The objective in creating this language was to compile compact code that would run efficiently and safely as a virtual machine, like a very lean Java.
Index of PL/i Tutorials
Tutorial #10

Direct LonWorks Network Variable Access (for Advanced Users)

LonWorks Local Bound Network Variables

The local LonWorks processor holds 36 network variable outputs or NVO's (defined as sensor function blocks) and 20 network variable inputs or NVI's (defined as actuator function blocks). These may be bound to other nodes by any external network management tool that supports standard LonWorks objects. The network types may be dynamically changed to any standard variable type (SNVT). The variables may be read or written from the PL/i program using the following statements:

nv_read ( <index> , <var> );
nv_write ( <index> , <var> );

<var> = nv_isnew ( <index> );
nv_info ( <index> , <var> );

The <index> in each of the above statements is the network variable index, which must be in the range of 5 to 61 (for AddMe III, or 3 to 59 for Babel Buster X2). Any valid expression may be used as the index (constant, variable, etc.). The network variable will be read into the variable named by <var> in nv_read, and written from <var> in nv_write. The nv_isnew function will return TRUE if the indexed network variable input (NVI) has received an update since the last call, or false otherwise. The NVO's will always return false (they do not "receive" data).

The variable types read or written should match the expected data type on the network. A large number of SNVT's are 2-byte integer values, and a large number are 4-byte floating point. Several SNVT's are also structures. Use a PL/i record defined in the same format as the SNVT to read or write structured network variables. The following simple example shows the definition of SNVT_switch and a program that turns the switch "on" at 100% (value is scaled x2 in SNVT_switch). In this case, we have made the assumption that the external network management tool has changed the type of the NV at index 5 to SNVT_switch.

program nvtest
declare
type SNVT_switch = record
value: uint8;
state: int8;
end;
netvar: SNVT_switch;

begin
netvar.state = 1;
netvar.value = 200;
nv_write (5, netvar);
end

It may be desirable to find out from within your PL/i program what type (SNVT) the network variable has been configured to. You cannot reconfigure network variable types from a PL/i program. Due to the fact that the Neuron Chip is not allowed to self-modify its code, network variable type changes can only be made from outside the device via the network using a network management tool. However, once reconfigured, you can retrieve the configuration information to see what type you have been given.

You must define a record which is an abbreviated form of SNVT_nv_type. It is defined in PL/i as shown in the following example. The call to nv_info then retrieves this information record for NV index 5.

program nvinfo
declare
type nvInfo = record
nvIndex: uint8;
nvSnvtIndex: uint16;
nvSnvtTypeCat: uint8;
nvSnvtTypeSize: uint8;
nvSnvtScaleA: int16;
nvSnvtScaleB: int16;
nvSnvtScaleC: int16;
end;
infovar: nvInfo;

begin
nv_info (5, infovar);
end

The nv_info function is only applicable to the local bound network variables.

LonWorks Remote Polled Network Variables     

Variables in remote devices may be polled without binding. Up to 50 devices may be identified in the device table of the server. Up to 62 network variables per device may be polled. The functions for accessing remote network variables are:

nv_update ( <node> , <index> , <var> );
nv_fetch ( <node> , <index> , <var> );

The nv_update function reads data from the given variable and sends it to the remote device. The nv_fetch function retrieves data from a remote device and writes it into the given variable. The <node> refers to the device number in the device table, 1 through 50. The <index> is the network variable index 0..61. The <var> is the variable that data will be read from or written into.

The variable type must match the network variable being accessed in the remote device. Data type declarations would be the same as noted above with the SNVT_switch example.

Use the LON Devices page to set up the device table. This table is included in the XML configuration file stored in the server. Once set up and saved, it will be restored each time the server is restarted.

Network variable mapping and conversion is automatic for scalar data types (int, float, etc.). However, structures require programmed interpretation to convert the single structured network variable to a series of registers. The following is an example of fetching an HVAC status structured variable. This program will run as is on the server; however, the hton/ntoh functions must be added if run on the I/O coprocessor.

program nvtest

declare
type SNVT_hvac_status = record
mode: uint8;
heat_out_pri: int16;
heat_out_sec: int16;
cool_out: int16;
econ_out: int16;
fan_out: int16;
in_alarm: uint8;
end;

thisUnit: SNVT_hvac_status;
tempValue: int;

begin
nv_fetch (2, 39, thisUnit);
tempValue = int(thisUnit.mode);
seti (101, tempValue);
tempValue = int(thisUnit.heat_out_pri);
seti (102, tempValue);
tempValue = int(thisUnit.heat_out_sec);
seti (103, tempValue);
tempValue = int(thisUnit.cool_out);
seti (104, tempValue);
tempValue = int(thisUnit.econ_out);
seti (105, tempValue);
tempValue = int(thisUnit.fan_out);
seti (106, tempValue);
tempValue = int(thisUnit.in_alarm);
seti (107, tempValue);
end