MODBUS Functions

<< Click to Display Table of Contents >>

Navigation:  Modbus Universal MasterOPC Server > Lua 5.1 Reference Manual > Function library > The MODBUS Library >

MODBUS Functions

MODBUS Functions

mbul_mail Analog Reading

mbul_mail Analog Writing

mbul_mail Discrete Reading

mbul_mail Discrete Writing

mbul_mail Reading Strings

mbul_mail Writing Strings

mbul_mail modbus.ExtFunction

mbul_mail modbus.ReadFileFunction

Analog Reading

The following functions belong to that group:

mbul   modbus.ReadHoldingRegistersAsInt16

mbul   modbus.ReadHoldingRegistersAsUInt16

mbul   modbus.ReadHoldingRegistersAsInt32

mbul   modbus.ReadHoldingRegistersAsUInt32

mbul   modbus.ReadHoldingRegistersAsFloat

mbul   modbus.ReadHoldingRegistersAsDouble

         

mbul   modbus.ReadInputRegistersAsInt16

mbul   modbus.ReadInputRegistersAsUInt16

mbul   modbus.ReadInputRegistersAsInt32

mbul   modbus.ReadInputRegistersAsUInt32

mbul   modbus.ReadInputRegistersAsFloat

mbul   modbus.ReadInputRegistersAsDouble

Parameters of the functions are: Address, Count, ChangeBytes and StringChangeBytes:

mbul   Address – register address

mbul   Count – number of elements of the given type

mbul   ChangeBytes – enable (true) or disable (false) use of byte swapping specified by the parameter StringChangeBytes

mbul   StringChangeBytes – mandatory 8 non-recurrent bytes (0-7)

Example

local w={}

local err

err,w = modbus.ReadHoldingRegistersAsInt16(1000,6,true,"10325476");

--err: true – error, false – no error

Analog Writing

The following functions belong to that group:

mbul   modbus.WriteHoldingRegistersAsInt16

mbul   modbus.WtiteHoldingRegistersAsUInt16

mbul   modbus.WriteHoldingRegistersAsInt32

mbul   modbus.WriteHoldingRegistersAsUInt32

mbul   modbus.WriteHoldingRegistersAsFloat

mbul   modbus.WriteHoldingRegistersAsDouble

Parameters of the functions are: Address, Count, ChangeBytes, StringChangeBytes, Cmd6 and Buf:

mbul   Address – register address

mbul   Count – number of elements of the given type

mbul   ChangeBytes – enable (true) or disable (false) use of byte swapping specified by the parameter StringChangeBytes

mbul   StringChangeBytes – mandatory 8 non-recurrent bytes (0-7)

mbul   Cmd6 – use (true) or do not use (false) the command 6

mbul   Buf – array of values

Example

--write 2 int16-type values from address 1 with

--byte swapping, not using the command 6

local w={}

local err

w[1]=5; w[2]=7;

err = modbus.WriteHoldingRegistersAsInt16(1,2,true,"10325476",false,w);

--err: true – error, false – no error

Discrete Reading

The following functions belong to that group:

mbul   modbus.ReadDiscreteInputs

mbul   modbus.ReadCoils

Parameters of the functions are: Address and Count:

mbul   Address – register address

mbul   Count – number of elements of the given type

Example

local w={}

local err

err,w = modbus.ReadDiscreteInputs(0,6);

--err: true – error, false – no error

Discrete Writing

The following functions belong to that group:

mbul   modbus.WriteCoils

Parameters of the functions are: Address, Count and Buf:

mbul   Address – register address

mbul   Count – number of elements of the given type

mbul   Buf – array of values

Example

local w={}

local err

w[1]=true;w[2]=false;

--write 2 values from address 1

err = modbus.WriteCoils(1,2,w);

--err: true – error, false – no error

Reading Strings

The following functions belong to that group:

mbul   modbus.ReadHoldingRegistersAsString

mbul   modbus.ReadInputRegistersAsString

Parameters of the functions are: Address, Count and TypeString:

mbul   Address – register address

mbul   Count – number of bytes read out

mbul   TypeString – 0 – ASCII, 1 – HEX

mbul   StringChangeBytes – 2 bytes (0 or 1) defining the interleaving of bytes in each register of the received string.

Example

local err,s

err,s = modbus.ReadHoldingRegistersAsString(2000,30,0,"01");

--err: true – error, false – no error

Writing Strings

The following functions belong to that group:

mbul   modbus.WriteHoldingRegistersAsString

Parameters of the functions are: Address, Count, TypeString and String:

mbul   Address – register address

mbul   Count – number of written bytes from the string String

mbul   TypeString – 0 – ASCII, 1 – HEX;

mbul   String – string

mbul   StringChangeBytes – 2 bytes (0 or 1) defining the interleaving of bytes in each register of the received string.

Example

local err

err = modbus.WriteHoldingRegistersAsString(2000,10,0,"String133","01");

--err: true – error, false – no error

modbus.ExtFunction

Sends a formatted sequence of bytes to a MODBUS device. The function can be invoked from a script of a node (of the TCP/IP or COM type).

A device address is obtained from the current device.

Input arguments are:

mbul   1 – function code (65-72)

mbul   2 – number of elements in the initial table

mbul   3 – a table of format masks

mbul   4 – the initial table

mbul   5 – a table of masks of received data or nil

