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.
If rednet doesn't receive signal?
Started by xXThunder607Xx, Feb 15 2015 08:11 PM
networking computer lua
8 replies to this topic
#1
Posted 15 February 2015 - 08:11 PM
#2
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 by Dog, 15 February 2015 - 09:20 PM.
#3
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?
#4
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 by Dog, 16 February 2015 - 12:39 AM.
#5
Posted 16 February 2015 - 01:27 AM
I'd use "if type(message) ~= "string" then", or whatever type you're expecting.
#6
Posted 16 February 2015 - 01:40 AM
Thanks guys. This helped alot.
#7
Posted 11 April 2015 - 12: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 by GreenGene, 19 April 2015 - 05:31 PM.
#8
Posted 11 April 2015 - 03:41 PM
You forgot the {} around rednet.receive . And why wouldn't only checking one of the arguments work?
Edited by flaghacker, 11 April 2015 - 03:42 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












