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

How to send arguments for a program via rednet?

Started by FortuneSyn, 12 November 2012 - 03:02 AM
FortuneSyn #1
Posted 12 November 2012 - 04:02 AM
Hello,

Let's say you have a turtle, and you want to use a computer to tell the turtle to run the program "Tunnel" with the argument "x" so that the turtle runs for example "Tunnel 32". How do I do this?
Sammich Lord #2
Posted 12 November 2012 - 04:32 AM
Well a few ways. First, you could just do this:
Sender:

local modemSide = "right" --Edit this for the side the modem is on.
local sendID = 0 --The ID to send the code to.
term.clear()
term.setCursorPos(1,1)
write("Script to run on receiving computer/turtle, separate arugments with a ','.")
input = read()
if input ~= "" then
  rednet.open(modemSide)
  rednet.send(sendID, input)
  rednet.close(modemSide)
else
  print("Example of use: go, forward")
end
Receiver:


function split(pString, pPattern) --I do not take credit for making this function. I found this on a website and this is what I use for most of my scripts. 
   local Table = {}  
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(Table,cap)
      end
      last_end = e+1
      s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
      cap = pString:sub(last_end)
      table.insert(Table, cap)
  end
   return Table
end
local modemSide = "right"
local receiveID = 1
while true do
  id, msg = rednet.receive()
  if id == receiveID then
	local runs = split(msg, ",")
    local runCode = "pcall("
    for k,v in pairs(runs) do
      runCode = runCode..runs[k]
    end
    runCode = runCode..")"
    local run = loadstring(runCode)
    success, error = run()
    if not success then
      print(error)
    end
  end
end
We use pcall so that it does not stop the code if it encounters an error. Instead pcall catches the error and returns it. That is the basics of how to do it.
Note: This is not tested but it should work.
FortuneSyn #3
Posted 12 November 2012 - 05:37 AM
The code you posted is not working. I tried using it, here is how I used it:

sender

local modemSide = "right" --Edit this for the side the modem is on.
local sendID = 3 --The ID to send the code to.
term.clear()
term.setCursorPos(1,1)
write("Script to run on receiving computer/turtle, separate arugments with a ','.")
input = read()
if input ~= "" then
  rednet.open(modemSide)
  rednet.send(sendID, input)
  rednet.close(modemSide)
else
  print("Example of use: go, forward")
end

receiver

function split(pString, pPattern) --I do not take credit for making this function. I found this on a website and this is what I use for most of my scripts.
   local Table = {}
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
	  if s ~= 1 or cap ~= "" then
		 table.insert(Table,cap)
	  end
	  last_end = e+1
	  s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
	  cap = pString:sub(last_end)
	  table.insert(Table, cap)
  end
   return Table
end
local modemSide = "right"
local receiveID = 4
while true do
  id, msg = rednet.receive()
  if id == receiveID then
		local runs = split(msg, ",")
	local runCode = "pcall("
	for k,v in pairs(runs) do
	  runCode = runCode..runs[k]
	end
	runCode = runCode..")"
	local run = loadstring(runCode)
	success, error = run()
	if not success then
	  print(error)
	end
  end
end

I ran program on receiver turtle. When running on sender, i typed "Dig2.1, 2" meaning dig2.1 program, argument 2. There was no error received on either computer or turtle.

I don't understand at all how the split function on the receiver is working or what its doing, nor the runs/runcode after that. It doesn't have to be fancy. All i want is to be able to define the argument to use on a specific program via rednet.
Sammich Lord #4
Posted 13 November 2012 - 06:15 AM
Spoiler
The code you posted is not working. I tried using it, here is how I used it:

sender

local modemSide = "right" --Edit this for the side the modem is on.
local sendID = 3 --The ID to send the code to.
term.clear()
term.setCursorPos(1,1)
write("Script to run on receiving computer/turtle, separate arugments with a ','.")
input = read()
if input ~= "" then
  rednet.open(modemSide)
  rednet.send(sendID, input)
  rednet.close(modemSide)
else
  print("Example of use: go, forward")
end

receiver

function split(pString, pPattern) --I do not take credit for making this function. I found this on a website and this is what I use for most of my scripts.
   local Table = {}
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
	  if s ~= 1 or cap ~= "" then
		 table.insert(Table,cap)
	  end
	  last_end = e+1
	  s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
	  cap = pString:sub(last_end)
	  table.insert(Table, cap)
  end
   return Table
end
local modemSide = "right"
local receiveID = 4
while true do
  id, msg = rednet.receive()
  if id == receiveID then
		local runs = split(msg, ",")
	local runCode = "pcall("
	for k,v in pairs(runs) do
	  runCode = runCode..runs[k]
	end
	runCode = runCode..")"
	local run = loadstring(runCode)
	success, error = run()
	if not success then
	  print(error)
	end
  end
end

I ran program on receiver turtle. When running on sender, i typed "Dig2.1, 2" meaning dig2.1 program, argument 2. There was no error received on either computer or turtle.

I don't understand at all how the split function on the receiver is working or what its doing, nor the runs/runcode after that. It doesn't have to be fancy. All i want is to be able to define the argument to use on a specific program via rednet.
You can simply just have some "if" statements on the receiving computer like this:
Sender:

local modemSide = "ride"
local sendID = 0
local toPrint = [[
A. Dig2.1 With the arg(s) "2"
B. Dig2.1 With the arg(s) "1"]]
term.clear()
term.setCursorPos(1,1)
print(toPrint)
while true do
  event, p1 = os.pullEvent("char") --Only pulls the "char" event
  if p1 == "a" then
    rednet.open(modemSIde)
    rednet.send(sendID, "shell.run('Dig2.1, '2'')")
    rednet.close(modemSide)
  elseif p1 == "b"
    rednet.open(modemSide)
    rednet.send(sendID, "shell.run('Dig2.1', '1')")
  end
end
Receiver:

local modemSide = "right"
local receiveID = 1
while true do
  id, msg = rednet.receive()
  if id == receiveID then
	 local hello = loadstring(msg) --Makes it a function
	 hello()
  end
end
Note: If the string has an error it will stop the receiving program.
This is very simple and is just a proof of concept.