Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local m = component.modem -- get primary modem component
- local fs = require("filesystem")
- local PORT = 17
- local function readFile(path)
- if not fs.exists(path) then
- return false
- else
- local f_h = fs.open(path, "r")
- local rtn = f_h:read(fs.size(path))
- f_h:close()
- return rtn
- end
- end
- local function writeFile(path, data_string)
- local f_h = fs.open(path, "w")
- f_h:write(data_string)
- f_h:close()
- end
- local function getMessage()
- local _, _, from, port, _, message = event.pull("modem_message")
- return message
- end
- local function sendMessage(msg)
- m.broadcast(PORT, msg)
- end
- local function sendFile(fileName, fileRaw) -- Copy this function to programs that will communicate with this one. Also copy the getMessage and sendMessage functions.
- local file_length = fileRaw:len()
- local sent_amount = 0
- sendMessage("download:start")
- getMessage()
- sendMessage(fileName)
- getMessage()
- while sent_amount < file_length do
- local send_length = math.min(file_length, sent_amount + 500)
- local send_string = fileRaw:sub(sent_amount + 1, send_length)
- sent_amount = send_length
- sendMessage(send_string)
- print("Sending File (" .. tostring(file_length - sent_amount) .. " bytes remaining)")
- getMessage()
- end
- print("File sent!")
- sendMessage("download:end")
- end
- m.open(PORT)
- m.setStrength(64)
- sendFile("le_test.lua", "print('HUZZAH!')")
- sendMessage("execute")
- getMessage()
- sendMessage("le_test")
Add Comment
Please, Sign In to add comment