How to make a turtle run a program over the Modem API in computercraft
I have an advanced computer with a wireless modem in my world as well as several Wireless turtles. Now I want to sent a program from that computer to the turtle and run it.
I looked up the modem API on the ComputerCraft website but I could not find a way to do it.
Do I need advanced turtles? Are there advanced turtles in FTB Mindcrack 1.4.7 (I don't know the version of ComputerCraft in the Mindcrack version of FTB)
How can I send programs to the turtles and run them from my advanced computer?
Let me begin by saying that the Rednet API in ComputerCraft is... fun. I imagine about as fun as any given netcoding could ever be, I suppose.
You can do one of two things. You can either transfer your program to the turtle with a floppy disk and send it a command to run that program from the computer, or, you can actually push an entire lua program to a computer, and then order it to execute that program. I will be covering both solutions in this answer.
You can transfer a program from a computer to a turtle using a floppy disk. To transfer a program between computers, you will need a disk drive and a blank floppy disk. Place the disk drive next to your advanced computer, and put your floppy disk in it. You can copy a file to the disk with copy <file> disk
. Now you have your program stored on the floppy disk. You can now use the floppy disk to put the program on the Turtle. Place your turtle down next to the disk drive. You can copy the file from the disk to the turtle with copy disk/<file> <file>
. Now the turtle has the program. Make sure you name that turtle with label set <name>
so that all programs will be stored when you break him.
Now you have a file called <file>
on your turtle. Let's set up a program on the turtle called server
, which will listen for a message, and then execute that message in the shell.
rednet.open("right")
while true do
senderID, message, distance = rednet.receive()
if message == "exit" then
break
end
shell.run(message)
end
rednet.close("right")
This program will listen for a message, execute it as a shell command, and then start listening again. So if you send it excavate 10
, it will start the excavate
program with argument 10
, or, if you send it <file>
, it will run your newly copied program. The corresponding client program could be something like this:
rednet.open(<side>)
while true do
input = read()
rednet.send(<ID>, input)
if input == "exit" then
break
end
end
rednet.close(<side>)
Where <ID>
is the ID of the receiving turtle. This will allow you to type commands on the advanced computer to be received by the turtle.
To push an entire lua program to a turtle is a little more complicated, but not much. You will need to use the fs
API to access the filesystem of the turtle and computer, in addition to the shell
API from earlier.
Start with a program called send
. This program's job is to read from a file and send that file as a string over rednet. You can accomplish this using the fs
API. Let's start by opening a file from the argument list and initializing everything like that:
args = {...}
infile = fs.open(args[1], "r")
rednet.open(<side>)
rednet.send(<ID>,args[1])
os.sleep(2)
filestring = infile.readAll()
rednet.send(<ID>,filestring)
rednet.close(<side>)
infile.close()
This will take an input file from an argument and send it over rednet as a string to the computer at <ID>
. The turtle will then have to write this string to a file and run that file.
rednet.open("right")
while true do
file = rednet.receive()
contents = rednet.receive()
outfile = fs.open(file, "w")
outfile.write(contents)
outfile.flush()
shell.run(file)
end
rednet.close("right")
This should receive a filename followed by a large string with the file contents, write that to the filename, and run that file.
So you can do this either way. You can either predefine commands on the turtle, or you can send an entire lua file as a message and have the turtle write that message to a local file before executing it. Either way works.