--add by: liang   2019-07-09


--[[采集常用表说明:
dc_dev 采集设备表,包含采集设备的连接信息,包含 IP端口、串口信息等
dc_dev_var 采集变量表,主要字段var_id、var_name、eqpt_id、var_busi_func_code
两表通过dc_dev_id关联

var_busi_func_code 为采集变量 业务功能码,在dc_var_busi_func表中
]]


--要连接采集,首先需要知道采集服务的IP和端口,一般采集默认的端口是9002
--采集服务IP端口一般在站点设备表dc_loc表中,字段为dc_loc_ip、dc_loc_port


function main()
    local sSiteID = '999';
    local sEqptID = '888';
    local sVarFunc = 'barcode';

    --查询采集服务的IP 和 端口
    local sql = "select dc_loc_id,dc_loc_ip,dc_loc_port from dc_loc where dc_loc_id='%s'";
    local help = CSelectHelp();
    if db:select(string.format(sql, sSiteID), '', help) < 0 then
        return print('查询站点采集服务连接信息失败'..db:error());
    elseif help:size() <= 0 then
        return print('站点ID信息不存在');
    elseif string.len(help:vs(0, 'dc_loc_ip')) <= 0 then
        return print('未配置采集服务连接信息');
    end
    local iPort = tonumber(help:vs(0, 'dc_loc_port'));
    if iPort == nil then
        iPort = 9002;
    end
    local pICE = LuaICE();
    pICE:setServer(help:vs(0, 'dc_loc_ip'), iPort);
    if pICE:isLogin() == false then
        if pICE:login() == false then
            return print('采集服务连接失败');
        end
    end

    --查询指定站点下  某台设备 业务功能为barcode的采集变量var_name,通过dc_dev_id和var_name读取写入指定数据
    sql = [[select b.dc_dev_id,b.var_id,b.var_name from dc_dev a,dc_dev_var b where b.var_busi_func_code='%s' 
        and b.eqpt_id='%s' and a.dc_loc_id='%s' and a.dc_dev_id=b.dc_dev_id]];
    local helpVar = CSelectHelp();
    if db:select(string.format(sql, sVarFunc, sEqptID, sSiteID), '', helpVar) < 0 then
        return print('查询采集信息失败');
    elseif help:size() <= 0 then
        return print('确认相关采集变量信息');
    end


    local outIt = CIntent();
    local itIn = CIntent();


    --读取采集服务中的所有变量数据
    if pICE:command('dc_get_all', '', outIt) < 0 then
        return print('从采集服务读取数据失败');
    end
    local helpDc = CSelectHelp();
    outIt:getHelp('help', helpDc);
    helpDc:dump();


    --读取指定dc_dev_id下所有变量数据
    itIn:clear();
    outIt:clear();
    itIn:set('dc_dev_id', helpVar:vs(0, 'dc_dev_id'));
    if pICE:command('dc_get_dev', itIn:toString(), outIt) < 0 then
        return print('从采集服务读取数据失败');
    end
    local helpDc = CSelectHelp();
    outIt:getHelp('help', helpDc);
    helpDc:dump();


    --读取指定站点dc_loc_id下所有变量数据
    itIn:clear();
    outIt:clear();
    itIn:set('dc_loc_id', sSiteID);
    if pICE:command('dc_get_site', itIn:toString(), outIt) < 0 then
        return print('从采集服务读取数据失败');
    end
    local helpDc = CSelectHelp();
    outIt:getHelp('help', helpDc);
    helpDc:dump();


    --单变量写入到采集设备
    itIn:clear();
    outIt:clear();
    itIn:set('var_name', helpVar:vs(0, 'var_name'));
    itIn:set('value', '678');
    itIn:set('dc_dev_id', helpVar:vs(0, 'dc_dev_id'));
    iRet = pICE:command('dc_write', itIn:toString(), outIt);
    if iRet < 0 then
        return print('写入失败');
    end


    --单变量读取
    itIn:clear();
    local outmsg = CTNString();
    itIn:set('var_name', helpVar:vs(0, 'var_name'));
    itIn:set('dc_dev_id', helpVar:vs(0, 'dc_dev_id'));
    itIn:set('real_read', '0');--0为读取采集服务内存中的数据,1为直接读取PLC采集设备的数据,数据准确性时效性更好
    iRet = pICE:commandStr('dc_get', itIn:toString(), outmsg);
    if iRet < 0 then
        return print('读取失败');
    end
    print(outmsg:get())

    return true;
end

if main() ~= true then

end
Copyright © TouchNet 2015 all right reserved,powered by Gitbook最后修订时间: 2022-06-21 09:21:04

results matching ""

    No results matching ""