Modem

From ComputerCraft
Jump to: navigation, search
EventEventmodem_message
Occurs every time a wireless message has been received by a wrapped (wireless) modem that had the message's channel open.
Returns
  • name : string The side or network name of the wrapped Modem that received the message
  • channel : number The channel the message has been send on
  • channel : number The desired reply channel the sender specified
  • message : any The message's content
  • distance? : number The distance from the sender to the receiver, in meters/blocks. This will not be set if the modems are in different dimensions.
Part of CC:Tweaked
ExampleExampleReceiving modem messages
Wraps a connected modem peripheral, opens channel 123 and listens for incoming messages.
Code
local modem = peripheral.find("modem")
modem.open(123)

while true do
  local event, peripheral_name, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  print("Received a message on channel", channel, "at", distance, "blocks away.")
  print("The message: ", message)
end
EventEventrednet_message
Triggered by the rednet coroutine and is created from the modem_message event. For most use cases, rednet.receive is usually easier to use.
Returns
  • id : number The supposed ID of the sender.
  • msg : any The message.
  • protocol? : string The specified protocol. Prior to ComputerCraft 1.6, was the distance between sender and reciever.
Part of CC:Tweaked
ExampleExamplePrint messages
Prints messages received through rednet. Requires that there is a modem open for rednet to use.
Code
while true do
  local event, senderId, message, protocol = os.pullEvent("rednet_message")
  print("Computer", senderId, "sent a message", message, "with the", protocol, "protocol!")
end