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

Multiple Peripherals via Network Cable

Started by Left, 22 March 2013 - 07:50 PM
Left #1
Posted 22 March 2013 - 08:50 PM
Hi,
I just had a crazy idea. I want to make a VERY basic program that can mass print a file you create. I just need to know if you can make a computer be assigned to multiple peripherals via a network cable. Thanks, Lin
Kingdaro #2
Posted 22 March 2013 - 10:48 PM
Yes.

If you prefer to do it on your own, it's not that hard either:


local names = peripheral.call('sideYourModemIsOn', 'getNamesRemote')

for i=1, #names do
  if peripheral.getType(names[i]) == 'printer' then
    local printer = peripheral.wrap(names[i])
    printer.newPage()
    printer.setPageTitle('boop')
    printer.write('booooooooooooop')
    printer.endPage()
  end
end
Cloudy #3
Posted 23 March 2013 - 01:04 AM
Why do people insist on making things difficult for themselves?

Do NOT call any modem functions directly. Just call peripheral.getNames().
Kingdaro #4
Posted 23 March 2013 - 03:04 AM
Well, cloudy, I used to do that, but the problem with that, is that it returns any and all usable peripherals, including those not connected through wired modems. You could get a table of names that looks like this if iterated and printed:


back
monitor_0
monitor_1
monitor_2
right
printer_0
printer_1
left
bottom
computer_5
computer_6

And I prefer just having more control over which peripherals I'm using. Just using getNames can become problematic if I have other peripherals connected on a different wired network for other purposes.
MindenCucc #5
Posted 23 March 2013 - 03:20 AM
I just wrote in 5 minutes a function for this :D/>


function getTypes(sType)
local fancyCrap = {}
local net = {}
local ret = {}
if sType == nil or type(sType) ~= "string" then
  error("Cannot parse input.", 1)
else
  net = peripheral.getNames()
  for asdX, valX in ipairs(net) do
   for netX = 1, #net do
    if peripheral.getType(valX) == sType then
	 retval=true
    else
	 retval=false
    end
   end
   if retval then
    table.insert(ret, valX)
   end
  end
  return ret
end
end

Usage: printers={}
printers=getTypes(string Type)

Example: {"printer_1", "left", "bottom"}