heat_meter_sks-3_axis_industries
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Last revisionBoth sides next revision | ||
heat_meter_sks-3_axis_industries [2018/08/02 12:43] – emozolyak | heat_meter_sks-3_axis_industries [2018/08/02 12:58] – emozolyak | ||
---|---|---|---|
Line 8: | Line 8: | ||
<code lua> | <code lua> | ||
- | -- строка инициализации, | + | -- app_reset packet addr and CRC should be changed |
- | local app_reset = {0x68, 0x04, 0x04, 0x68, 0x53, 0x5D, 0x50, 0x00, 0x01, 0x16}; | + | local app_reset = {0x68, 0x04, 0x04, 0x68, 0x53, 0x5D, 0x50, 0x00, 0x01, 0x16} |
- | -- запрос на получение данных | + | -- get data packet |
- | local REQ_UD2 = {0x10, 0x7B, 0x5D, 0xD8, 0x16}; | + | local REQ_UD2 = {0x10, 0x7B, 0x5D, 0xD8, 0x16} |
- | local err_cnt = 0; -- счетчик ошибок | + | local err_cnt = 0 -- счетчик ошибок |
- | local dif_err_mask = 0x30; | + | local dif_err_mask = 0x30 -- parameter error mask |
- | local resp3 = {}; -- Таблица для хранения значимой части ответа | + | local resp3 = {} -- table for response bytes |
- | local alreadyRead = false; -- флаг выполнения полного чтения | + | local alreadyRead = false |
- | local timeStmp = 0; | + | local timeStmp = 0 -- timestamp of full read |
- | AppRST = false; | + | AppRST = false -- flag of app reset done |
- | + | ||
- | -- Вспомогатеьные функции -- | + | |
- | function getBCD(a, | + | |
- | + | ||
- | local str = ""; | + | |
- | local i,max_i = pos, pos+len-1; | + | |
- | TRACE(" | + | |
- | while i <= max_i do | + | |
- | if (a[i] < 0x0f) then | + | |
- | str = " | + | |
- | else | + | |
- | str = string.format(" | + | |
- | end | + | |
- | i = i + 1; | + | |
- | end | + | |
- | TRACE(" | + | |
- | local result = tonumber(str, | + | |
- | TRACE(result); | + | |
- | return result; | + | |
- | end -- getBCD | + | |
- | + | ||
- | -- GetCRC посчитать контрольную сумму | + | |
- | function getCRC(a, | + | |
- | + | ||
- | local sum = 0; | + | |
- | + | ||
- | for i = pos, pos+len-1 do | + | |
- | sum = sum + a[i]; | + | |
- | end | + | |
- | TRACE(" | + | |
- | return sum; | + | |
- | end -- getCRC | + | |
- | + | ||
- | -- Расширенный поиск 2-ая версия | + | |
- | function efindpos(a, dif, dife, vif, vife) | + | |
- | -- | + | |
- | local dife_exists = false; | + | |
- | local vife_exists = false; | + | |
- | local err_flag = false; | + | |
- | -- проверить наличие ошибок | + | |
- | if (bit.band(0x30, | + | |
- | err_flag = true ; | + | |
- | end | + | |
- | -- проверить dif | + | |
- | if (bit.band(0x80, | + | |
- | dife_exists = true ; | + | |
- | end | + | |
- | -- есть ли vife | + | |
- | if (bit.band(0x80, | + | |
- | vife_exists = true ; | + | |
- | end | + | |
- | -- | + | |
- | if (not dife_exists) then | + | |
- | if (not vife_exists) then -- dif vif | + | |
- | for i,v in ipairs (a) do | + | |
- | if dif == bit.band(v, | + | |
- | if a[i+1] == vif then | + | |
- | return i+2, | + | |
- | end | + | |
- | end | + | |
- | end -- loop | + | |
- | else -- vife_exists | + | |
- | for i,v in ipairs (a) do | + | |
- | if dif == bit.band(v, | + | |
- | if (a[i+1]==vif) and (a[i+2]==vife) then | + | |
- | return i+3, | + | |
- | end | + | |
- | end | + | |
- | end -- loop | + | |
- | end | + | |
- | else -- dife_exists | + | |
- | -- INFO(" | + | |
- | if (not vife_exists) then -- dif vif | + | |
- | for i,v in ipairs (a) do | + | |
- | if (dif == bit.band(v, | + | |
- | if a[i+2] == vif then | + | |
- | return i+3, | + | |
- | end | + | |
- | end | + | |
- | end -- loop | + | |
- | else -- dife vife_exists | + | |
- | -- DEBUG(" | + | |
- | for i,v in ipairs (a) do | + | |
- | local condition = ( (dif == bit.band(v, | + | |
- | if condition then -- сравниваем мл тетраду без ошибки | + | |
- | if (a[i+2]==vif) and (a[i+3]==vife) then | + | |
- | return i+4, | + | |
- | end | + | |
- | end | + | |
- | end -- loop | + | |
- | end | + | |
- | + | ||
- | end -- if | + | |
- | return nil; -- ничего не нашли | + | |
- | end -- of efindpos | + | |
- | + | ||
-- | -- | ||
function createDevices () | function createDevices () | ||
-- prefix | -- prefix | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
end | end | ||
Line 141: | Line 45: | ||
function onScanStart () | function onScanStart () | ||
- | AppRST = false; | + | AppRST = false |
end | end | ||
function readRegister (reg, device, unitId) | function readRegister (reg, device, unitId) | ||
- | | + | |
- | app_reset[6] = unitId; | + | app_reset[6] = unitId |
- | REQ_UD2[3] = unitId; | + | REQ_UD2[3] = unitId |
- | -- теперь поставить контрольные СУММЫ | + | |
- | app_reset[9] = getCRC(app_reset, | + | -- now put CRC |
- | REQ_UD2[4] = getCRC(REQ_UD2, | + | app_reset[9] = getCRC(app_reset, |
+ | REQ_UD2[4] = getCRC(REQ_UD2, | ||
| | ||
-- Application reset | -- Application reset | ||
- | if not AppRST then -- была ли инициализация | + | if not AppRST then -- not yet ? |
- | local res = sendBytes(app_reset); -- | + | local res = sendBytes(app_reset) -- |
- | DEBUG(" | + | DEBUG(" |
if (res == false) then | if (res == false) then | ||
- | | + | |
- | return false; | + | return false |
end | end | ||
- | local response = readBytes(1); -- прием ответа | + | local response = readBytes(1) -- get reply |
if (response == false) then | if (response == false) then | ||
- | ERROR(" | + | ERROR(" |
- | return false; | + | return false |
else | else | ||
- | | + | |
- | AppRST = true; | + | AppRST = true |
- | err_cnt = 0; | + | err_cnt = 0 |
end | end | ||
end | end | ||
- | + | ||
- | + | -- after 10 errors reset againg | |
- | -- сделать инициализацию | + | |
if err_cnt > 10 then | if err_cnt > 10 then | ||
- | AppRST = false; | + | AppRST = false |
- | TRACE(" | + | TRACE(" |
- | return false; | + | return false |
end -- err check | end -- err check | ||
- | -- если прошло | + | -- after 20 seconds getdata again |
- | local now = os.time(); | + | local now = os.time() |
if (not alreadyRead) then | if (not alreadyRead) then | ||
- | timeStmp = now; -- запомнить время | + | timeStmp = now -- store time |
-- REQ_UD2 -- | -- REQ_UD2 -- | ||
- | local resp2 = sendBytes(REQ_UD2); -- | + | local resp2 = sendBytes(REQ_UD2) -- |
if (resp2 == false) then | if (resp2 == false) then | ||
- | | + | |
- | return false; | + | return false |
end | end | ||
- | -- прием первых | + | -- get first 4 bytes |
- | local response2 = readBytes(4); | + | local response2 = readBytes(4) |
if (response2 == false) then | if (response2 == false) then | ||
- | ERROR(" | + | |
- | | + | |
- | | + | |
elseif not ((response2[1] == 0x68) and (response2[4] == 0x68)) then | elseif not ((response2[1] == 0x68) and (response2[4] == 0x68)) then | ||
- | | + | |
- | err_cnt = err_cnt + 1; | + | err_cnt = err_cnt + 1 |
- | return false; | + | return false |
else | else | ||
- | -- нормальное чтение читаем весь пакет | + | -- Ok reading full packet |
- | local length = response2[2]; -- длину берем из пакета | + | local length = response2[2] -- get len from packet |
- | -- прием ВСЕГО ответа | + | -- get ALL |
- | resp3 = readBytes(length+2); | + | resp3 = readBytes(length+2) |
if (resp3 == false) then | if (resp3 == false) then | ||
- | ERROR(" | + | |
- | | + | |
- | | + | |
else | else | ||
- | | + | |
- | DEBUG(" | + | DEBUG(" |
- | DEBUG(" | + | DEBUG(" |
- | DEBUG(" | + | DEBUG(" |
-- ID 4 bytes -- | -- ID 4 bytes -- | ||
- | local ID_str = "" | + | local ID_str = "" |
for i = 4, 7 do | for i = 4, 7 do | ||
if resp3[i] < 10 then | if resp3[i] < 10 then | ||
- | | + | |
else | else | ||
- | | + | |
| | ||
end | end | ||
- | DEBUG(" | + | DEBUG(" |
-- Manufacrutre 2 bytes -- | -- Manufacrutre 2 bytes -- | ||
- | local m_str = "" | + | local m_str = "" |
for i = 8, 9 do | for i = 8, 9 do | ||
if resp3[i] < 10 then | if resp3[i] < 10 then | ||
- | m_str = " | + | m_str = " |
else | else | ||
- | m_str = string.format(" | + | m_str = string.format(" |
| | ||
end | end | ||
- | | + | |
- | -- Version -------------------- | + | -- Version -------------------- |
- | DEBUG(" | + | DEBUG(" |
- | DEBUG(" | + | DEBUG(" |
- | DEBUG(" | + | DEBUG(" |
- | DEBUG(" | + | DEBUG(" |
---------------------------------------- | ---------------------------------------- | ||
-- signature | -- signature | ||
- | local m_str = "" | + | local m_str = "" |
for i = 14, 15 do | for i = 14, 15 do | ||
if resp3[i] < 10 then | if resp3[i] < 10 then | ||
- | m_str = " | + | m_str = " |
else | else | ||
- | m_str = string.format(" | + | m_str = string.format(" |
| | ||
end | end | ||
- | | + | |
- | alreadyRead = true; | + | alreadyRead = true |
end | end | ||
end | end | ||
end | end | ||
- | -- настройка частоты опроса | + | -- adjust real polling interval here |
if (now - timeStmp) >= 15 then | if (now - timeStmp) >= 15 then | ||
- | alreadyRead = false; | + | alreadyRead = false |
end | end | ||
-- | -- | ||
- | local position = 0; | + | local position = 0 |
- | local resp_num = 0; | + | local resp_num = 0 |
- | local err_flag = false; | + | local err_flag = false |
- | -- Ищем и проверяем значение | + | -- find and check value using aux function |
- | | + | |
- | -- преобразуем в число | + | -- convert to number |
- | | + | |
- | | + | |
if not err_flag then | if not err_flag then | ||
- | return resp_num; | + | return resp_num |
| | ||
- | | + | |
| | ||
Line 282: | Line 186: | ||
-- Add your code here | -- Add your code here | ||
end | end | ||
+ | |||
+ | -- AUXILARY FUNCTIONS -- | ||
+ | function getBCD(a, | ||
+ | |||
+ | local str = "" | ||
+ | local i,max_i = pos, pos+len-1 | ||
+ | TRACE(" | ||
+ | while i <= max_i do | ||
+ | if (a[i] < 0x0f) then | ||
+ | str = " | ||
+ | else | ||
+ | str = string.format(" | ||
+ | end | ||
+ | i = i + 1 | ||
+ | end | ||
+ | TRACE(" | ||
+ | local result = tonumber(str, | ||
+ | TRACE(result) | ||
+ | return result | ||
+ | end -- getBCD | ||
+ | |||
+ | -- calc CRC | ||
+ | function getCRC(a, | ||
+ | | ||
+ | local sum = 0 | ||
+ | |||
+ | for i = pos, pos+len-1 do | ||
+ | sum = sum + a[i] | ||
+ | end | ||
+ | TRACE(" | ||
+ | return sum | ||
+ | end -- getCRC | ||
+ | |||
+ | -- find data in packet using DIF VIF | ||
+ | function efindpos(a, dif, dife, vif, vife) | ||
+ | -- | ||
+ | local dife_exists = false | ||
+ | local vife_exists = false | ||
+ | local err_flag = false | ||
+ | -- check for error | ||
+ | if (bit.band(0x30, | ||
+ | err_flag = true | ||
+ | end | ||
+ | -- check dif | ||
+ | if (bit.band(0x80, | ||
+ | dife_exists = true | ||
+ | end | ||
+ | -- if vife exist | ||
+ | if (bit.band(0x80, | ||
+ | vife_exists = true | ||
+ | end | ||
+ | -- | ||
+ | if (not dife_exists) then | ||
+ | if (not vife_exists) then -- dif vif | ||
+ | for i,v in ipairs (a) do | ||
+ | if dif == bit.band(v, | ||
+ | if a[i+1] == vif then | ||
+ | return i+2, | ||
+ | end | ||
+ | end | ||
+ | end -- loop | ||
+ | else -- vife_exists | ||
+ | for i,v in ipairs (a) do | ||
+ | if dif == bit.band(v, | ||
+ | if (a[i+1]==vif) and (a[i+2]==vife) then | ||
+ | return i+3, | ||
+ | end | ||
+ | end | ||
+ | end -- loop | ||
+ | end | ||
+ | else -- dife_exists | ||
+ | -- INFO(" | ||
+ | if (not vife_exists) then -- dif vif | ||
+ | for i,v in ipairs (a) do | ||
+ | if (dif == bit.band(v, | ||
+ | if a[i+2] == vif then | ||
+ | return i+3, | ||
+ | end | ||
+ | end | ||
+ | end -- loop | ||
+ | else -- dife vife_exists | ||
+ | -- DEBUG(" | ||
+ | for i,v in ipairs (a) do | ||
+ | local condition = ( (dif == bit.band(v, | ||
+ | if condition then -- сравниваем мл тетраду без ошибки | ||
+ | if (a[i+2]==vif) and (a[i+3]==vife) then | ||
+ | return i+4, | ||
+ | end | ||
+ | end | ||
+ | end -- loop | ||
+ | end | ||
+ | |||
+ | end -- if | ||
+ | return nil -- found noting | ||
+ | end -- of efindpos | ||
</ | </ | ||
heat_meter_sks-3_axis_industries.txt · Last modified: 2018/08/02 13:00 by emozolyak