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

Rednet Basics Part I

Started by hbomb79, 30 December 2014 - 08:14 AM
hbomb79 #1
Posted 30 December 2014 - 09:14 AM
First of all lets start by saying that this is a very basic tutorial, if the response is good then a second part covering more advanced communications will be released, Lets go!

Before you can start using rednet you will need two computers and two wireless modems attached to the a fore mentioned computers, Once attached we are going to have to 'open' the rednet on this modem.

This is NOT the same-as opening channels using the modem API, this will be covered in future parts.

To open your rednet your going to want the modem 'wrapped' as a peripheral, we will use this is other tutorials when we move onto the modem API, essentially means you can access it as a variable and uses its functions like sending and receiving messages, here's the very basic way of opening a modem, for this you will need to hardcode the side in which the modem resides;


local wireless = peripheral.wrap("top") --#Side of modem
rednet.open("top") --#Open Modem

The first line wraps the modem allowing you to use it later, the second opens the modem, the modem should now have a red ring around it. The only problem here is that errors may arise if the modem isnt present or is on another side, to fix that we will need to create more sophisticated code, like so;


for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
  print('Found Modem On Side '..side.."!")
  rednet.open(side)
  wireless = peripheral.wrap(side) --# For when we use the Modem API
  break --#Opened modem, dont open any others.
end
end

This will open the first modem it finds on any side of the computer, very helpful, although this will open wired modems as well, so make sure you only attach wireless modems, they are both called modems so there is no easy way to distinguish the two…

Now we have two modems open, one on each computer your ready to start communicating, to do so we need to set up some event listening, events are queued by the computer when something happens, like a key press or a mouse click, along with the event variables are passed containing important information. Such information included the rednet message, to setup event listening lets do the easy way first:

local id, message, protocol = rednet.receive()

This code is VERY simple, it will wait for ever or until a rednet message is received, this isnt a very 'good' way to receive messages as this requires the use of parallel or co-routine unnecessarily. You could instead use event listeners, when a message is received send it off to get processed, although it really depends on the situation, my programs use various methods, if I dont NEED to be listening for other events then I go for the simple way, else I use this code snippet:


function mainEventLoop()
while true do
  local e, p1, p2, p3, p4, p5, p6 = os.pullEvent() --#Waits for events and places all values into local vars.
  if e == "rednet_message" then --#Handle rednet message
  elseif e == "mouse_click" then element.tryClick(e, p1, p2, p3)
  elseif e == "terminate" then os.shutdown() end
  sleep() --#Prevents too long without yielding errors that MAY occur.
end
end

This uses my basic framework, although even this is obselete for my purpose, I use the more common event registration and call a function upon event appearance, I may get into this if you guys want in a future part, for the sake of the tutorial I will use the simple method of receiving messages, lets start our first test program, this program will be on the receiving computer;


function waitForMessage(displayMessage)
local sender, message, protocol = rednet.receive()
if displayMessage then print(message) else return message end
end

waitForMessage(true)
This waits for a message and the true im passing to the function tells it to print the message rather than return it as a var. This part will be on the sending computer;


function sendMessage(destination, message, protocol)
if destination and message then
  rednet.send(destination, message, protocol)
end
end
sendMessage(49, 'hey')

This will send a message to computer ID 49 with the message 'hey', to find your computer id type
id
into the terminal.

Complete Code
SpoilerJust incase you lost your way heres all the code in one view;

PC1

for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
  print('Found Modem On Side '..side.."!")
  rednet.open(side)
  wireless = peripheral.wrap(side)
  break --#Opened modem, dont open any others.
end
end
function waitForMessage(displayMessage)
local sender, message, protocol = rednet.receive()
if displayMessage then print(message) else return message end
end

waitForMessage(true)

PC2

for i, side in ipairs(rs.getSides()) do --#Loop for each side on the computer that redstone can be used, these can also be used by modems.
if peripheral.getType(side) == 'modem' then --#Did we find a modem... Or some other peripheral?
  print('Found Modem On Side '..side.."!")
  rednet.open(side)
  wireless = peripheral.wrap(side)
  break --#Opened modem, dont open any others.
end
end
function sendMessage(destination, message, protocol)
if destination and message then
  rednet.send(destination, message, protocol)
end
end
sendMessage(49, 'hey')

Well everyone, that concludes part one, next part will cover protocols and table sending. If you want a second part then let me know!

- Harry
Edited on 01 January 2015 - 01:20 AM
venya #2
Posted 30 December 2014 - 07:29 PM
one suggestion to wrap the peripheral use peripheral.find("modem")
this may will help
KingofGamesYami #3
Posted 30 December 2014 - 09:49 PM
I'm pretty sure to use rednet you don't even need to wrap the modem - just find it.
Dragon53535 #4
Posted 31 December 2014 - 02:03 AM
Rednet is also avaliable for WIRED modems.
hbomb79 #5
Posted 31 December 2014 - 10:20 AM
First of all, the way the peripheral is wrapped currently works just as good, your version is using peripheral.find which is just the same as peripheral.getType(), although yours may contain multiple causing crashes if not prepared for (tested). This is part ONE and is written very simply, like I said, if you want more advanced tutorials then let me know, otherwise this will be the first and last

Second of all, you are right, you dont need to wrap it for this part, although the next few parts the peripheral will be needed as a wrapped var, this is why I did it then, like I explained in the tutorial

And yes, I know its available on wired modems, this is why I warned people about that in the tutorial, like I said, there really is no way of easily differentiating between the two as they both return 'modem' and have access to rednet API
Edited on 31 December 2014 - 09:22 AM
venya #6
Posted 31 December 2014 - 06:24 PM
count=0
modem = peripheral.find("modem",function(name,object) count=count+1 end)

will give the amount of modems connected

(I tried this:
"monitor = peripheral.find("monitor",function(name,object) count=count+1 end)"
with monitors
it worked)
Lyqyd #7
Posted 31 December 2014 - 07:55 PM
This would be easier:


local modems = {peripheral.find("modem")}
print(#modems)
hbomb79 #8
Posted 31 December 2014 - 11:35 PM
Either way, this is part ONE, it is easy and it is harder to handle your way because you would have to use a loop each time you wanted to access the modems because they are in a table, it's just easy this way and unescesary to have more than one modem open at a time.
Dragon53535 #9
Posted 01 January 2015 - 12:44 AM
First of all, the way the peripheral is wrapped currently works just as good, your version is using peripheral.find which is just the same as peripheral.getType(), although yours may contain multiple causing crashes if not prepared for (tested). This is part ONE and is written very simply, like I said, if you want more advanced tutorials then let me know, otherwise this will be the first and last

Second of all, you are right, you dont need to wrap it for this part, although the next few parts the peripheral will be needed as a wrapped var, this is why I did it then, like I explained in the tutorial

In this tutorial you are doing nothing with the wrapped peripheral, and if you want to do anything with rednet, you NEVER will use a wrapped peripheral. Everything to do with rednet is handled inside the rednet api, it wraps the peripheral internally and uses that for your rednet stuff, however you never will use the wrapped peripheral yourself to do anything as that changes from the rednet api to the modem api.

To summarise: Using the wrapped peripheral is the modem API, using the rednet API and it's commands, is not the same.
hbomb79 #10
Posted 01 January 2015 - 02:20 AM
I am aware and like I SAID, the modem APIs will be covered in other videos this is why its there, second of all, incase someone uses this to wrap other peripherals then they can. Either way the tutorial works and does its job, if you had read it youd have seen that wrapping the peripheral is for future tutorials.