Advertisement
VanillaBean

Modem event test code

Mar 14th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. -- computer (server)
  2. local component = require("component")
  3. local modem = component.modem
  4. local eventName = "modem"
  5. local event = require("event")
  6. local port = 9999
  7.  
  8. function sendEvent(msg)
  9.     print("sendEvent")
  10.     modem.broadcast(port, msg)
  11. end
  12.  
  13. function waitForEvent()
  14.     print("waitForEvent")
  15.     local _, _, _, _, _, msg = event.pull(eventName)
  16.    
  17.     return msg
  18. end
  19.  
  20. local i = 1
  21. while true do
  22.    
  23.     msg = "sending event " .. i
  24.     sendEvent(msg)
  25.    
  26.     print(msg)
  27.     i = i +1
  28.     os.sleep(5)
  29. end
  30. -- end computer (server)
  31.  
  32.  
  33.  
  34. -- robot (client)
  35. local component = require("component")
  36. local modem = component.modem
  37. local eventName = "modem"
  38. local event = require("event")
  39. local port = 9999
  40.  
  41. function sendEvent(msg)
  42.     print("sendEvent")
  43.     modem.broadcast(port, msg)
  44. end
  45.  
  46. function waitForEvent()
  47.     print("waitForEvent")
  48.     local _, _, _, _, _, msg = event.pull(eventName)
  49.    
  50.     return msg
  51. end
  52.  
  53. print('listening for event...')
  54. while true do
  55.  
  56.     result = waitForEvent()
  57.    
  58.     if  (result) then
  59.         print(result)
  60.     else
  61.         print('no result')
  62.     end    
  63. end
  64. --end robot (client)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement