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

Reading Rednet

Started by coolblockj, 24 February 2012 - 12:44 AM
coolblockj #1
Posted 24 February 2012 - 01:44 AM
Hi, im trying to make a system that my mainframe can send a signal using modems to the computers using rednet. And if that message through rednet has their password, allow access into them. I cant get this work, any help?
TheNotch3000 #2
Posted 24 February 2012 - 06:56 AM
Can you show us your code? And what exactly is the problem? Do you run into errors, don't you know where to begin.
coolblockj #3
Posted 24 February 2012 - 04:44 PM
Can you show us your code? And what exactly is the problem? Do you run into errors, don't you know where to begin.
I have tried many things to try to get it to work, all of them had errors or wouldn't cooperate. So i dont really know where to begin now.
Advert #4
Posted 24 February 2012 - 05:48 PM
It'd really help if you could paste what you got, even if it doesn't work.

Here are some pointers:
1) You need to use rednet.open("side"), where side is the side you placed the modem on your computer, before you can send stuff through it.

2) Here's a simple client-server program:
server:

rednet.open("top") -- assuming that the modem is on the top
repeat
 print("'q' to quit.")
 local event, p1, p2 = os.pullEvent()
 if event == "rednet_message" then
  print("Message from ", p1, ": ", p2)
  rednet.send(p1, "Message Recieved.")
 end
until event == "char" and p1 == "q" -- Exit the loop if "q" is pressed with the computer open
client:

rednet.open("top")
repeat
 print("'m' to send a message; 'q' to quit.")
 local event, p1, p2 = os.pullEvent()
 if event == "rednet_message" then
  print("Message from ", p1, ": ", p2)
 elseif event == "char" and p1 == "m" then
  print("Input target & Message:")
  rednet.send(tonumber(read()), read()) -- This will take input twice: first, for the ID, and a second time, for the message itself.
 end
until event == "char" and p1 == "q"

You can expand upon this, if you wish.
coolblockj #5
Posted 24 February 2012 - 09:09 PM
Thanks, sorry, i didn't put in the code or the errors.
Also, what does os.pullEvent do?
Advert #6
Posted 24 February 2012 - 09:15 PM
Thanks, sorry, i didn't put in the code or the errors.
Also, what does os.pullEvent do?

os.pullEvent will get an event from the stack.

For example:
Every time you press a key when using the computer, it fires up to two events:
one 'key' event, and another 'char' event (if you hit a-z, 0-9, something that isn't shift/capslock/ctrl/…)
If a redstone input is changed, it'll fire a "redstone" event.
If you're using rednet, and a message is sent to your computer, a "rednet_message" event is fired.

os.pullEvent() will take the most recent event, remove it from the queue, and return it to you.

Type 'help events' ingame for a list of the rest.
MysticT #7
Posted 25 February 2012 - 12:25 AM
os.pullEvent() will take the most recent event, remove it from the queue, and return it to you.
Actually, since it's a queue, I guess it takes the oldest event and returns it.
Anyway, it's used to get the events generated by the user, the computer (timers, alarms), another computer (rednet), redstone, peripherals, etc…
In this case it's used to get the rednet messages and user input.
Advert #8
Posted 25 February 2012 - 03:39 AM
os.pullEvent() will take the most recent event, remove it from the queue, and return it to you.
Actually, since it's a queue, I guess it takes the oldest event and returns it.
Anyway, it's used to get the events generated by the user, the computer (timers, alarms), another computer (rednet), redstone, peripherals, etc…
In this case it's used to get the rednet messages and user input.
Err, yes, my bad; I meant the oldest event.