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

program file servers?

Started by whiteanon, 23 July 2012 - 02:13 PM
whiteanon #1
Posted 23 July 2012 - 04:13 PM
ive been attempting to have a main computer send a program to a wireless mining turtle to execute. is there any way to do this? ive tried a few of the ideas on the forum and none have worked, id like to see a simple example of what you would do to make this work. thanks.
inventor2514 #2
Posted 23 July 2012 - 05:15 PM
You could have the main computer load the contents of a program file and send it to the turtle. Then, use loadstring to load it as a function and call the function. Below is an example server and client.

Server
local sSide = "right"
-- load the program
local file = fs.open("program", "r")
if not file then
print("Error opening file")
return
end
local sFile = file.readAll()
file.close()
-- main loop
rednet.open(sSide)
local bRun = true
while bRun do
local sEvent, ret1, ret2, ret3 = os.pullEvent()
if sEvent=="key" and ret1==28 then -- stop with enter/return key
  bRun = false
elseif sEvent=="rednet_message" then
  if ret2=="download_program" then
   rednet.send(ret1, sFile)
  end
end
end
rednet.close(sSide)

Client

local sSide = "right"
local serverID = 0
-- contact server
rednet.open(sSide)
rednet.send(serverID, "download_program")
local id, sFile = rednet.receive(3)
if not id then
print("No responce from server")
return
end
-- save and run program
local file = fs.open("program", "w")
file.write(sFile)
file.close()
shell.run("program")
whiteanon #3
Posted 23 July 2012 - 06:25 PM
when i try that it says "bios:267: attempt to write to global" the file isnt anywhere clientside nor does it execute after saying that.
inventor2514 #4
Posted 23 July 2012 - 07:19 PM
Sorry about that; it seems that loadstring doesn't work if a program defines global variables. I fixed the client code to save and execute the program.