As the title says... Chat on...

User avatar
By Der Artz
#10752 So I am writing a simple TCP client that will connect to some LED light bulbs that I have and send a command to them. I have multiple of these light bulbs so I need to open and close connections between multiple IP addresses. I wrote the following to handle setting up the connection and sending the command. The problem is I can't find a way to wait for the command to be sent so that I can call the next function.

Code: Select allsendingCommand = 0

function sendCommand(ipAddress,port,command)
    sendingCommand = 1
    conn=net.createConnection(net.TCP, 0)
    conn:connect(port,ipAddress)
    conn:send(command)
    conn:on("sent", function(c) c:close() print("Command Sent") sendingCommand = 0 end)
    conn:on("disconnection", function(c) print("Connection Closed") end)
    while sendingCommand == 1 do
        tmr.wdclr()
    end
    print("Finished")
end

sendCommand("xxx.xxx.xxx.xxx",x,"Hello")


When I call this command I never break out of the while loop because the callback for sent is never fired. I am new to LUA and unsure of it's interrupt priorities with these callbacks.
User avatar
By picstart
#10797 The issue is TCP is asynchronous......the time between a send and a call back to "sent" is a function of network traffic.
If I have it right Lua when it interprets send doesn't block until "sent" is called.
User avatar
By Der Artz
#10836 Yes I understand that part of the asynchronous nature but I still shouldn't exit the function until sent is called and the flag reset. I just want to know when it is safe to send a new command.
User avatar
By hydroboy
#10844 Your wait loop is blocking any socket events from firing and as far as I know there is no 'do events' equivalent available.

You could sequence your actions within a timer function that is periodically being called based on the state of global variables. That would allow time for the events to trigger and you don't need to clear the watchdog.