Jump to content




[1.3] How To Use Modems (Rednet)


92 replies to this topic

#1 Casper7526

    OG of CC

  • Members
  • 362 posts

Posted 26 February 2012 - 03:01 AM

[As of ComputerCraft 1.5, this information may be out of date, in part or in whole. Read and use accordingly. -L]

OK.. so you've got 1.3 and now your thinking to yourself... I wanna use this modem... I wanna create the internet in minecraft!!!!

Lets take it back a few steps and first learn all about the modem and how to use it in a basic way.

The first thing you should know is that to attach the modem to a computer you must sneak first then right-click on the side of the computer you would like to attach the modem too.

Turtles with modems automatically have them placed on the right side when crafted.

So now that we have 2 computers or a computer and a turtle setup with modems we are going to learn how to pass information between them.

Computer 1:
rednet.open("right")
This command opens the port of the modem. The "right" statement is the side of the computer that the modem is located.
rednet.broadcast("Our information")
This command says that we want to broadcast the string "Our Information" to every modem within range. Anyone that is listening for information will receive our statement.

Turtle 1:
rednet.open("right") 
Once again, we have to open up the port on the modem so that we can communicate, luckily turtle's always have their modems placed on the right side.
id, message  = rednet.receive(10)
This command makes the computer listen to the modem for 10 seconds. You could simply do () instead of (10) and it would listen indefinitely until it received communication. The rednet.receive function returns us 2 variables when it receives information. The ID of the computer that sent the information and the message itself.
print ("Computer ".. id .. " has sent us a message")
print ("The message is")
print (message)
This is just some output information so we can see who sent us something and what that something was.

Now that's all fine and dandy, but we don't want all those computers getting our information, thats just wrong, we don't wanna share our private data with everyone. So this time instead of using the rednet.broadcast command we will be using the rednet.send command.

Computer 1:
rednet.open("right")
rednet.send(5, "Our Information")
This will open the port like normal, and then it will send information but it will only send that information to Computer/Turtle #5 (you can see the ID of a computer or turtle by typing "id" inside your computer or turtle)

So now we have to make sure we use Computer or Turtle #5 to receive our information and we can do the same script as above.


I hope this gets everyone on their way to using modems successfully, I understand it's a brief tutorial, but your goal is to manipulate everything you learned here into something amazing :)/>/>

If you need more help on anything in computercraft at anytime you can simply type "help index" inside your computer and it will show you an index of all the help files that are available. One of them happens to be "help rednet" which explains some more of the functions you have access to.

#2 Bennievv

  • Members
  • 26 posts
  • LocationThe Netherlands

Posted 26 February 2012 - 07:45 AM

Thanks! Now I know how it works :)/>

#3 Pizmovc

  • New Members
  • 5 posts

Posted 28 February 2012 - 08:49 PM

Thanks!
Now I'm interested in sending a program/file to my turtle... So it can start working for me!

#4 Casper7526

    OG of CC

  • Members
  • 362 posts

Posted 28 February 2012 - 08:49 PM

Well just remember you can only send strings, so basically you would save your program file into a string then turn it back into a file on the other end :D/>

#5 Pizmovc

  • New Members
  • 5 posts

Posted 28 February 2012 - 09:10 PM

I thought about that, but it seemed like there has to be a better way!

How would I go about doing that? I can't just do x = MyProgram right?

#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 28 February 2012 - 09:18 PM

View PostPizmovc, on 28 February 2012 - 09:10 PM, said:

How would I go about doing that? I can't just do x = MyProgram right?
You have to do open the file, read all and send it.
Something like this:
local file = fs.open("Your/file/path", "r")
if file then
    local sFile = file.readAll()
    rednet.send(ID, sFile)
    file.close()
end


#7 Pizmovc

  • New Members
  • 5 posts

Posted 28 February 2012 - 09:41 PM

Awesome! This now works!
Now all I need to do is save it to file! Thanks!

#8 Brilliantos

  • New Members
  • 4 posts

Posted 01 March 2012 - 01:32 AM

Hi, ive gotten all this working, my turtle can recieve commands from my Pc. My problem is there are other people broadcasting commands which are messing with my turtle. I would like to know if it is possible for the turtle to only recieve commands from my Pc
Thanks :unsure:/>

#9 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 01 March 2012 - 01:48 AM

Yes, that is possible, in a way.
It will always 'see' all messages, even the ones from other computers.
But you can tell it to only listen for messages coming from your computer ID.

If you're using rednet.receive( timeout ) to listen for incoming messages, then ignore all messages whose id does not match the one from your sending computer, e.g.:
local allowedComputerID = 5

local id, msg = rednet.receive(10)
if id == allowedComputerID then
  print( msg )
end

If you're using the event-method, then you do the same:
local allowedComputerID = 5

local sEvent, param1, param2 = os.pullEvent()

if sEvent == "rednet_message" and param1 == allowedComputerID then
  print( param2 )
end

Disclaimer: That's all from the top of my head and thus untested in-game. Should work though.
Fix: Changed local allowedComputerID = 5 to local allowedComputerID = "5"

Edited by Espen, 01 March 2012 - 02:58 PM.


#10 6677

  • Members
  • 197 posts
  • LocationCambridgeshire, England

Posted 01 March 2012 - 10:57 AM

For some reason
rednet.recieve()
returns an error about attempting to call nil. It does this when I add a parameter as either an integer or string aswell.

I prefer to use events anyway but I can't see why it won't work.

#11 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 01 March 2012 - 11:24 AM

That is because there is no rednet.recieve().
You mistyped it, it's actually rednet.receive(). :unsure:/>

In general: attempt to call nil

#12 Brilliantos

  • New Members
  • 4 posts

