APRUS Lua-PPI 配置说明

概述

      本章主要为APRUS Lua-PPI协议的相关配置说明,该协议主要针对支持(西门子)PPI协议的设备。APRUS适配器可通过PPI协议与对接设备进行通信。而其中APRUS的Lua包含apru.luaconfig.lua两个文件,客户只需配置config.lua就可以对支持PPI协议的设备进行数据采集。

      目前版本支持V寄存器的按位或者字节为单位进行数据采集;对于位数据通过PPI协议采集时仅支持一位一位进行采集,不支持同时采集多个位数据。

      如需修改APRUS.lua文件的内容时,请咨询相关的技术人员,随意修改会导致适配器不能正常工作,所以此文档主要介绍config.lua内容。

一、 APRUS.lua配置说明

1.API接口

1.1 导入ppi协议支持库

语句 说明
require "ppi" 导入ppi协议支持库

1.2 加载config.lua配置文件

语句 说明
config = require "config" 导入config.lua中的配置信息 (下一节介绍config.lua)

1.3 创建ppi对象

语句 说明
ppiobj = ppi.new("ppi") 创建ppi对象,返回ppiobj供全局使用

1.4 配置ppi对象接口参数

语句 说明
ppi.config(ppiobj, cjson.encode(config.PPI.Device)) 配置ppi通信接口RS485参数

1.5 添加ppi采集节点

语句 说明
ppi_load_collectnodes(ppiobj,config.PPI.ColNode) 添加config.lua中所配置的采集节点

1.6 添加ppi上报变量节点

语句 说明
ppi_load_varnodes(ppiobj,config.PPI.VarNode) 添加config.lua中所配置的变量节点

1.7 运行ppi实例

语句 说明
ppi.run(ppiobj) 在mqtt建立连接后运行ppi实例,即开始采集+运算

1.8 暂停ppi实例

语句 说明
ppi.stop(ppiobj) 在mqtt连接断开时暂停ppi实例

1.9 设置ppi实例的调试打印级别

语句 说明
ppi.debug(ppiObj, debugNumber) 启用详细的ppi调试信息。
ppiObj为ppi实例,
debugNumber是32位十六进制数,
按字节定义:错误信息、连接&一般信息、接收详情、发送详情。
按需开启调试级别以监控设备状态、排查问题和优化性能。

1.10 等待ppi事件

语句 说明
local msg = user.waitmsg() 启动事件等待,该接口会返回全局各种事件,包括ppi对象事件

1.11 msg 事件处理

语句 elseif msg.from == "msg " then
      msg_handle(msg.obj, msg.name, msg.code, msg.data)
说明 当接收到msg对象事件时,调用处理函数ppi_handle
msg.obj即new时所返回的对象;
msg.name即new时所配置的对象名称;
msg.code事件码区分改变上报事件周期上报事件等等;
msg.data事件数据

1.12 mqtt数据上报

语句 说明
function ppi_handle(obj, name, code, data) 若无需特殊处理,则直接将事件数据通过mqtt发送;
如需要二次处理可将data展开做分析

1.13 反向控制(暂不支持)

语句 说明
ppi.write(ppiobj, cjson.encode({["name"] = k, ["val"] = v})) 反向控制时调用的ppi写入接口

2 aprus.lua示例

package.cpath = "./?.so"
package.path = "./?.lua"
cjson = require "cjson"
config = require "config"
require "ppi"

function ppiServer_load_station(svr, cfg)
    for k, v in pairs(cfg) do
        print("ppiServer load Station" .. cjson.encode(v))
        ppiServer.setStation(svr, cjson.encode(v))
    end
end

function act_control(m, json)
    for k, v in pairs(json) do
        if k ~= "Act" then
            AXio.write(axioObj, k, v)
        end
    end
end

function mqttdata_handle(m, topic, data)
    local json = cjson.decode(data)

    if json.Act == "Control" then
        act_control(m, json)
    end
end

