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


  • 0

When to use "tcpSocket:disconnect();" ?


Guest samuelboerhoop
 Share

Question

Guest samuelboerhoop

When do I use the following code in lua:?

 

tcpSocket:disconnect();

 

i some sample code it has been added to the sample in others not.

 

example:

 

-- Volume 40dB --
tcpSocket:write("MV40".."\r")
fibaro:sleep(500);
result, err = tcpSocket:read();
tcpSocket:disconnect();
if (err == 0) then
  fibaro:log("transfer OK");
  fibaro:debug("transfer OK");
else
  fibaro:log("transfer failed");
  fibaro:debug("transfer failed");
end
 
------------------------------------
 
 
-- HERE its not been used --
 
tcpSocket:write(string.char(0x53, 0x49, 0x49, 0x52, 0x41, 0x44, 0x49, 0x4f, 0x0d));
fibaro:sleep(1000);
result, err = tcpSocket:read();
if (err == 0) then
  fibaro:log("Transfer OK: Internet Radio");
else
  fibaro:log("Transfer failed: extra check");
  -- check source: needs 2 times... otherwise still failed...
  tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
  fibaro:sleep(500);
  result, err = tcpSocket:read();
  tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
  fibaro:sleep(500);
  result, err = tcpSocket:read();
  if (err == 0) then
    if ( trim(string.sub(result, 1, string.len(result)-9)) == 'SIIRADIO' ) then
      fibaro:log("Transfer OK: Internet Radio");
    else
      fibaro:log("Transfer failed");
    end
  else
    fibaro:log("Transfer failed");
  end
end
 
 
 
 
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 1

Where can I find a reference manual/page about this TCPsocket? I've searched the web, but all I can find is examples of one type of application (to see if a device is present on the network or not). I would like to be able to see what all of its variables do. I noticed that the readTimeout does not work as expected; the socket takes ages (>20 seconds) if no device is available, i.e. write returns an error. I would like to shorten this time-out, but changing readTimeout doesn't do the trick.

Link to comment
Share on other sites

  • 1

Would be nice if the Fibaro adinistrator not only moves the subject, but also anwers the question about where to find documentaiton of TCPsocket

Link to comment
Share on other sites

  • 0

It is of course used to close the connection and dispose the socket. It is more of a good practice question. You should close things like that.

Link to comment
Share on other sites

  • 0
Guest samuelboerhoop
  • Inquirer
  • Ok If you look at the last example below where should i place the sentence: tcpSocket:disconnect(); ?

     

    After every "result, err = tcpSocket:read();" OR at the end of the last sentence of the code "result, err = tcpSocket:read();" ?

    Because in the example the "result, err = tcpSocket:read();" appears 3x times ?

     

    ----- Example ----

    tcpSocket:write(string.char(0x53, 0x49, 0x49, 0x52, 0x41, 0x44, 0x49, 0x4f, 0x0d));
    fibaro:sleep(1000);
    result, err = tcpSocket:read();
    if (err == 0) then
      fibaro:log("Transfer OK: Internet Radio");
    else
      fibaro:log("Transfer failed: extra check");
      -- check source: needs 2 times... otherwise still failed...
      tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
      fibaro:sleep(500);
      result, err = tcpSocket:read();
      tcpSocket:write(string.char(0x53, 0x49, 0x3f, 0x0d));
      fibaro:sleep(500);
      result, err = tcpSocket:read();
      if (err == 0) then
        if ( trim(string.sub(result, 1, string.len(result)-9)) == 'SIIRADIO' ) then
          fibaro:log("Transfer OK: Internet Radio");
        else
          fibaro:log("Transfer failed");
        end
      else
        fibaro:log("Transfer failed");
      end
    end
    Link to comment
    Share on other sites

    • 0

    The topic has been moved from "

    Please login or register to see this link.

    " to "

    Please login or register to see this link.

    ".

     

    Temat został przeniesiony z "

    Please login or register to see this link.

    " do "

    Please login or register to see this link.

    ".

    Link to comment
    Share on other sites

    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.

     Share

    ×
    ×
    • Create New...