APRUS Lua-PPIServer配置说明

第一章 概述

1.1 文档说明

序号 描述
1 APRUS适配器可配置为PPI服务器,支持与其他支持PPI协议的客户端设备进行通信。APRUS Lua-PPIServer主要包括两个文件:aprus.lua和config.lua。配置为ppi服务器后,用户可将设备运行的过程数据和结果放置到服务器的数据中,供其他设备读取和写入。需要注意的是ppiServer共享的时候,通过PPI采集的数据是什么类型就以什么类型数据共享。本文将介绍如何将APRUS适配器配置为ppi服务器,并解释各参数的含义。

1.2 参考资料

序号 资料名称
1 《APRUS Lua-数据中心 配置说明》

第二章 配置说明

2.1 aprus.lua配置

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

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(ppiSvrObj)
    elseif code == 1 then
        --ppi.run(ppiSvrObj)
    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()------------------------>")
    ppiSvrObj = ppi.new("ppi")
    ppi.config(ppiSvrObj, cjson.encode(config.PPI.Device))
    ppi_load_collectnodes(ppiSvrObj, config.PPI.ColNode)
    ppi_load_varnodes(ppiSvrObj, config.PPI.VarNode)
    print("<--------------------ppiServer_init()------------------------>")
    ppiSvrObj = ppiServer.new("ppiSvrObj")
    ppiServer.config(ppiSvrObj, cjson.encode(config.PPI.Device))
    ppiServer_load_station(ppiSvrObj, config.PPI.ColNode)
    print("<--------------------ppi.run------------------------>")
    ppi.run(ppiSvrObj)
    print("<--------------------ppiServer.run------------------------>")
    ppiServer.run(ppiSvrObj)
    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()

2.2 ppiServer相关的函数和操作

2.2.1 ppiServer.new

序号 参数 说明
1 “ppiServer” 模块运行实例的名称

功能:创建ppiServer实例

示例:

ppiSvrObj = ppiServer.new("ppiServer")

2.2.2 ppiServer.config

功能 示例 参数 说明
配置ppiServer实例 ppiServer.config(ppiSvrObj, cjson.encode(config.PPI.Device)) ppiSvrObj ppiServer.new实例的返回值 -
cjson.encode(config.PPI.Device) ppiServer配置参数 (详见config.lua)

2.2.3 ppiServer.setStation

功能 示例 参数 说明
配置Modbus站点信息 ppiServer.setStation(ppiSvrObj, cjson.encode(stationConfig)) ppiSvrObj ppiServer.new实例的返回值 -
stationConfig ppiServer的站点配置 (详见config.lua)

2.2.4 ppiServer.run

功能 示例 参数 说明
创建ppiServer启动实例 ppiServer.run(ppiSvrObj) ppiSvrObj ppiServer.new实例的返回值 -

2.2.5 ppiServer_load_station(ppiSvrObj, config.PPI.ColNode)

使ppiServer加载config.lua中PPI.ColNode段配置的ppiServer服务器。
函数原型如下(使用循环方式添加config.lua文件中Stations段的每一行):

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

3 config.lua配置说明
对于ppiServer的config.Lua一般只需要配置PPI部分,需要在采集结点中含有dbName即可
config.lua示例:

PPI = {
        Device = {
            portindex = 1,
            rate = 9600,
            databit = 8,
            stopbit = 1,
            parity = "Even", -- None/Odd/Even
            queryInterval = 100
        },
        ColNode = {
            --bit
            { 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 = 100, cnt = 10, ctype = "byte", dbName = "PPI_2V100L10"},
             { ID = 2, reg = "V",  addr = 506, cnt = 1,  ctype = "byte",  dbName = "PPI_2V506L1"},
            --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"},
        },
    },

PPI服务器的config.lua和PPI的一致,无需额外配置。
数据中心相关知识,请参考《APRUS编程手册_数据中心.pdf》。

文档更新时间: 2025-01-10 17:57   作者:CGL