This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
javierbs's profile picture

Using rednet to copy programs

Started by javierbs, 04 March 2012 - 11:18 PM
javierbs #1
Posted 05 March 2012 - 12:18 AM
Hi I am new to lua, I was wondering if there is a way to copy a program I wrote in a computer and send the copy it over the rednet to another/ a turtle. I was kind of looking into it but I dont really understand very well what I have found. Can Anyone help me?
MysticT #2
Posted 05 March 2012 - 12:26 AM
To send a file over rednet:

function SendFile(sFileName, id)
    local file = io.open(sFileName, "r")
    if file then
	    local s = file:read("*a")
        rednet.send(s, id)
        file:close()
    end
end
javierbs #3
Posted 05 March 2012 - 01:04 AM
OK thanks, but I am new to lua (as I said) so I dont understand quite well your program nor how to use it. Can you be more noob friendly? Thanks! :unsure:/>/>
MysticT #4
Posted 05 March 2012 - 01:27 AM
Sender code (save it as sendfile):

local tArgs = { ... }
if #tArgs ~= 2 then
    print("Usage: sendfile <filename> <id>")
    return
end
local file = io.open(tArgs[1], "r")
if file then
    local s = file:read("*a")
    local id = tonumber(tArgs[2])
    if id then
        rednet.send(id, s)
    else
	    print("ID should be number")
    end
else
    print("No such file "..tArgs[1])
end
Recipient code (save it as getfile):

local tArgs = { ... }
local timer
if #tArgs < 1 or #tArgs > 2 then
    print("Usage: getfile <filename> <timeout>")
    return
end
if #tArgs == 2 then
    local timeout = tonumber(tArgs[1])
    if timeout then
	    timer = os.startTimer(timeout)
    end
end

while true do
    local evt, param, msg = os.pullEvent()
    if evt == "rednet_message" then
	    local file = io.open(tArgs[1], "w")
	    if file then
            file:write(msg)
            file:close()
	    else
		    print("Error opening file "..tArgs[1])
	    end
        break
    elseif evt == "timer" and param == timer then
            break
    end
end
Run the getfile program on the computer/turtle you want to get the file, then run the sendfile program on the computer with the file.
If you don't understand something just tell me :unsure:/>/>
javierbs #5
Posted 05 March 2012 - 03:59 AM
Ok I must really suck at this haha. When I try to use your sendfile program with a test program I did (called "test" it just prints ("success")) I dont seem to get it sent.

I use rednet.open on both computer, I use getfile program as "getfile test 30" (also tried "getfile <test> <30>") and then use "sendfile test 34"(the id) but I get no results and if I use the sendfile program as "sendfile <test> <34>" it says "No such file <test>" Any help?
MysticT #6
Posted 05 March 2012 - 05:17 PM
Sorry, I always forget the open call :unsure:/>/>
Try with this:
sendfile:

local tArgs = { ... }
if #tArgs ~= 2 then
	print("Usage: sendfile <filename> <id>")
	return
end

local function OpenModem()
	for _, s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false, "No modem found."
end

local ok, err = OpenModem()
if not ok then
	print(err)
	return
end

local file = io.open(tArgs[1], "r")
if file then
	local s = file:read("*a")
	local id = tonumber(tArgs[2])
	if id then
		rednet.send(id, s)
   	 print("File ", tArgs[1], " sent to ", id)
	else
		print("ID should be number")
	end
else
	print("No such file "..tArgs[1])
end

getfile:

local tArgs = { ... }
local timer
if #tArgs < 1 or #tArgs > 2 then
	print("Usage: getfile <filename> <timeout>")
	return
end
if #tArgs == 2 then
	local timeout = tonumber(tArgs[1])
	if timeout then
		timer = os.startTimer(timeout)
	end
end

local function OpenModem()
	for _, s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false, "No modem found."
end

local ok, err = OpenModem()
if not ok then
	print(err)
	return
end

while true do
	local evt, param, msg = os.pullEvent()
	if evt == "rednet_message" then
		local file = io.open(tArgs[1], "w")
		if file then
			file:write(msg)
			file:close()
	   	 print("File received and saved as ", tArgs[1])
		else
			print("Error opening file "..tArgs[1])
		end
		break
	elseif evt == "timer" and param == timer then
		break
	end
end
I tested it, it works with modems (autodetects on wich side the modem is), if you need to use it with bundled cables tell me and i'll modify it.