function mqttsys_handle(m, code)
    if code == 0 then
        ppi.stop(ppiObj)
    elseif code == 1 then
        --ppi.run(ppiObj)
    end
end

function ppi_handle(obj, name, code, data)
    mqtt.publish(mqtt3Obj, name, "r", data)
end

function ppi_load_collectnodes(obj, nodes)
    for k, v in pairs(nodes) do
        ppi.addcnode(obj, cjson.encode(v))
    end
end

function ppi_load_varnodes(obj, nodes)
    for k, v in pairs(nodes) do
        ppi.addvnode(obj, cjson.encode(v))
    end
end

function init()
    mqtt3Obj = mqtt.new()
    mqtt.config(mqtt3Obj, "", "gards.mixyun.com", "31883", "aprusdev@aprus", "mixlinker123") --V8
    user.setluaver(config.AprusX.luaver)
    user.setdevinfo(config.AprusX.devinfo)
    user.ipconfig(config.AprusX.ipmode, config.AprusX.inet_addr, config.AprusX.netmask)
end

function start()
    print("<--------------------user lua start------------------------>")
    init()
    print("<--------------------ppi_init()------------------------>")
    ppiObj = ppi.new("ppi")
    ppi.config(ppiObj, cjson.encode(config.PPI.Device))
    ppi_load_collectnodes(ppiObj, config.PPI.ColNode)
    ppi_load_varnodes(ppiObj, config.PPI.VarNode)
    --ppi.debug(ppiObj, 0x01010101)
    print("<--------------------ppi.run------------------------>")
    ppi.run(ppiObj)
    mqtt.run(mqtt3Obj)

    counter = 0
    while true do
        local msg = user.waitmsg()
        if msg.from == "mqtt-sys" then
            mqttsys_handle(msg.session, msg.code)

        elseif msg.from == "mqtt-msg" then
            mqttdata_handle(msg.session, msg.topic, msg.payload)

        elseif msg.from == "ppi" then
            ppi_handle(msg.obj, msg.name, msg.code, msg.data)
        end
    end

    print("<--------------------user lua over------------------------>")
end

start()

二、 config.lua配置说明

1.API接口

1.1 APRUS: 接口属性

参数 说明
ipmode “auto”/“manual”/“none” IP获取方式
inet_addr “192.168.1.234” APRUS的IP地址
netmask “255.255.255.0” 子网掩码
luaver “MAX.LUA.V032700.R” APRUS的Lua版本信息
devinfo “ppiDev” 与APRUS对接设备

1.2 PPI-Device: 接口属性

参数 说明
portindex 整型:1/2 RS485-1/ 2接口
rate 整型:2400/4800/…/15200 波特率
databit 整型:5/6/7/8 数据位
stopbit 整型:1/2 停止位
parity 字符串:Even/Odd/None 校验

1.3 PPI-ColNode :采集节点属性

参数 说明
ID 整型:1~n 设备ID
reg 字符串:V 寄存器类型
addr 整型:0~n 采集起始地址
cnt 整型:1~n 采集单位数
ctype 字符串:byte(默认)/bit bit:以位为单位采集
Byte:以字节为单位采集
bitn 整型:0~7 位偏移量
在addr的基础上做偏移
即采集起始地址=addr*8+bitn
dbname 字符串: 数据中心的数据库名字

1.4 PPI-VarNode:上报节点属性

参数 说明
ID 整型:1~n 设备ID
reg 字符串:V 寄存器类型
addr 整型:0~n 数据起始地址
ctype 字符串:byte(默认)/bit 用于关连对应的采集节点
dtype 字符串: 变量类型
bit/byte/ubyte/short/ushort 若上报类型是short则需要采集类型为byte,cnt=2
bitn 整型:0~n 位偏移量
当数据类型为bit时有效
cycle 整型:0~n 单位:秒
等于0:即指定上报类型为改变上报
大于0:即指定上报周期
name 字符串 上报名称
offset + - * / ~ 数字 例如:offset=”*10; +5.123; ~1”
表示乘以10 再加5.123 结果保留1位小数

