This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
If rednet doesn't receive signal?
Started by xXThunder607Xx, 15 February 2015 - 07:11 PMPosted 15 February 2015 - 08:11 PM
Hello. I have been working on a project in computercraft for quite some time now, and I have run into a problem. My program uses rednet to request and send info to other computers, and when one of those computers is not on or isn't working, my program keeps waiting for a message, and often times messes me up. I was wondering if there was a way to, after say a couple of seconds, it would print "Computer is offline" or "no signal" and reboot itself. Any help or ideas would be greatly appreciated. Thanks in advance.
Posted 15 February 2015 - 08:18 PM
Since you're using rednet, you can specify a timeout for rednet.receive() like so…
That would have rednet.receive() timeout after 3 seconds, then you could continue on with your code, taking into account that your receive timed out (your values will be nil) instead of receiving a response.
local id, message = rednet.receive(3)
That would have rednet.receive() timeout after 3 seconds, then you could continue on with your code, taking into account that your receive timed out (your values will be nil) instead of receiving a response.
Edited on 15 February 2015 - 08:20 PM
Posted 16 February 2015 - 12:13 AM
So I could do something like:
Correct?
rednet.open(side)
local id,msg = rednet.receive(3)
if msg == nil then
print("error")
else
-- do stuff
end
Correct?
Posted 16 February 2015 - 12:38 AM
That *should* work. If, for some reason, msg is not nil after the timeout (it should be, iirc), then you could use id instead…
if id == nil then
Edited on 15 February 2015 - 11:39 PM
Posted 16 February 2015 - 01:27 AM
I'd use "if type(message) ~= "string" then", or whatever type you're expecting.
Posted 16 February 2015 - 01:40 AM
Thanks guys. This helped alot.
Posted 11 April 2015 - 01:25 AM
local amount = 5 --wait time
local evnt = { rednet.receive( amount ) }
if evnt[ 1 ] ~= nil and evnt[ 2 ] ~= nil and evnt[ 3 ] ~= nil then
--code to run if message received.
else
print( "Print computer offline" )
end
Edited on 19 April 2015 - 04:31 PM
Posted 11 April 2015 - 04:41 PM
You forgot the {} around rednet.receive . And why wouldn't only checking one of the arguments work?
Edited on 11 April 2015 - 02:42 PM
Posted 19 April 2015 - 06:31 PM
You forgot the {} around rednet.receive . And why wouldn't only checking one of the arguments work?
Woops ur right I was in a hurry! XD