Jump to content




If rednet doesn't receive signal?

networking computer lua

  • You cannot reply to this topic
8 replies to this topic

#1 xXThunder607Xx

  • Members
  • 20 posts

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.

#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

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 by Dog, 15 February 2015 - 09:20 PM.


#3 xXThunder607Xx

  • Members
  • 20 posts

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?

#4 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

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 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 16 February 2015 - 01:27 AM

I'd use "if type(message) ~= "string" then", or whatever type you're expecting.

#6 xXThunder607Xx

  • Members
  • 20 posts

Posted 16 February 2015 - 01:40 AM

Thanks guys. This helped alot.

#7 GreenGene

  • Members
  • 78 posts
  • LocationMagic land, awesomeness

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 flaghacker

  • Members
  • 655 posts

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.


#9 GreenGene

  • Members
  • 78 posts
  • LocationMagic land, awesomeness

Posted 19 April 2015 - 05:31 PM

View Postflaghacker, on 11 April 2015 - 03:41 PM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users