2 config.lua示例

return
{
    AprusX = {
        ipmode = "none", --auto/manual/none
        inet_addr = "192.168.1.234",
        netmask = "255.255.255.0",
        luaver = "V00.R",
        devinfo = "ModbusRtuDev",
    },
    Forward485 = {
        device = {
            rate = 9600,
            databit = 8,
            stopbit = 1,
            parity = "Even", -- None/Odd/Even
            timeout = 3000,
        },
    },
    PPI = {
        Device = {
            portindex = 1,
            rate = 9600,
            databit = 8,
            stopbit = 1,
            parity = "Even", -- None/Odd/Even
            queryInterval = 100
        },
        ColNode = {
            --采集只有bit和byte采集两种模式,默认是byte采集

            --bit  ctype = "bit" 采集的时候只能单采,cnt只能==1   bitn默认是0
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 0, dbName = "PPI_2V100L0" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 1, dbName = "PPI_2V100L1" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 2, dbName = "PPI_2V100L2" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 3, dbName = "PPI_2V100L3" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 4, dbName = "PPI_2V100L4" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 5, dbName = "PPI_2V100L5" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 6, dbName = "PPI_2V100L6" },
            { ID = 2, reg = "V",  addr = 100, cnt = 1, ctype = "bit", bitn = 7, dbName = "PPI_2V100L7" },

            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 0, dbName = "PPI_2V101L0" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 1, dbName = "PPI_2V101L1" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 2, dbName = "PPI_2V101L2" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 3, dbName = "PPI_2V101L3" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 4, dbName = "PPI_2V101L4" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 5, dbName = "PPI_2V101L5" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 6, dbName = "PPI_2V101L6" },
            { ID = 2, reg = "V",  addr = 101, cnt = 1, ctype = "bit", bitn = 7, dbName = "PPI_2V101L7" },


            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 0, dbName = "PPI_2V506L0" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 1, dbName = "PPI_2V506L1" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 2, dbName = "PPI_2V506L2" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 3, dbName = "PPI_2V506L3" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 4, dbName = "PPI_2V506L4" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 5, dbName = "PPI_2V506L5" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 6, dbName = "PPI_2V506L6" },
            { ID = 2, reg = "V",  addr = 506, cnt = 1, ctype = "bit", bitn = 7, dbName = "PPI_2V506L7" },
            --bit

            --byte 
            { ID = 2, reg = "V",  addr = 102, cnt = 10, ctype = "byte", dbName = "PPI_2V102L10"},
            { ID = 2, reg = "V",  addr = 507, cnt = 2,  ctype = "byte", dbName = "PPI_2V507L1"},
            --byte

            --otherReg
            --{ ID = 2, reg = "S",  addr = 0, cnt = 30,  ctype = "byte",  dbName = "PPI_2S0L30"},
            --{ ID = 2, reg = "SM",  addr = 0, cnt = 50,  ctype = "byte",  dbName = "PPI_2SM0L50"},
        },
        VarNode = {
            --ctype = "bit", dtype = "bit"
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 0, cycle = 1,   name = "L2_V_100_0" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 1, cycle = 1,   name = "L2_V_100_1" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 2, cycle = 1,   name = "L2_V_100_2" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 3, cycle = 1,   name = "L2_V_100_3" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 4, cycle = 1,   name = "L2_V_100_4" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 5, cycle = 1,   name = "L2_V_100_5" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 6, cycle = 1,   name = "L2_V_100_6" },
            { ID = 2, reg = "V",  addr = 100, ctype = "bit",    dtype = "bit",  bitn = 7, cycle = 1,   name = "L2_V_100_7" },

            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 0, cycle = 1,   name = "L2_V_101_0" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 1, cycle = 1,   name = "L2_V_101_1" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 2, cycle = 1,   name = "L2_V_101_2" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 3, cycle = 1,   name = "L2_V_101_3" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 4, cycle = 1,   name = "L2_V_101_4" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 5, cycle = 1,   name = "L2_V_101_5" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 6, cycle = 1,   name = "L2_V_101_6" },
            { ID = 2, reg = "V",  addr = 101, ctype = "bit",    dtype = "bit",  bitn = 7, cycle = 1,   name = "L2_V_101_7" },


            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 0, cycle = 1,   name = "L2_V_506_0" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 1, cycle = 1,   name = "L2_V_506_1" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 2, cycle = 1,   name = "L2_V_506_2" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 3, cycle = 1,   name = "L2_V_506_3" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 4, cycle = 1,   name = "L2_V_506_4" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 5, cycle = 1,   name = "L2_V_506_5" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 6, cycle = 1,   name = "L2_V_506_6" },
            { ID = 2, reg = "V",  addr = 506, ctype = "bit",    dtype = "bit",  bitn = 7, cycle = 1,   name = "L2_V_506_7" },
            --ctype = "bit"


            --ctype = "byte",  dtype = "byte"
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",  dtype = "byte", cycle = 1, offset="*100; ~1", name = "L2_V_102" },
            { ID = 2, reg = "V",  addr = 103, ctype = "byte",  dtype = "byte", cycle = 1,  name = "L2_V_103" },
            { ID = 2, reg = "V",  addr = 104, ctype = "byte",  dtype = "byte", cycle = 1,  name = "L2_V_104" },
            --ctype = "byte",  dtype = "byte"
            { ID = 2, reg = "V",  addr = 105, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_105" },
            { ID = 2, reg = "V",  addr = 106, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_106" },
            { ID = 2, reg = "V",  addr = 107, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_107" },
            { ID = 2, reg = "V",  addr = 108, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_108" },
            { ID = 2, reg = "V",  addr = 109, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_109" },
            { ID = 2, reg = "V",  addr = 110, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_110" },
            { ID = 2, reg = "V",  addr = 111, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_111" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",  dtype = "ubyte", cycle = 1,  name = "L2_V_507" },
            --ctype = "byte",  dtype = "short"
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_102_S" },
            { ID = 2, reg = "V",  addr = 103, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_103_S" },
            { ID = 2, reg = "V",  addr = 104, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_104_S" },
            { ID = 2, reg = "V",  addr = 105, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_105_S" },
            { ID = 2, reg = "V",  addr = 106, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_106_S" },
            { ID = 2, reg = "V",  addr = 107, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_107_S" },
            { ID = 2, reg = "V",  addr = 108, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_108_S" },
            { ID = 2, reg = "V",  addr = 109, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_109_S" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",  dtype = "short", cycle = 1,  name = "L2_V_507_S" },
            --ctype = "byte"

            --ctype = "byte", dtype = "bit"
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 0, cycle = 1,   name = "L2_V_102_0" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 1, cycle = 1,   name = "L2_V_102_1" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 2, cycle = 1,   name = "L2_V_102_2" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 3, cycle = 1,   name = "L2_V_102_3" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 4, cycle = 1,   name = "L2_V_102_4" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 5, cycle = 1,   name = "L2_V_102_5" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 6, cycle = 1,   name = "L2_V_102_6" },
            { ID = 2, reg = "V",  addr = 102, ctype = "byte",    dtype = "bit",  bitn = 7, cycle = 1,   name = "L2_V_102_7" },

            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 0, cycle = 1,   name = "L2_V_507_0" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 1, cycle = 1,   name = "L2_V_507_1" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 2, cycle = 1,   name = "L2_V_507_2" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 3, cycle = 1,   name = "L2_V_507_3" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 4, cycle = 1,   name = "L2_V_507_4" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 5, cycle = 1,   name = "L2_V_507_5" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 6, cycle = 1,   name = "L2_V_507_6" },
            { ID = 2, reg = "V",  addr = 507, ctype = "byte",    dtype = "bit",  bitn = 7, cycle = 1,   name = "L2_V_507_7" },
            --ctype = "byte"
        }
    },
}
文档更新时间: 2024-11-01 11:44   作者:CGL