Jump to content




[Lua][Rednet][Programming]Reading message to send via Rednet?


  • You cannot reply to this topic
11 replies to this topic

#1 EmTeaKay

  • Members
  • 115 posts

Posted 20 June 2012 - 11:12 PM

How do I read a message and send it to another computer via rednet?

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 20 June 2012 - 11:14 PM

To receive a message:
local id, msg = rednet.receive()
to send:
rednet.send(theID, "The Message")
The id must be a number, and the message a string.

#3 EmTeaKay

  • Members
  • 115 posts

Posted 20 June 2012 - 11:15 PM

Thanks, but I mean if I want to type the message out of lua. That, to my knowledge, can only be used in lua.

#4 Zalerinian

  • New Members
  • 68 posts
  • LocationThe Internet

Posted 20 June 2012 - 11:20 PM

View PostEmTeaKay, on 20 June 2012 - 11:15 PM, said:

Thanks, but I mean if I want to type the message out of lua. That, to my knowledge, can only be used in lua.

Please explain this better.

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 20 June 2012 - 11:20 PM

You have to make a program to do it, there isn't a default program to send/receive messages.
Try with this one:
local tArgs = { ... }
local id = tonumber(tArgs[1])
local msg = tArgs[2]

if not id or not msg then
  print("Usage: send <id> <message>")
  return
end

for _,s in ipairs(rs.getSides()) do
  rednet.open(s)
end

rednet.send(id, msg)

And to receive:
for _,s in ipairs(rs.getSides()) do
  rednet.open(s)
end

local id, msg = rednet.receive()
print(id, ": ", msg)


#6 EmTeaKay

  • Members
  • 115 posts

Posted 20 June 2012 - 11:23 PM

Wow! Thanks for that, helped a lot. But this is too complicated for me. But thanks for the help.

while true do
rednet.open("right")
io.read = msg
rednet.broadcast(msg)
end
Will a variation of that work?

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 20 June 2012 - 11:24 PM

Almost, should be:
rednet.open("right")
while true do
  local msg = read()
  rednet.broadcast(msg)
end


#8 EmTeaKay

  • Members
  • 115 posts

Posted 20 June 2012 - 11:30 PM

Perfect! This works amazingly.

Now, how do I send a command through a rednet message? I.e. a reboot message.

#9 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 20 June 2012 - 11:36 PM

If you want the receiver to execute a command, it has to be waiting for a message, and then execute the command. There's different ways to do it, you can use keywords (like "reboot" to make the computer reboot), or make the computer execute any message it receives as a program (with shell.run).

#10 EmTeaKay

  • Members
  • 115 posts

Posted 20 June 2012 - 11:37 PM

Would I do this?

if msg == reboot then
os.reboot()
end


#11 tfoote

  • New Members
  • 134 posts
  • LocationSalt Lake, UT

Posted 20 June 2012 - 11:52 PM

yes.

#12 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 21 June 2012 - 12:19 AM

View PostEmTeaKay, on 20 June 2012 - 11:37 PM, said:

Would I do this?

if msg == reboot then
os.reboot()
end
Yes, but remember to put quotes to the strings:
if msg == "reboot" then
  os.reboot()
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users