Модуль ввода Socket-1

Материал из WebHMI Wiki
Перейти к: навигация, поиск

Работа WebHMI с модулем VK Socket-1 легко реализуется благодаря возможности использования пользовательских протоколов на встроенном языке скриптов.

Данный протокол поддерживает адреса вида DI0, DI1, DI2, DI3. Они соответствуют входам 0..3.

Тип протокола: TCP/IP

Порт по умолчанию: 9761

Проверка адреса: ^(DI[0-3])$

Пример функций поддержки протокола Socket 1 на языке Lua:

local errorCount = 0;
local lastReponse = {};
local alreadyRead = false;
function createDevices ()
  addDevice({name = "DI",  shift = 0, base = 10, xtraFields = {}});
end
function onScanStart ()
    alreadyRead = false;
end

function readRegister (reg, device, unitId)
    if alreadyRead == false then
        local request = {};
        request[1] = 50;
        local res = sendBytes(request);
    
        if (res == false) then
            DEBUG("Can't send bytes");
            return false;
        end
        
        local response = {};
        response = readBytes(1);
        if (response == false) then
            errorCount = errorCount + 1;
            if (errorCount > 3) then
                closeConnection();
                errorCount = 0;
            end
            DEBUG("Can't read response");
            return false;
        end
        while (response ~= false and response[1] == 49) do
          response = readBytes(2);
          response = readBytes(1);
        end
        response = readBytes(4);
        res = #response;
        
        if (res ~= 4) then
            errorCount = errorCount + 1;
            if (errorCount > 3) then
                closeConnection();
                errorCount = 0;
            end
            DEBUG("Read " .. res .. " bytes. Incomplete response.");
            return false;
        end
        for i = 1,4 do
            lastReponse[i] = response[i];
        end
        alreadyRead = true;
    end

    local result = {0};
    if (reg.dataType < 2) then
        result[1] = lastReponse[reg.internalAddr + 1];
    end
    if (reg.dataType == 2) then
        result[2] = lastReponse[reg.internalAddr + 1];
    end
    if (reg.dataType == 3) then
        result[2] = 0;
        result[3] = 0;
        result[4] = lastReponse[reg.internalAddr + 1];
    end
    return result;
end

function writeRegister (reg, device, unitId, newValue)
    return true;
end