hydrus-flowmeter
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
hydrus-flowmeter [2023/08/17 10:47] – emozolyak | hydrus-flowmeter [2023/08/18 08:32] – emozolyak | ||
---|---|---|---|
Line 1: | Line 1: | ||
{{ : | {{ : | ||
- | ====== Connecting to HYDRUS by DIEHL flowmeter | + | ====== Connecting to DIEHL HYDRUS |
<WRAP center round info 80%> | <WRAP center round info 80%> | ||
Line 16: | Line 16: | ||
end | end | ||
- | POLL_DELAY | + | POLL_DELAY |
HEX_NUMBERING = 16 -- numbering system conversion | HEX_NUMBERING = 16 -- numbering system conversion | ||
Line 35: | Line 35: | ||
end | end | ||
end | end | ||
+ | -- prints any parameters | ||
function DBG(...) | function DBG(...) | ||
for i = 1, #arg do | for i = 1, #arg do | ||
Line 48: | Line 48: | ||
------ Table helpers ----------------------------- | ------ Table helpers ----------------------------- | ||
- | function table.findPattern(t, | + | function table.findPattern(t, |
for tabIndex, _ in ipairs(t) do | for tabIndex, _ in ipairs(t) do | ||
Line 60: | Line 60: | ||
end | end | ||
end | end | ||
- | |||
if matchFlag then | if matchFlag then | ||
- | return (tabIndex + # | + | return (tabIndex + # |
end | end | ||
end | end | ||
Line 68: | Line 67: | ||
end | end | ||
- | function table.sub(t, | + | function table.sub(t, |
local tmpTable = {} | local tmpTable = {} | ||
for k = startIndex, endIndex do | for k = startIndex, endIndex do | ||
Line 76: | Line 75: | ||
end | end | ||
- | table.hexView = function(hextab, | + | table.hexView = function(hextab, |
local hex = {} | local hex = {} | ||
for _, hexbyte in ipairs(hextab) do | for _, hexbyte in ipairs(hextab) do | ||
Line 89: | Line 88: | ||
return tonumber(hex) | return tonumber(hex) | ||
end | end | ||
+ | -- integer convertions | ||
+ | table.int = function(t) | ||
+ | | ||
+ | end | ||
+ | |||
- | function getHexByteAsStr(inputByte) | + | -- gets 2 - char hex string of a byte |
+ | function getHexByteAsStr(inputByte) | ||
local strByte = string.format(" | local strByte = string.format(" | ||
return (#strByte == 1 and ' | return (#strByte == 1 and ' | ||
end | end | ||
- | table.reverse = function (tab) -- reverses a table | + | table.reverse = function (tab) |
local outTable = {} | local outTable = {} | ||
for i = #tab, 1, -1 do | for i = #tab, 1, -1 do | ||
Line 110: | Line 115: | ||
CTRL_LONG_FRAME_STX = 0x68 | CTRL_LONG_FRAME_STX = 0x68 | ||
- | REQ_UD2 | + | REQ_UD2 |
- | REQ_UD2_ | + | REQ_UD2_ |
BRDCAST_NET_LAYER_ADDR = 0xFD -- 253 | BRDCAST_NET_LAYER_ADDR = 0xFD -- 253 | ||
Line 127: | Line 132: | ||
dataHandlers = { | dataHandlers = { | ||
- | volume = {pattern = {0xC, 0x14}, length = 4, func = table.bcd}, | + | volume = {pattern = {0xC, 0x14}, |
- | flow = {pattern = {0xB, 0x3C}, length = 3, func = table.bcd}, | + | flow = {pattern = {0xB, 0x3C}, |
- | tint = {pattern = {0xA, 0x5A}, length = 2, func = table.bcd}, | + | tint = {pattern = {0xA, 0x5A}, |
- | text = {pattern = {0xA, 0x66}, length = 2, func = table.bcd} | + | text = {pattern = {0xA, 0x66}, |
+ | err | ||
} | } | ||
Line 138: | Line 144: | ||
| | ||
| | ||
+ | | ||
end | end | ||
Line 144: | Line 151: | ||
if (not lastReadTimeStamp or (now - lastReadTimeStamp) >= POLL_DELAY ) then | if (not lastReadTimeStamp or (now - lastReadTimeStamp) >= POLL_DELAY ) then | ||
- | PROBE_PACKET[PROBE_PACKET_CRC_POS] = getCRC(PROBE_PACKET, | + | PROBE_PACKET[PROBE_PACKET_CRC_POS] = getCRC(PROBE_PACKET |
+ | | ||
+ | | ||
if (not sendBytes(PROBE_PACKET) ) then | if (not sendBytes(PROBE_PACKET) ) then | ||
Line 152: | Line 161: | ||
local meterDataFrame = readUntil(ETX) | local meterDataFrame = readUntil(ETX) | ||
- | |||
if (# | if (# | ||
-- parsing | -- parsing | ||
for param, struc in pairs(dataHandlers) do | for param, struc in pairs(dataHandlers) do | ||
- | | ||
local foundPos = table.findPattern(meterDataFrame, | local foundPos = table.findPattern(meterDataFrame, | ||
if foundPos then | if foundPos then | ||
- | | + | |
+ | local foundBytes = table.sub(meterDataFrame, | ||
+ | , foundPos + struc.length - 1) | ||
+ | DBG(' | ||
+ | foundBytes = table.reverse(foundBytes) | ||
+ | DBG(" | ||
+ | local actualData = struc.func(foundBytes) | ||
+ | DBG(" | ||
+ | data[param] = actualData | ||
+ | | ||
lastReadTimeStamp = now | lastReadTimeStamp = now | ||
- | local foundBytes = table.sub(meterDataFrame, | ||
- | , foundPos + struc.length - 1) ; DBG(' | ||
- | foundBytes = table.reverse(foundBytes) | ||
- | data[param] = struc.func(foundBytes) | ||
else | else | ||
ERROR(" | ERROR(" | ||
Line 176: | Line 188: | ||
end | end | ||
| | ||
- | if (device.name == ' | + | if (device.name == ' |
- | if (device.name == ' | + | if (device.name == ' |
- | if (device.name == ' | + | if (device.name == ' |
- | if (device.name == ' | + | if (device.name == ' |
+ | if (device.name == ' | ||
end | end | ||
Line 188: | Line 201: | ||
-------------------------- Other helpers -------------------------- | -------------------------- Other helpers -------------------------- | ||
- | function readUntil(endByte) | + | function readUntil(endByte) |
- | local ONE_BYTE = 1 | + | local ONE_BYTE, buf = 1, {} |
- | | + | |
repeat | repeat | ||
local rx = readBytes(ONE_BYTE) | local rx = readBytes(ONE_BYTE) | ||
if rx then | if rx then | ||
- | rx = rx[1] | + | rx = rx[1] ; table.insert(buf, |
- | | + | |
end | end | ||
until (not rx or (rx == endByte)) | until (not rx or (rx == endByte)) | ||
Line 202: | Line 214: | ||
end | end | ||
- | function getCRC(a, pos, len) -- calc. CRC sum for the mbus packet | + | function getCRC(a, pos, len) |
local sum, mask = 0, 0xFF | local sum, mask = 0, 0xFF | ||
Line 210: | Line 222: | ||
end | end | ||
return sum | return sum | ||
- | end | + | end </ |
- | + | ||
- | + | ||
- | + | ||
- | </ | + | |
hydrus-flowmeter.txt · Last modified: 2024/02/20 14:42 by emozolyak