<< Click to Display Table of Contents >> Navigation: Modbus Universal MasterOPC Server > Lua 5.1 Reference Manual > Function library > The MODBUS Library > MODBUS Functions |
The following functions belong to that group:
modbus.ReadHoldingRegistersAsInt16
modbus.ReadHoldingRegistersAsUInt16
modbus.ReadHoldingRegistersAsInt32
modbus.ReadHoldingRegistersAsUInt32
modbus.ReadHoldingRegistersAsFloat
modbus.ReadHoldingRegistersAsDouble
modbus.ReadInputRegistersAsInt16
modbus.ReadInputRegistersAsUInt16
modbus.ReadInputRegistersAsInt32
modbus.ReadInputRegistersAsUInt32
modbus.ReadInputRegistersAsFloat
modbus.ReadInputRegistersAsDouble
Parameters of the functions are: Address, Count, ChangeBytes and StringChangeBytes:
Address – register address
Count – number of elements of the given type
ChangeBytes – enable (true) or disable (false) use of byte swapping specified by the parameter StringChangeBytes
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
The following functions belong to that group:
modbus.WriteHoldingRegistersAsInt16
modbus.WtiteHoldingRegistersAsUInt16
modbus.WriteHoldingRegistersAsInt32
modbus.WriteHoldingRegistersAsUInt32
modbus.WriteHoldingRegistersAsFloat
modbus.WriteHoldingRegistersAsDouble
Parameters of the functions are: Address, Count, ChangeBytes, StringChangeBytes, Cmd6 and Buf:
Address – register address
Count – number of elements of the given type
ChangeBytes – enable (true) or disable (false) use of byte swapping specified by the parameter StringChangeBytes
StringChangeBytes – mandatory 8 non-recurrent bytes (0-7)
Cmd6 – use (true) or do not use (false) the command 6
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
The following functions belong to that group:
modbus.ReadDiscreteInputs
modbus.ReadCoils
Parameters of the functions are: Address and Count:
Address – register address
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
The following functions belong to that group:
modbus.WriteCoils
Parameters of the functions are: Address, Count and Buf:
Address – register address
Count – number of elements of the given type
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
The following functions belong to that group:
modbus.ReadHoldingRegistersAsString
modbus.ReadInputRegistersAsString
Parameters of the functions are: Address, Count and TypeString:
Address – register address
Count – number of bytes read out
TypeString – 0 – ASCII, 1 – HEX
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
The following functions belong to that group:
modbus.WriteHoldingRegistersAsString
Parameters of the functions are: Address, Count, TypeString and String:
Address – register address
Count – number of written bytes from the string String
TypeString – 0 – ASCII, 1 – HEX;
String – string
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
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:
1 – function code (65-72)
2 – number of elements in the initial table
3 – a table of format masks
4 – the initial table
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:
1 – error code (0 – no errors)
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)
3 – number of elements in the table
Error codes are:
-1 – timeout error
01 – function code received cannot be processed in the SLAVE
02 – data address specified in the request is unavailable for given SLAVE
03 – value in the request data field is inadmissible for given SLAVE
04 – nonrestored error is detected while the SLAVE has attempted to execute an action demanded
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.
06 – the SLAVE is busy (is processing a command). The MASTER must repeat a message later when the SLAVE will be available.
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.
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:
12 – not enough bytes are received during the given timeout
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
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:
1 – number of tables obtained from a record
2 – table containing numbers of read files. This table must contain that number of files that is set for tables obtained.
3 – table containing numbers of read records. This table must contain that number of record numbers that is set for tables obtained.
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.
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:
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).
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