Jump to content
  • 0

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


Guest samuelboerhoop

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 post
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 post
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 post
Share on other sites
  • 0
Guest samuelboerhoop

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 post
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.

×
×
  • Create New...