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

Help With Simple Rednet Communication

Started by Himself12794, 08 November 2013 - 11:08 AM
Himself12794 #1
Posted 08 November 2013 - 12:08 PM
I'm trying to write a program that will let me set redstone state remotely using a computer, which will also save the state to a file to be reused when the world reloads. It seems simple, but it just is not working. I have one computer that sends the request, and the one which acts on it.
Here is the code I have for the receiving computer:

rednet.open('top')
main=311
side='bottom'
function printState()
--term.clear()
--term.setCursorPos(0,0)
print('')
print('Boreing 2.0')
if getState() then
  print('Batteries: active')
else
  print('Batteries: inactive')
end
end
function createFile()
if not fs.exists('config') then
  f=fs.open('config','w')
  f.writeLine('false')
  f.close()
end
end
function getState()
f=fs.open('config', 'r')
local state=f.readLine()
f.close()
return state
end
function saveState(state)
f=fs.open('config', 'w')
f.writeLine(state)
f.close()
end
function setOutput(state, side)
if getState() then
   rs.setOutput(side, false)
   printState()
elseif not getState() then
   rs.setOutput(side, true)
   printState()
end
end
function command()
--setOutput(not getState(), side)
rs.setOutput(side, not getState())
while true do
  a, id, msg, dist=os.pullEvent('rednet_message')
  if id==main then
   write(id..' says ')
   print(msg)
   saveState(not getState())
   rednet.send(main, state, true)
  end
end
end
printState()
createFile()
command()
And this is the code for the sending computer:

args={...}
rednet.open('top')
print(args[1])
rednet.send(309, args[1], true)
a, id, msg, d=os.pullEvent('rednet_message')
print(msg)

rednet.close('top')
Whenever I have the acting computer running, no matter what the config file says the state is, it is always active.
And when the sending computer sends, the receiving gets the correct msg and prints it, but instead of printing what the receiving computer told it to, the msg is just blank. Any suggestions?
Edited on 08 November 2013 - 12:03 PM
Lyqyd #2
Posted 08 November 2013 - 12:25 PM
Look! It's stringly typed!

Change the return statement of getState to `return state == "true"` to convert the string to a boolean.
Himself12794 #3
Posted 08 November 2013 - 12:30 PM

It's always something simple. I cannot believe that I missed that… Thanks for the help!
Edited on 08 November 2013 - 11:32 AM