A format of the mask of output conversions is String1:String2, where String1 is a data format and String2 is a swapping string. Data formats are string, byte, int16, uint16, int32, uint32, float and double. The formats string and byte do not require a swapping string. For other formats, "10325476" is used if a swapping string is not specified.

A format of the mask of input conversions is String1:String2:String3, where String1 is a data format, String2 is a number of received numbers, and String3 is a string of byte swapping (or, for formats with the prefix s, number of bytes to receive a number of the specified format). Data formats are string, byte, int16, uint16, int32, uint32, float, double, sint32 and sdouble (the formats sint32 and sdouble require a string of byte number instead of a string of byte swapping).

If conversion masks are not defined, bytes are transferred from tables and received to tables without conversions.

If the mask of input conversions is used up, but still there are bytes in the reception buffer, then remained bytes are converted into single character string of the reception table.

Returned values are:

mbul   1 – error code (0 – no errors)

mbul   2 – a table; each element of that table is equal to a number/string according to the mask of conversions, or to a byte from the reception buffer (if a mask of conversions is not defined)

mbul   3 – number of elements in the table

Error codes are:

mbul   -1 – timeout error

mbul   01 – function code received cannot be processed in the SLAVE

mbul   02 – data address specified in the request is unavailable for given SLAVE

mbul   03 – value in the request data field is inadmissible for given SLAVE

mbul   04 – nonrestored error is detected while the SLAVE has attempted to execute an action demanded

mbul   05 – the SLAVE is processing a received request, but this takes a lot of time. Such a reply prevents the MASTER of timeout error generation.

mbul   06 – the SLAVE is busy (is processing a command). The MASTER must repeat a message later when the SLAVE will be available.

mbul   07 – the SLAVE cannot execute a program function received in a request. This code is returned in case of unsuccessful program request that uses the function number 13 or 14. The MASTER must request for diagnostic information or information about SLAVE errors.

mbul   08 – the SLAVE attempts to read extended memory, but detects a parity error. The MASTER can repeat the request but repair is usually required in such cases.

Extra error code for COM:

mbul   12 – not enough bytes are received during the given timeout

mbul   11 – checksum error

Example 1

The function sends the following sequence of bytes:

01 41 00 01 00 02 00 00 31 32 33 34 04 B9 FA

local srcdata = {1,2,"1234",4}

local srcmask = {"int16:10","int32:1032","string","byte"}

local srclen = 4

local dstdata = {}

err,dstdata,dstlen = modbus.ExtFunction(65,srclen,srcmask,srcdata,nil,nil);

Example 2

The function receives the following 2 float-type numbers in the character format:

+05.000+04.235

local srcdata = {1,2,4}

local srcmask = {"int16:10","int32:1032","byte"}

local srclen  = 3

local dstdata = {}

local dstmask = {"sdouble:2:7"}

err,dstdata,dstlen = modbus.ExtFunction(65,srclen,srcmask,srcdata,dstmask);

--send 01 41 00 01 00 02 00 00 04 C3 36

if err == 0 then

if dstlen == 2 then

--dstdata[1] will equal 5

--dstdata[2] will equal 4.235

end

end

                       

modbus.ReadFileFunction

This functuion is destined to read out records from a device by the function 0x14. The function reads out a record from a file, and returns several tables formatted by preset masks. Thus, a recored obtained can be divided to several tables of different structure.

The input arguments are:

mbul   1 – number of tables obtained from a record

mbul   2 – table containing numbers of read files. This table must contain that number of files that is set for tables obtained.

mbul   3 – table containing numbers of read records. This table must contain that number of record numbers that is set for tables obtained.

mbul   4 – array of numbers of received elements for each table received. This table must contain that number of elements that is set for tables obtained.

mbul   5 – table of mask tables (if more than 1 table is received). This table must contain that number of mask tables that is set for tables obtained.

Returned values are:

mbul   1 – error code. 0 if a reply is correct.

0 - no error;

-1 - not connected, not response from controller;

1 - 8 - exception code returned by controller (3 byte of response).

10 - empty answer (no bytes in data field).

mbul   2..N – received value tables formatted by the preset masks. Number of received tables is set by a user.

In the next example, two records are read out (1 and 10 from the file 1).

             

local err,dstlen --variable declarations

local file = {1,1} --declaration and initialization of file table

local rec  = {1,10} --declaration and initialization of record table

local len  = {5,5} --declaration and initialization of table of numbers of received elements for each table received

local dstdata = {} --declaration of table for reception of data from record 1

local dstdata2= {} –-declaration of table for reception of data from record 10

local dstmsk1  = {"int16:5:10"} --mask of the first table (for record 1). Data type of a value is Int16, number of elements is 5, alternation is "upper byte first"

local dstmsk2  = {"int16:5:10"} -- mask of the second table (for record 10). Data type of a value is Int16, number of elements is 5, alternation is "upper byte first"

local dstmsk   =  {dstmsk1,dstmsk2} --creation of a table of mask tables

--reading an archive from the device

err,dstdata,dstdata2 = modbus.ReadFileFunction(2,file,rec,len,dstmsk);

--if there are no errors

if err == 0 then

--then messages with values received are written into a log

   server.Message("d1 ",dstdata[1],",",dstdata[2],",",dstdata[3],",",dstdata[4]);

   server.Message("d2 ",dstdata2[1],",",dstdata2[2],",",dstdata2[3],",",dstdata2[4]);

else

    server.Message("error");

end