Jump to content




rednet.receive

lua wireless

1 reply to this topic

#1 Razputin

  • Members
  • 21 posts

Posted 27 January 2013 - 01:08 PM

What code do I need for a computer to receive a message from another computer? I've tried several things but they all return errors.

#2 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 27 January 2013 - 02:44 PM

Computer 1 (the sender):
-- Open the rednet modem. This assumes you have the modem on the top of the computer. You can change it to left, right, bottom, etc...
rednet.open("top")

-- Broadcast a message to all computers
print("Sending Message")
rednet.broadcast("Hello, World!")

-- Wait for a response from another computer
print("Waiting for response...")
id, message = rednet.receive()

print("We received a response from comptuer ID: " .. id)
print("With the message: " .. message)

Computer 2 (The responder):
-- Open the modem. Same as the first program
rednet.open("top")

-- Wait for a message
print("Waiting for a message")
id, message = rednet.receive()

print("Message received from comptuer ID: " .. id)
print("Message says: " .. message)

-- Send a response to the computer that just sent us a message
print("Sending response")
rednet.send(id, "Hello there! Received your message!")

If you place a new computer, copy the second script to it, and run that script, it will start waiting for rednet messages to be sent to it. Then place computer 1 and run the first program. You should see that it broadcasts a message, then receives a response from the second computer. The second computer will show that it received a message, and then sent a response.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users