Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local channel = 0
- local modem
- function setModem(side)
- modem = peripheral.wrap(side)
- end
- function setChannel(ch)
- channel = ch
- end
- function openModem(ch)
- ch = ch or channel
- if not modem.isOpen(ch) then
- modem.open(ch)
- print("Opened modem: " .. ch)
- else
- print("Modem already opened: " .. ch)
- end
- end
- function closeModem(ch)
- ch = ch or channel
- if modem.isOpen(ch) then
- modem.close(ch)
- print("Closed modem: " .. ch)
- else
- print("Modem already closed: " .. ch)
- end
- end
- function modemSetup(side, ch)
- modem = peripheral.wrap(side)
- setChannel(ch)
- openModem()
- end
- function sendMessage(msg, ch, returnCh)
- ch = ch or channel
- returnCh = returnCh or channel
- if type(msg) == "table" then
- msg = textutils.serialize(msg)
- end
- modem.transmit(ch, returnCh, msg)
- end
- function waitForMessage(ch, timeout)
- ch = ch or channel
- local myTimer
- if timeout then
- myTimer = os.startTimer(timeout)
- end
- while true do
- local event, par1, senderChannel,
- replyChannel, message, senderDistance = os.pullEvent()
- if event == "timer" and par1 == myTimer then
- print("message timeout")
- return false
- end
- if senderChannel == ch then
- return true, message, replyChannel
- end
- end
- end
- function sendAndWaitForResponse(msg, ch, returnCh, timeout)
- sendMessage(msg, ch, returnCh)
- local worked, message, replyChannel = waitForMessage(returnCh, timeout)
- return worked, message, replyChannel
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement