Релейный модуль Socket-4 — различия между версиями
Материал из WebHMI Wiki
Строка 1: | Строка 1: | ||
<p>Работа WebHMI с модулем [http://vkmodule.com.ua/Ethernet/Socket4.html VK Socket-4] легко реализуется благодаря возможности использования [http://wiki.webhmi.com.ua/index.php/%D0%9F%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%B8%D0%B5_%D0%BF%D1%80%D0%BE%D1%82%D0%BE%D0%BA%D0%BE%D0%BB%D1%8B пользовательских протоколов] на встроенном языке скриптов.</p> | <p>Работа WebHMI с модулем [http://vkmodule.com.ua/Ethernet/Socket4.html VK Socket-4] легко реализуется благодаря возможности использования [http://wiki.webhmi.com.ua/index.php/%D0%9F%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%B8%D0%B5_%D0%BF%D1%80%D0%BE%D1%82%D0%BE%D0%BA%D0%BE%D0%BB%D1%8B пользовательских протоколов] на встроенном языке скриптов.</p> | ||
− | Пример функций поддержки протокола Socket 4 на языке Lua | + | Данный протокол поддерживает адреса вида C0, C1, .., C7. Они соответствуют релейным выходам 0..7. |
+ | |||
+ | Тип протокола: TCP/IP | ||
+ | |||
+ | Порт по умолчанию: 9761 | ||
+ | |||
+ | Проверка адреса: ^(C[0-7])$ | ||
+ | |||
+ | Пример функций поддержки протокола Socket 4 на языке Lua. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> |
Версия 15:35, 26 сентября 2016
Работа 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