Jump to content




Rednet Checking for incoming Messages (for a certain time only)


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

#1 Uch1ha_Sasuke

  • Members
  • 25 posts

Posted 02 December 2013 - 08:03 PM

I have two computers set up that are communicating with each other.
But I want the one computer to wait for the other after it sends a message to see if a message is sent back (basically checking if the other computer is on)
This is my Code : Computer 1
========================

local modem = peripheral.wrap("right")
text = io.read()
modem.transmit(35,30,text)

========================
--Ok so the modem sends a message to Computer 2
This is my Code : Computer 2
========================

local modem = peripheral.wrap("top")
modem.open(35)
local event,modemSide,senderChannel,replyChannel,message,senderDistance = os.pullEvent("modem_message")
modem.transmit(30,35,"On")

========================
--So after computer 2 receives a message it replies saying its on then Computer 1 must listen for 5 seconds then stop(so if computer 2 is offline it will not receive a message and the program on computer 1 will continue)
This is my Code(Happens right after Computer 1 transmits) : Computer 1
========================

modem.open(30)
local test,modemSide,senderChannel,replyChannel,message,senderDistance = os.pullEvent("modem_message")
if message == "On" then
print("Computer 2 is on")
end

========================

I need to use the computer 1 if there is no reply from Computer 2 . Currently it is waiting forever , is there some way I can make Computer 1 only wait 5 seconds after sending a message and if no message is received it will continue the program ?

Thanks in advance!

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 December 2013 - 08:29 PM

Yes definitely, you can start a timer and then when it completes move on.

#3 Uch1ha_Sasuke

  • Members
  • 25 posts

Posted 03 December 2013 - 04:02 AM

Oke well i still have no idea how do do this . I can make the timer but how do i implement it into my code?
The os.pullEvent is waiting for a reply infinitely so how can there be a timer running in the same line??

#4 Bomb Bloke

    Hobbyist Coder

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

Posted 03 December 2013 - 04:23 AM

The idea is to remove the filter from os.pullEvent() so it can return any event, then check what sort of event you get back.

Something like:

modem.open(30)
local myTimer = os.startTimer(5)

while true do  -- Start a loop that repeats indefinitely.
  local event,par1,par2,par3,par4,par5 = os.pullEvent()  -- Wait for any event.

  if event == "modem_message" then
    if par4:lower() == "on" then  -- Adding ":lower()" to a string returns a lowercase version. Handy for non-case-sensitive checks.
      print("Computer 2 is on")
    end
    break  -- We received a modem message, so stop the loop.
  elseif event == "timer" and par1 == myTimer then
    break  -- Our timer expired, so stop the loop.
  end
end


#5 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 03 December 2013 - 06:43 PM

You could also use the Rednet API, however, it is essentially an old version of the Modem API, and usage of it is generally frowned upon.

Here is how it works:
rednet.open("my modem side")
while true do
  local sender, message, distance = rednet.receive(--[[/* Timeout Value */]]-- 5)
  if message == "on" then
	print("Computer 2 is on") --# The editor screwed up the indentation. 
	break
  elseif message == nil then
	break
  end
end

(Rednet messages come in "rednet_message" events, and rednet.receive() simply starts the timer (if necessary) and does the os.pullEvent())

Edited by awsmazinggenius, 03 December 2013 - 06:43 PM.


#6 Bomb Bloke

    Hobbyist Coder

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

Posted 03 December 2013 - 08:26 PM

Rather, the rednet API is currently a wrapper for the modem API. It can handle the timer for you, but at the expense of not being able to handle anything else (like eg keypresses).

#7 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 03 December 2013 - 08:30 PM

View PostBomb Bloke, on 03 December 2013 - 08:26 PM, said:

Rather, the rednet API is currently a wrapper for the modem API. It can handle the timer for you, but at the expense of not being able to handle anything else (like eg keypresses).

That is what I meant, just in noob language :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users