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

[LUA] problem transfering program by rednet

Started by gui2one, 10 January 2013 - 02:27 PM
gui2one #1
Posted 10 January 2013 - 03:27 PM
Hi everyone, I need help for a project of mine.

Let's say I have a file on "mainComputer". I want to copy this file to a remote turtle, via rednet



I have two programs : one on the mainComputer, and one on turtle1.

the program on mainComputer :

local targs = {...}

--open file in read mode
file = io.open(targs[1],"r")
-- read all the file and format the content. ( I tried without 'string.format() ...'   doesn't work either )
content = string.format('%q',file:read('*a'))

print("content ==== "..content)

cmd = ('copyFile '..targs[1]..' '..content)

--send the command as a rednet message to the turtle
rednet.send(48,cmd)

I fire it by typing : copyFileMaster "test1" – "test1" is a file present on my mainComputer

the program on the turtle :


local targs = {...}


cmd = ('copy "startup" '..targs[1])  -- this command works fine it copies startup with a new name ( I didn't know how to create a new file otherwise ....)

print (cmd)
shell.run(cmd)


file = io.open(targs[1],"w")

file:write(targs[2]) -- it's the part that does not work at all .... targs[2]
file:close()


Almost everything works fine : the shell command to copy the file works fine. I can open it and start writing in it.
The probelm, I think is the formating of the content of the file I want to copy.
In "test1", there are only two simple lines for now :


print  ("hello")
print  ("world")

on the copied version of the file the only content is : "print" , which, I guess, is just the first word of the content I want to transfer …..
I tried different ways to format it.
I tried with Fs API, IO API.

Don't know what to try anymore.

I hope I am being clear.
theoriginalbit #2
Posted 10 January 2013 - 03:37 PM
try this



-- server/computer
local tArgs = { ... }

local file = io.open( tArgs[1], "r" )
local contents = file:read( "*a" )
file:close()


rednet.open( "right" )

rednet.send( 48, contents )

rednet.close( "right" )


-- turtle
local tArgs = { ... }

rednet.open( "right" )

fs.move( "/startup", "/oldstartup" )

local id, msg = rednet.receive()

local file = io.open( tArgs[1], "w" )
file:write( msg )
file:close()

rednet.close( "right" )
gui2one #3
Posted 10 January 2013 - 04:23 PM
Hi, thank you it almost works like I need it to :)/>

but for it to work, I have to launch the program on the turtle manually.
I modified it a little, but it's the one you gave me.


local tArgs = { ... }

rednet.open( "right" )

fs.copy( "startup", tArgs[1] )

while true do
	local id, msg = rednet.receive()
	 if id == 39 then
		  local file = io.open( tArgs[1], "w" )
		  file:write( msg )
		  file:close()

		  rednet.close( "right" )
	 end
end


In the end, I would like to be able to transfer a program to a tutrle that is stuck on bed rock, or under lava for instance ^^
That's the reason why I was trying to call the program via a shell command in my first attempt.

Thank you very much though !! at least you got the good "content" written to the file.
theoriginalbit #4
Posted 10 January 2013 - 04:37 PM
Hi, thank you it almost works like I need it to :)/>

but for it to work, I have to launch the program on the turtle manually.
I modified it a little, but it's the one you gave me.

I just modified your one to work how you were trying to do it, without knowing your full use case I couldn't have got it 100% how you wanted it. ;)/>


Thank you very much though !! at least you got the good "content" written to the file.

Your welcome. :)/>
D3matt #5
Posted 10 January 2013 - 09:01 PM
Hi, thank you it almost works like I need it to :)/>

but for it to work, I have to launch the program on the turtle manually.
I modified it a little, but it's the one you gave me.


local tArgs = { ... }

rednet.open( "right" )

fs.copy( "startup", tArgs[1] )

while true do
	local id, msg = rednet.receive()
	 if id == 39 then
		  local file = io.open( tArgs[1], "w" )
		  file:write( msg )
		  file:close()

		  rednet.close( "right" )
	 end
end


In the end, I would like to be able to transfer a program to a tutrle that is stuck on bed rock, or under lava for instance ^^
That's the reason why I was trying to call the program via a shell command in my first attempt.

Thank you very much though !! at least you got the good "content" written to the file.
Simply shell.run() the program you copied. Problem solved?
gui2one #6
Posted 11 January 2013 - 03:30 AM
Simply shell.run() the program you copied. Problem solved?

Frankly, I don't know anything anymore … :blink:/>
Maybe I went a little over my head with this project. I can feel my lack of programming skills right there ^^
I am sure I'm not very far from success, but I need to understand better what I am trying to do.

Be sure I will be back !! ^^

thank you guys
ChunLing #7
Posted 11 January 2013 - 05:28 AM
If TheOriginalBIT's code is working for you, you only need to put "shell.run(tArgs[1])" at the end of the script, and it will attempt to run the program it just downloaded. This only works if the program doesn't take extra command line parameters, of course.