Aller au contenu

Quick Apps


Krikroff

Messages recommandés

Il y a 17 heures, Lazer a dit :

Si on veut être précis, c'est ni l'un ni l'autre... c'est pour cela que je t'ai donné le lien vers un tuto qui parle bien de "portée" des variables.

 

Merci pour la réponse.

Je vais regarder cela en détail ce soir, parce que là je suis perdu. Mais c'est sûrement dû à une étroitesse d'esprit.

Modifié par G.RozNCo
Lien vers le commentaire
Partager sur d’autres sites

Bonjour,

 

Est ce que quelqu'un aurait une explication concernant le déroulé des fonctions, ou plutot leur timming?

 

Dans ce code lorsque je lance la fonction "Bouton_Read()"  je m'attends a avoir [data sent] puis ma reponse "MB_Reponse = self:read()" et enfin [send DONE X] et [read DONE X].

Mais j'obtiens un déroulé complètement different et pas de return exploitable.

Seule la fonction "onDataReceived(data)" recoit une réponse.

 

Une idée ou mon erreur se situe?

 

      [28.05.2024] [09:18:28] [DEBUG] [QUICKAPP135]: send DONE X

      [28.05.2024] [09:18:28] [DEBUG] [QUICKAPP135]: read DONE X

      [28.05.2024] [09:18:28] [DEBUG] [QUICKAPP135]: Data: / MB_Reponse: / GetVar MB_Reponse: 0701040441973333[28.05.2024]

      [09:18:28] [DEBUG] [QUICKAPP135]: data sent

 

-- Variables
    local data
    local MB_Reponse
    local MB_Float32, MB_Signe, MB_Exposant, MB_Mantisse
--

function QuickApp:Float32toReal(MB_Float32) -- Conversion Float32 en Real
    MB_Signe	= math.pow(-1, math.fmod( MB_Float32, 2 ))
	MB_Exposant	= math.pow( 2, math.modf( MB_Float32 / 8388608 ) - 127 )
	MB_Mantisse	= 8388608 + math.fmod( MB_Float32, 8388608 )

    return (( MB_Signe * MB_Mantisse * MB_Exposant ) / 8388608)
end	--function Float32toReal

function QuickApp:Bouton_Read()
    self:send(string.char(0x01,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x00,0x48,0x00,0x02))
    MB_Reponse = self:read()

	QuickApp:debug("Data: ", data, "/ MB_Reponse: ",MB_Reponse, "/ GetVar MB_Reponse: ",  self:getVariable("MB_Reponse"))
end

function QuickApp:onDataReceived(data)
    self:setVariable("MB_Reponse",data)
end

function QuickApp:onInit()
    local ModBus
    self:trace("onInit")
    self.ip = self:getVariable("MultiCell_IP")
    self.port = tonumber(self:getVariable("MultiCell_Port"))
    self.ModBus = net.TCPSocket() -- creation of a TCPSocket instance
    self:connect()
end

function QuickApp:connect()                         -- a method to open a TCP connection.
    self.ModBus:connect(self.ip, self.port, {         -- connection to the device with the specified IP and port
        success = function()                        -- the function will be triggered if the connection is correct
            self:trace("Connected ")                 -- if the connection is successful, the data readout loop will be called 
            --self:read()          -- launching a data readout
        end,
        error = function(err) -- a function that will be triggered in case of an incorrect connection, e.g. timeout
            self:disconnect()  --self.sock:close() -- closing the socket
            self:debug("connection error",err)
            fibaro.setTimeout(5000, function() self:connect() end) -- re-connection attempt (every 5s)
        end,
    })
end

function QuickApp:disconnect()                         -- a method to close the TCP connection.
    self.ModBus:close() -- closing the socket
    self:trace("disconnected")
end

function QuickApp:send(strToSend) -- sending command 
    self.ModBus:write(strToSend, {
        success = function() -- the function that will be triggered when the data is correctly sent
            self:debug("data sent")
        end,
        error = function(err) -- the function that will be triggered in the event of an error in data transmission
            self:debug("error while sending data")
        end
    })
    self:debug("send DONE X")
end

function QuickApp:read()
    self.ModBus:read({ -- reading a data package from the socket
        success = function(data)
            --self:setVariable("MB_Reponse",data)
            self:onDataReceived(data) -- handling of received data
            --self:debug("read DONE")
            return data 
        end,
        error = function() -- a function that will be called in case of an error when trying to receive data, e.g. disconnecting a socket
            self:error("response error" , error)
            self:disconnect()  --self.sock:close() -- socket closed
            fibaro.setTimeout(5000, function() self:connect() end) -- re-connection attempt (every 5s)
        end
    })
    self:debug("read DONE X")
end

function QuickApp:HexDumpString(str,spacer)
    return (
    string.gsub(str,"(.)",
        function (c)
            return string.format("%02X%s",string.byte(c), spacer or ""); --convertion décimal en hexa
        end))
end

 

Lien vers le commentaire
Partager sur d’autres sites

C'est normal, car tu fais appel à la fonction read() de la librairie TCPsocket qui a un mode de fonctionnement asynchrone.
Cela va t'obliger à architecturer ton code différemment.

 

J'ai fait un topo ici (avec la fonction http:request() mais le principe est le même) :

 

 

 

Lien vers le commentaire
Partager sur d’autres sites

TOP! Merci pour ton aides!

 

il y a 25 minutes, Lazer a dit :

C'est normal, car tu fais appel à la fonction read() de la librairie TCPsocket qui a un mode de fonctionnement asynchrone.
Cela va t'obliger à architecturer ton code différemment.

 

J'ai fait un topo ici (avec la fonction http:request() mais le principe est le même) :

Lien vers le commentaire
Partager sur d’autres sites

×
×
  • Créer...