Peripheral Basics - isPresent, getType, getMethods, wrap, call (Will be linked when finished)
Modem Basics : Wired Modems (Will be linked when finished)
Hello, welcome to the AaP Renewal Project 'Modem Intermediate : Remote Peripherals' tutorial.
In order to begin using peripherals remotely, you're going to have to place a wired modem on both your computer and peripheral, and wire them up with networking cable, like in this picture :
Spoiler
Now you need to turn the modem attached to the peripheral on. Right click on it and a message will appear in the chat, and the grey ring will become red :
Spoiler
Once you've gotten the network setup, you can work on establishing the connection in software. Go into your computer and fire up the lua prompt.
First, you need to interface with your wired modem. I'd suggest using peripheral.wrap(), but peripheral.call() works too.
local modem = peripheral.wrap("right")
-- If you modem is on the right, that is
Now we can use the Modem API to interact with our device. In order to figure out what devices are connected, we'll use modem.getNamesRemote().
This ensures that only peripherals that are connected to our modem on the right side will show up as candidates for peripheral operations.
If you want any peripheral attached to the computer in some way (for example, a peripheral connected directly to the computer, or through a different modem), use peripheral.getNames().
Both peripheral.getNames() and modem.getNamesRemote() return a table, which is numerically indexed with the names as values to those keys.
If we iterate through this table, we'll get the names of the peripherals. The table would look something like the commented table, if it were declared manually :
local peripherals = modem.getNamesRemote()
--[[
This table will be different sizes depending on both the size of the network
(how many peripherals) and how you are narrowing your peripheral options
local peripherals = {
[1] = "drive_0",
}
]]
Ensure that your peripheral is the type you want, by using peripheral.getType().
for k, v in pairs(peripherals) do
-- We want a floppy drive, which is type 'drive'
if peripheral.getType(v) == "drive" then
toWrap = v
end
end
If it's what you want, you can use the name of the peripheral in call() or wrap() calls, in place of the directions like "front" or "top"
local floppy = peripheral.wrap(toWrap)
if not floppy then
error("No floppy drives attached")
end
-- Now we interface with the drive, as normal
if floppy.isDiskPresent() then
-- Floppy manipulation here
else
write("No disk in drive :(/> ")
end
-- Rest of code
That's it for the tutorial. Now you're able to use peripherals across the network. Think of the possibilities!
Thanks to Lyqyd, TheOriginalBIT, and others who has commented and made suggestions on how to make this tutorial better.