Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Question

Posted

Hi, need to controll Epson projector (send PWR On, PWR Off, PWR?) messages

For controllinhg Epson, need to send a command for establishing connection (0x450x530x430x2F0x560x0500x2E0x6E0x650x740x100x030x000x000x000x00) and after that send the command for interogating power status (0x500x570x520x3f0x0d)

 

Testing this from Hercules utility, works great

When coding in LUA, after sending first command, when trying to send second command I get Connection reset by peer error

If sending just first command, I get a proper response, so, no error in connection or command string

 

 

Here is my code:

function QuickApp:onInit()
    self:debug("onInit")
    self.ip = self:getVariable("ip")
    self.port = tonumber(self:getVariable("port"))
    self.sock = net.TCPSocket()
end
 
function QuickApp:parseData(str)
    while true do 
        if string.find(str, '0x'then
            i,j = string.find(str, '0x')
            str = string.sub(str, 1, i - 1) .. self:fromhex(string.sub(str, i + 2, j +2)) .. string.sub(str, j + 3)
        else
            return str
        end
    end
end
 
function QuickApp:fromhex(str)
    return (str:gsub('..'function(cc)
        return string.char(tonumber(cc, 16))
    end))
end
 
function QuickApp:connect(successCallback)
    print("connecting:", self.ip, self.port)
    self.sock:connect(self.ip, self.port, {
        success = function()
            self:debug("connected")
            successCallback()
        end,
        error = function(err)
            self.sock:close()
            self:debug("connection error connect", err)
        end,
    })
end 
 
function QuickApp:init(event)
    --self:connect()
    self:send("0x450x530x430x2F0x560x0500x2E0x6E0x650x740x100x030x000x000x000x00"true"0x500x570x520x3f0x0d")
end
 
function QuickApp:send(dataToSend, waitForResponse, pwr)   
        self:connect(function()
            local dataConverted = self:parseData(dataToSend)
            local pwrConverted = self:parseData(pwr)
            self.sock:write(dataConverted)
            fibaro.setTimeout(1000function() self.sock:write(pwrConverted) end)
            self.sock:read({
                success = function(data)
                    self:debug("response data:", data)
                    --self.sock:close()
                end,
                error = function(err)
                    self:debug("response error wait ", err)
                    --self.sock:close()
                end
            })
            fibaro.setTimeout(1000function() self.sock:write(pwrConverted) end)
            self.sock:read({
                success = function(data)
                    self:debug("response data:", data)
                    --self.sock:close()
                end,
                error = function(err)
                    self:debug("response error wait ", err)
                    --self.sock:close()
                end
            })
        end)
end

 

Looks like the connection is closed after frist call, but do not know how to keep alive, tried everything

 

Can anybody help me?

 

Regards

 

2 answers to this question

Recommended Posts

  • 0
Posted (edited)

I don't know what the response from the Epson is but something like this should work. (The code under --- QuickApp --- + fromhex/tohex )

A side note. Not having an Epson to test against I can run this in my emulator and simulate a TCPsocket device. Easy to debug... :-) 

Please login or register to see this code.

 

Edited by jgab
  • Thanks 1
  • 0
  • Inquirer
  • Posted

    Hi, looks like the connectin is remaining open, but now I haev bad file descriptor when trying to send the second stream. 

     

    The syntax of command are correct, tested in Hercules security

     

    here my code:

     

    function QuickApp:connect()
      self.sock:connect(self.ip,self.port,
        {success = 
          function()
            self:debug("connecting:", self.ip, self.port)
                self:send("45 53 43 2F 56 50 2E 6E 65 74 10 03 00 00 00 00 0d"true"0x500x570x520x3f"-- getting status of projector
                --self:send("0x450x530x430x2F0x560x0500x2E0x6E0x650x740x100x030x000x000x000x00", true, "5057523f0d") -- getting status of projector
          end,
          error = function(err) self:debug(err) end
        }
      )
      
    end 
     
    function QuickApp:send(dataToSend, waitForResponse, pwr)    
                local dataConverted = self:parseData(dataToSend)
                local pwrConverted = self:parseData(pwr)
                self.sock:write(dataConverted)
                --self.sock:write("ESC/VP.net/r")
                --fibaro.setTimeout(1000, function() self.sock:write(pwrConverted) end)
                --self.sock:readUntil("0x450x530x430x2F0x560x500x2E0x6E0x650x740x100x200x000x000x400x00",{
                self.sock:read({
                    success = function(data)
                        local function tohex(str)
                            local bs = {}
                            for i=1,str:len() do bs[#bs+1] = string.format('0x%02X',string.byte(str,i)) end
                            return table.concat(bs)
                        end
                        self:debug("response data:", tohex(data), "  ", data)
                        -- pwrConverted
                        self.sock:write("pwrConverted", {
                            success = function(data)
                                self:debug("1 ", data)
                            end,
                            error = function(err)
                                self:debug("2 ", err)
                            end
                        })
     
                        self:debug("should be ok")
     
                        self.sock:close()
                    end,
                    error = function(err)
                        self:debug("response error wait ", err)
                        self.sock:close()
                    end
                })
    end
     
    any ideeas?
    when debugging, get response from the first command, then "shoul be od", the "2 bad descriptor of file"

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...