Jump to content




Help With Simple Rednet Communication


2 replies to this topic

#1 Himself12794

  • Members
  • 56 posts

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 by Himself12794, 08 November 2013 - 01:03 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#3 Himself12794

  • Members
  • 56 posts

Posted 08 November 2013 - 12:30 PM

Posted Image
It's always something simple. I cannot believe that I missed that... Thanks for the help!

Edited by Himself12794, 08 November 2013 - 12:32 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users