Necrotico

OC Remote Control Test

Sep 14th, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1.  
  2. local component = require("component")
  3. local event = require("event")
  4. local m = component.modem -- get primary modem component
  5. local fs = require("filesystem")
  6.  
  7. local PORT = 17
  8.  
  9. local function readFile(path)
  10.     if not fs.exists(path) then
  11.         return false
  12.     else
  13.         local f_h = fs.open(path, "r")
  14.         local rtn = f_h:read(fs.size(path))
  15.         f_h:close()
  16.         return rtn
  17.     end
  18. end
  19. local function writeFile(path, data_string)
  20.     local f_h = fs.open(path, "w")
  21.     f_h:write(data_string)
  22.     f_h:close()
  23. end
  24.  
  25. local function getMessage()
  26.     local _, _, from, port, _, message = event.pull("modem_message")
  27.     return message
  28. end
  29. local function sendMessage(msg)
  30.     m.broadcast(PORT, msg)
  31. end
  32.  
  33. local function sendFile(fileName, fileRaw) -- Copy this function to programs that will communicate with this one. Also copy the getMessage and sendMessage functions.
  34.     local file_length = fileRaw:len()
  35.     local sent_amount = 0
  36.  
  37.     sendMessage("download:start")
  38.     getMessage()
  39.     sendMessage(fileName)
  40.     getMessage()
  41.     while sent_amount < file_length do
  42.         local send_length = math.min(file_length, sent_amount + 500)
  43.         local send_string = fileRaw:sub(sent_amount + 1, send_length)
  44.         sent_amount = send_length
  45.         sendMessage(send_string)
  46.         print("Sending File (" .. tostring(file_length - sent_amount) .. " bytes remaining)")
  47.         getMessage()
  48.     end
  49.     print("File sent!")
  50.     sendMessage("download:end")
  51. end
  52.  
  53. m.open(PORT)
  54. m.setStrength(64)
  55.  
  56. sendFile("le_test.lua", "print('HUZZAH!')")
  57.  
  58. sendMessage("execute")
  59. getMessage()
  60. sendMessage("le_test")
  61.  
Add Comment
Please, Sign In to add comment