Релейный модуль Socket-4

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

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

Данный протокол поддерживает адреса вида C0, C1, .., C7. Они соответствуют релейным выходам 0..7.

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

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

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

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

local errorCount = 0;
local lastReponse = {};
local alreadyRead = false;

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

function readRegister (reg, device, unitId)
    if alreadyRead == false then
        local request = {};
        request[1] = 35;
        local res = sendBytes(request);
    
        if (res == false) then
            DEBUG("Can't send bytes");
            return false;
        end
        
        local response = {};
        response = readBytes(9);
        
        if (response == false) then
            errorCount = errorCount + 1;
            if (errorCount > 3) then
                closeConnection();
                errorCount = 0;
            end
            DEBUG("Can't read response");
            return false;
        end
        res = #response;
        
        if (res ~= 9) then
            errorCount = errorCount + 1;
            if (errorCount > 3) then
                closeConnection();
                errorCount = 0;
            end
            DEBUG("Read " .. res .. " bytes. Incomplete response.");
            return false;
        end
        for i = 1,8 do
            lastReponse[i] = response[i+1];
        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)
    local request = {};
    request[1] = 34;
    request[2] = reg.internalAddr;
    request[3] = 0;
    if (newValue > 0) then
        request[3] = 1;
    end
    request[4] = 0;
    local res = sendBytes(request);
    if (res == false) then
        DEBUG("Can't send bytes");
        return false;
    end
    
    local response = {};
    local requestLen = #request;

    response = readBytes(requestLen);
    if (response == false) then
      DEBUG("Can't read response");
      return false;
    end
    res = #response;
    if (res ~= requestLen) then
      DEBUG("Wrong response length");
      return false;
    end
    for i = 1,res do
        if (response[i] ~= request[i]) then
            DEBUG("Wrong response");
            return false;
        end
        
    end
    return true;
end