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!
Rednet Checking for incoming Messages (for a certain time only)
Started by Uch1ha_Sasuke, Dec 02 2013 08:03 PM
6 replies to this topic
#1
Posted 02 December 2013 - 08:03 PM
#3
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??
The os.pullEvent is waiting for a reply infinitely so how can there be a timer running in the same line??
#4
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:
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
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 messages come in "rednet_message" events, and rednet.receive() simply starts the timer (if necessary) and does the os.pullEvent())
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
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
Posted 03 December 2013 - 08:30 PM
Bomb 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