Posted 01 March 2012 - 02:05 PM

Hmm i tried what you said in game and it keeps telling me it is expecting 'then' even though i have put the then in the right place

#13 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 01 March 2012 - 02:29 PM

View PostBrilliantos, on 01 March 2012 - 02:05 PM, said:

Hmm i tried what you said in game and it keeps telling me it is expecting 'then' even though i have put the then in the right place
Can you post your code or a thread where you maybe posted it already?
It'd be a lot easier to pinpoint your problem then. :unsure:/>

#14 Brilliantos

  • New Members
  • 4 posts

Posted 01 March 2012 - 02:40 PM

Its fine i got it working you just forgot the "" around AllowedComputerID :unsure:/>

#15 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 01 March 2012 - 02:52 PM

View PostBrilliantos, on 01 March 2012 - 02:40 PM, said:

Its fine i got it working you just forgot the "" around AllowedComputerID :unsure:/>
Ah, I see what you mean, I defined allowedComputer as a number instead of a string.
Thought the rednet_message event would return the ID as a number, not a string.
Thx, will fix the code above.


Wait a minute, something isn't right. It should return a number, will test this in-game now...

Edit: Ok I tried it in-game now and it works fine for me, so you must've done something wrong.
The only tings I didn't include were the call to rednet.open and a loop, because I only wanted to show the principals and assumed you would integrate it into your program anyway.
So perhaps that is what might've given you problems?
Just to be sure here's the code within a loop, incl. the rednet.open call:
local nAllowedComputerID = 1	-- Change this number to the ID of your sending computer.
local sSide = "back"	-- Change this to the side of your receiving computer, where messages are coming in.

rednet.open( sSide )	-- Open the rednet-port on side 'sSide'
while true do
  local sEvent, param1, param2 = os.pullEvent()	-- Listen for events and two of their parameters.

  if sEvent == "rednet_message" and param1 == nAllowedComputerID then
	print( param2 )
  end

  if sEvent == "char" and param1 == "q" then break end	-- Pressing q stops the program.
end

rednet.close( sSide )	-- Close the rednet-port on side 'sSide'

Edited by Espen, 01 March 2012 - 03:16 PM.


#16 mrgreaper

  • Members
  • 88 posts

Posted 02 March 2012 - 03:05 AM

ok im completly stuck

i have a terminal written in 1.2 that just asks for a password to then activate our bases light system, as we have grown we keep having to run to this terminal to do the command so with 1.3 and modems all our prayers are answered how ever its not working
function os.pullEvent()
local sEvent, param1, param2, event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
os.reboot()
end
if sEvent == "rednet_message" and param1 == 144 and param2 == 54 then
rs.setOutput("bottom", true)
sleep(1)
rs.etOutput("bottom", false)  
end
return event, p1, p2, p3, p4, p5
end
function pass()
term.clear()
term.setCursorPos( 1, 1 )
print ("Base Lockdown disengage system")
print ("Enter Authorisation to unlock and light up")
t = read("*")
write ("> ")
if t == "lettherebelight" then
rs.setOutput("bottom", true)
sleep(1)
rs.setOutput("bottom", false)
pass()
elseif t == "sillypc" then
term.clear()
term.setCursorPos( 1, 1 )
print ("PRROGRAM TERMINATED")
shell.run"shell"
else
pass()
end
end
pass()
(its 0302 and it took me 20 minutes to copy what was on my screen to this so the odd grammer error may or may not be just a typo, its on our server and with out abusing admin powers i dont have access to the computer folder (and ctrl c ctrl v dont work in the editor :unsure:/> (anyone know how to add ctrl+c or +v will be my hero))
as you can see its a modified tutorial script, i added the anti ctrl+t code, and masked the input.

now what i want it to do is function as it has always done but if it hears a code from terminal 144 that says 54 it should power up the base, now since the ctrl + t code is working even if the program is waiting for input it seemed logical to add the listen code there so using the example further up i added the veriables in

running this it sticks before the input command so it cant do anything (it disables ctrl + t nicely lol had to put a discdrive next to it to gain access lol)

so what did i do wrong?


edit retyping out the code helped
return event, p1, p2, p3, p4, p5 should be return sEvent, param1, param2, event, p1, p2, p3, p4, p5
and it may help if i turn the modem on lol

#17 remlly

  • New Members
  • 9 posts

Posted 03 March 2012 - 03:30 PM

ok, can somebody help, if i typ rednet.open('''right'') its says no such program. i have craftos 1.3

#18 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 03 March 2012 - 03:38 PM

View Postremlly, on 03 March 2012 - 03:30 PM, said:

ok, can somebody help, if i typ rednet.open('''right'') its says no such program. i have craftos 1.3
That's because rednet.open() is a call to a function and not a program.
You can't call funtions directly from the shell, you'll either have to create a program and write it in there, or you go into the Lua interactive shell.
The latter can be started by typing "lua". Your command prompt will then change to "lua>" and then you can call rednet.open("right").

#19 remlly

  • New Members
  • 9 posts

Posted 03 March 2012 - 03:50 PM

i''ll try...
edit, nope. if i typ rednet.open(''back'')'i get :27: [string ''lua'']:1: expected, i dont know waht this means

and the same on the turtle but at the end unexpected symbol

#20 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 03 March 2012 - 04:09 PM

View Postremlly, on 03 March 2012 - 03:50 PM, said:

i''ll try...
edit, nope. if i typ rednet.open(''back'')'i get :27: [string ''lua'']:1: expected, i dont know waht this means

and the same on the turtle but at the end unexpected symbol
Ahh, now I see your problem.^^
You're not using double-quotes, but two single-quotes instead.
Use two double-quotes " instead of two single-quotes ''
The former is one character and the latter is two characters.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users