Advertisement
Guest User

hapi

a guest
Jul 15th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2.  
  3. if not modem then
  4.     error("No modems attached")
  5. end
  6.  
  7. function closeAll()
  8.     modem.closeAll()
  9. end
  10.  
  11. closeAll()
  12.  
  13. function open(channel)
  14.   if channel and channel >= 0 and channel <= 25565 then
  15.      modem.open(channel)
  16.     return true
  17.   end
  18.   return false
  19. end
  20.        
  21. function send(channel, message)
  22.   if not modem.isOpen(channel) then
  23.     open(channel)
  24.   end
  25.  
  26.   if channel and message then
  27.     modem.transmit(channel, channel, message)
  28.   end
  29. end
  30.  
  31. function receive(channel, timeout)
  32.   if not modem.isOpen(channel) then
  33.     open(channel)
  34.   end
  35.  
  36.   if timeout then
  37.     time = os.startTimer(timeout)
  38.   end
  39.  
  40.   while true do
  41.     event = {os.pullEvent()}
  42.     if event[1] == "modem_message" then
  43.       return event[5]
  44.     elseif event[2] == time then
  45.         return "Timeout"
  46.     end
  47.   end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement