User Tools

Site Tools


helvar

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
helvar [2023/05/21 17:38] emozolyakhelvar [2023/09/22 14:33] (current) emozolyak
Line 39: Line 39:
 PARTIAL_ETX = '$' PARTIAL_ETX = '$'
 FULL_ETX    = '#' FULL_ETX    = '#'
 + 
 function onScanStart () function onScanStart ()
     now = os.time()     now = os.time()
Line 47: Line 47:
    addDevice{name = "CMD",     shift = 0, base = 10, xtraFields = {} }    addDevice{name = "CMD",     shift = 0, base = 10, xtraFields = {} }
 end  end 
 + 
 function readRegister (reg, device, unitId) function readRegister (reg, device, unitId)
- 
     if (not readBuffer) then      if (not readBuffer) then 
         readBuffer = 'no data'          readBuffer = 'no data' 
     end      end 
-     +    return readBuffer 
-    return readBuffer -- will be assinged in writeRegister+
 end  end 
 + 
 function writeRegister (reg, device, unitId, newValue) function writeRegister (reg, device, unitId, newValue)
-     +  
-    sendString_(newValue) -- expects string to be written externally  +   sendString_(newValue) -- expects string to be written externally  
-    + 
    local buf = {}     local buf = {} 
 +   local tryCount = 0
        
    repeat     repeat 
 +       tryCount = tryCount + 1 ; DEBUG('tryCount = ' .. tryCount)
 +       
        local currentFrame = readUntil(PARTIAL_ETX, FULL_ETX)         local currentFrame = readUntil(PARTIAL_ETX, FULL_ETX) 
-       local lastChar 
        if currentFrame then         if currentFrame then 
            table.insert(buf, currentFrame)            table.insert(buf, currentFrame)
            lastChar = currentFrame[#currentFrame]            lastChar = currentFrame[#currentFrame]
        end         end 
-        +    until (lastChar == FULL_ETX or tryCount > 3)
-   until (not lastChar or (lastChar == FULL_ETX))+
        
-   if (#buf > 0) then  +    if (lastChar == FULL_ETX) then  
-        for i, row in ipairs (buf) do  +        readBuffer = buf  
-            readBuffer[i] table.concat(row) +        return true  
-        end  +    end  
-        readBuffer = table.concat(readBuffer) +     
-   end  +    readBuffer = 'read bad data!' 
- +    return true 
-   return true +
 end end
 + 
 -------------------------- Helpers -------------------------- -------------------------- Helpers --------------------------
 + 
 function sendString_(s) -- new sendString version  function sendString_(s) -- new sendString version 
 + 
     local sendBuf = {}      local sendBuf = {} 
    
Line 94: Line 92:
     sendBytes(sendBuf)     sendBytes(sendBuf)
 end end
 + 
 function readUntil(endByte1, endByte2) -- read input buffer untils endByte(s) or timeout  function readUntil(endByte1, endByte2) -- read input buffer untils endByte(s) or timeout 
 +    DEBUG("Entered readUntil.")
 +    
     local ONE_BYTE = 1      local ONE_BYTE = 1 
-    local buf = {}+    local buf, rb = {}, nil  
 +    
     repeat      repeat 
-        local rb = readBytes(ONE_BYTE) +        rb = readBytes(ONE_BYTE) 
         if rb then          if rb then 
-            rb = rb[ONE_BYTE]+            rb = rb[ONE_BYTE]          ; DEBUG('read byte: ' .. string.char(rb))
             table.insert(buf, rb)             table.insert(buf, rb)
         end          end 
     until (not rb or (rb == endByte1 or rb == endByte2))     until (not rb or (rb == endByte1 or rb == endByte2))
- +     
-    return ((#buf >= ONE_BYTE) and bufor false +    DEBUG("Exit on rb = " .. tostring(rb) ) 
 +    DEBUG('buf = ' .. table.concat(buf, ' ')) 
 +     
 +    if (#buf > 0) and (buf[#buf] == endByte1 or buf[#buf] == endByte2) then  
 +        DEBUG('Got right frame : ') return buf  
 +    else  
 +        DEBUG('Got wrong frame : ') ; return false  
 +    end 
 end  end 
 </code> </code>
  
helvar.1684690737.txt.gz · Last modified: 2023/05/21 17:38 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki