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

Updating the turtle programs (wireless)

Started by lIlIlIIlll, 26 September 2012 - 12:52 AM
lIlIlIIlll #1
Posted 26 September 2012 - 02:52 AM
Here's what I have:
[computer]
rednet.open("left")
command= 'print("helloturtle")'
rednet.send(natet01,command)

[turtle]
rednet.open("right)
id, message = rednet.receive(60)
run = loadstring(message)
run()


my question is, how do I make it so that I can update the files on the turtle, I want to be able to make a program on the computer and then just do:

update (which will send the program to all the turtles I have hooked up)

Is there a way to do that?

-Nate
immibis #2
Posted 26 September 2012 - 02:56 AM
Instead of running the message, save it to a file?
lIlIlIIlll #3
Posted 26 September 2012 - 03:02 AM
Yes but how do I send the file to the turtle?

Would it just be:

rednet.send(<name>,<nate/<programname>) ?
Zoinky #4
Posted 26 September 2012 - 03:09 AM
You could use:

local file = fs.open("NAME OF FILE", "a") -- Make sure you change the name
file.writeLine(message)
file.close()
Not sure if that's what you're looking for.
lIlIlIIlll #5
Posted 26 September 2012 - 03:13 AM
Sort of, I just want to be able to send programs to my wireless turtles….
Zoinky #6
Posted 26 September 2012 - 03:29 AM
Well… I'm not very good at coding… but try this…
Spoiler

rednet.open("right")
textutils.slowPrint("Downloading...")
scrap, pName = rednet.receive(60)
scrap, nLine = rednet.receive(1)
if fs.exists(pName) == true then
print("Program name '"..pName.."' in use.")
print("Downlaod cancelled!")
error()
end
local file = fs.open(pName, "a")
local fileData = {}
for i = 0,nLine do
scrap, line = rednet.receive()
table.insert(fileData, line)
end
for t = 0,nLine do
file.writeLine(fileData[t])
end
file.close()
print("Download successful!")
rednet.close("right")
That will receive the code. (Run it on the turtle before you start sending the code).
And this will send the code: (Run it on the computer)
Spoiler

local tArgs = {...}
local pName = tostring(tArgs[1])
local sTime = os.time()
local delay = tonumber(tArgs[2])
if delay == nil then
delay = 0
end
if delay < 0 then
sleep(delay)
end
if #tArgs < 1 or #tArgs > 2 then
print("Usage:")
print("isend <program> <delay>")
print("")
error()
end
local program = tostring(tArgs[1])

rednet.open("back")
rednet.open("front")
rednet.open("top")
rednet.open("bottom")
rednet.open("right")
rednet.open("left")
local program = shell.resolveProgram(program)
print(program)
local file = fs.open(program, "r")
local fileData = {}
local nLine = 0
local nilLine = 0
repeat
table.insert(fileData, line)
line = file.readLine()
nLine = nLine + 1
if line == nil then
  nilLine = nilLine + 1
end
if line ~= nil then
  nilLine = 0
end
until nilLine == 2
file.close()
pName = tostring(pName)
nLine = tostring(nLine)
rednet.broadcast(pName)
rednet.broadcast(nLine)
nLine = tonumber(nLine)
for i = 0,nLine do
if fileData[i] == nil then
  rednet.broadcast(" ")
else
  rednet.broadcast(fileData[i])
end
end
local eTime = os.time()
tTime = eTime - sTime
print("Time elapsed: ".. tTime*50 .." seconds.")
rednet.close("top")
rednet.close("bottom")
rednet.close("left")
rednet.close("right")
rednet.close("front")
rednet.close("back")
lIlIlIIlll #7
Posted 26 September 2012 - 03:33 AM
So here is what I have so far on my computer:

local file = fs.open("test", "w")
rednet.open("left")

file.writeline(message)
file.close

How do I get the code of the file into the message?
Zoinky #8
Posted 26 September 2012 - 03:35 AM
Try what I posted. You'll need to run the listener (first program) before you try and send the program.
lIlIlIIlll #9
Posted 26 September 2012 - 03:37 AM
Try what I posted. You'll need to run the listener (first program) before you try and send the program.

Yes I understand that you have to run the listener program first, but in the code you posted the line:

file.writeline(message)

has no content…

how do I get the code of the program into the message?
lIlIlIIlll #10
Posted 26 September 2012 - 03:39 AM
Also is there an easy way to get the code into ComputerCraft? Other than just typing the entire thing up…
Zoinky #11
Posted 26 September 2012 - 03:43 AM
Yep. Lettuce made a nice tutorial. http://www.computerc...programs-in-cc/
EDIT: I just noticed this: http://www.computercraft.info/forums2/index.php?/topic/4419-rednet-files-over-rednet/. Makes my program useless :P/>/> Meh. I'm learning too.
lIlIlIIlll #12
Posted 26 September 2012 - 03:57 AM
I made it work, perhaps someone should post this to the wiki, I'm going to add arguments for filenames and monitor output now etc…

[turtle]

rednet.open("right")
id, message = rednet.receive(60)
file = fs.open("/turtle/program", "w")
file.write(message)
file.close()
print("Program Updated")


[computer]
rednet.open("left")
f = fs.open("nate/derp", "r")

message = f.readAll()

rednet.send(natet01,message)
ChunLing #13
Posted 12 October 2012 - 05:49 PM
A terrifyingly useful function. With this there is no limit to the degree of control a central computer can have over any number of turtles…well, I guess there is the limit of how many turtles can fit into the currently loaded minecraft world chunks, but since that number could easily be in the tens or hundreds of thousands that's still pretty terrifying.

Bah. "Pretty terrifying"? We'll just use world anchors, Bwahahahahahhahah!