This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xXThunder607Xx's profile picture

If rednet doesn't receive signal?

Started by xXThunder607Xx, 15 February 2015 - 07:11 PM
xXThunder607Xx #1
Posted 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.
Dog #2
Posted 15 February 2015 - 08:18 PM
Since you're using rednet, you can specify a timeout for rednet.receive() like so…

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
xXThunder607Xx #3
Posted 16 February 2015 - 12:13 AM
So I could do something like:


rednet.open(side)
local id,msg = rednet.receive(3)
  if msg == nil then
print("error")
else
   -- do stuff
end

Correct?
Dog #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 on 15 February 2015 - 11:39 PM
Bomb Bloke #5
Posted 16 February 2015 - 01:27 AM
I'd use "if type(message) ~= "string" then", or whatever type you're expecting.
xXThunder607Xx #6
Posted 16 February 2015 - 01:40 AM
Thanks guys. This helped alot.
GreenGene #7
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
flaghacker #8
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
GreenGene #9
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