Jump to content




[1.51+] Using Network Cables (for dummies)


106 replies to this topic

#1 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 March 2013 - 10:03 AM

Network Interaction
A wired modem has the exact same functions as a wireless modem, so if you already know how to use wireless, you know how to work with wired modems, and vice versa. Wireless modems are just this, but without cables. Old Rednet APIs are even compatible with wired modems.

So let's say we have a setup like this:

Spoiler

We have sending and receiving code for both computers.

Left Computer:
local modem = peripheral.wrap('right')
modem.transmit(1,1,'Hello!')

Here we're wrapping the modem as a peripheral, like we would a monitor or printer. Then we use the .transmit() function to send a message, like rednet.send().

The first number is the frequency to send the message on. Computers that have modems opened on this frequency can receive messages on this frequency. In this example, modems that ran modem.open(1) will be able to receive this message, if they are connected in the wired network. You don't need to open your modem on that frequency to send a message.

The second number is the channel you want your senders to reply on. This is unimportant if you're only trying to send a message, but is helpful for creating servers. To get a reply, you have to open the modem on the frequency you want the reply on. In this example, if we wanted a reply, we would have to do modem.open(1) on this computer.

The message is self-explanatory. It's simply the string we send to other computers, like we would with rednet.send.

Right Computer:
local modem = peripheral.wrap('left')
modem.open(1)

local _, side, freq, rfreq, message = os.pullEvent('modem_message')

print('The message came on the '..side..' modem.')
print('It was on frequency '..freq..' and wants a reply on '..rfreq)
print('The message was: '..message)

Here, we're going to listen for the message that the left computer is sending to us. To do that, we have to open on the frequency that we believe the other computer is sending. We know that the left computer is sending a message on frequency 1, so we .open() on frequency 1.

When we receive our message, it's captured as an event, like 'key', 'char' or 'mouse_click'. The params for the event are side, the side of the modem we got our message from, freq, the frequency it was sent on, rfreq, the frequency we should reply on, and message, what the other computer sent to us.

Afterwards, we're simply printing the information we've received. Running this program first, and the program on the left computer afterwards should yield this result:

Spoiler


Peripheral Interaction
If, perhaps, you wanted to learn about peripheral interaction through cables, that's way easier. We'll go ahead and connect a regular old monitor to this setup.

Spoiler

In order to "enable" it, you have to right-click the connection to the monitor.

Spoiler

On the left computer, we can have this code:
local mon = peripheral.wrap('monitor_19')

mon.setBackgroundColor(colors.white)
mon.setTextColor(colors.black)
mon.setCursorPos(1,1)
mon.setTextScale(3)
mon.clear()

mon.write('Hello There!')

When we connect the computer to the monitor via modem and cable, then enable the monitor's connection, we instantly have access to the monitor, and instead of connecting to it though its side, we use it's "name". The name is given if you enable or disable the peripheral's connection, and in this case, it's monitor_19. From there, we would use it like any normal peripheral. The bottom is the result of running this script:

Spoiler

That should give a pretty good explanation on how to use the cables. Feel free to ask any questions.

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 March 2013 - 10:16 AM

nice tutorial :) about time someone did this :P

#3 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 March 2013 - 10:18 AM

View PostTheOriginalBIT, on 18 March 2013 - 10:16 AM, said:

nice tutorial :) about time someone did this :P
Haha, I know right? I feel like cables are like my specialty now, or something. I've been playing with them a lot recently ever since I found out how to use peripherals through them.

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 March 2013 - 10:19 AM

View PostKingdaro, on 18 March 2013 - 10:18 AM, said:

Haha, I know right? I feel like cables are like my specialty now, or something. I've been playing with them a lot recently ever since I found out how to use peripherals through them.
Haha yeh I've been meaning to, but I got a little too lazy...

You should reply to this topic

#5 SadKingBilly

  • Members
  • 160 posts

Posted 18 March 2013 - 03:21 PM

Is that a Windows theme you're running in the screenshot of the "msg" output? Or is it a different OS? It looks amazing.

Oh, and the tutorial is great, too! Is it possible to have a single modem open on more than one frequency at a time, though? I assume it is, since one of the returns from "modem_message" is the frequency it was received on.

#6 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 March 2013 - 03:36 PM

View PostTheCoryKid, on 18 March 2013 - 03:21 PM, said:

Is that a Windows theme you're running in the screenshot of the "msg" output? Or is it a different OS? It looks amazing.
It's Ubuntu, Cinnamon Desktop, theme is Boje Blue. Quite nice, yes.

View PostTheCoryKid, on 18 March 2013 - 03:21 PM, said:

Oh, and the tutorial is great, too! Is it possible to have a single modem open on more than one frequency at a time, though? I assume it is, since one of the returns from "modem_message" is the frequency it was received on.
Yep. You can have up to 128 channels opened on one modem.

#7 Altair357

  • Members
  • 6 posts

Posted 18 March 2013 - 03:45 PM

Very nice tutorial, helped me out a lot. I had no clue how to use these things beforehand.

Question: What is a 'reply' here? Does the peripheral have some sort of .reply() function?

#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 18 March 2013 - 03:46 PM

Eh, I would say that the reply channel is only there for helper/convenience reasons. A reply is simply a modem.transmit() through the reply frequency.

#9 Altair357

  • Members
  • 6 posts

Posted 18 March 2013 - 03:51 PM

Alright, thanks.

#10 dan200

  • Administrators
  • 542 posts
  • LocationCambridge, England

Posted 19 March 2013 - 12:33 AM

Good tutorial! One thing worth mentioning, however, is that the peripheral functions exposed by the wired modem are *exactly the same* as those on the wireless modem. So if you already know how to use wireless modes, you know how to use wired modems. You can even use the old "rednet" APIs exactly as you always could.

#11 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 19 March 2013 - 12:57 AM

Edited the OP. Thanks dan!

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 March 2013 - 12:59 AM

View PostKingdaro, on 19 March 2013 - 12:57 AM, said:

Edited the OP. Thanks dan!
You know its a good tutorial when dan200 replies saying its a good tutorial :P

#13 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 20 March 2013 - 03:46 AM

verry helpful, i was wondering how to get peripherals to work c_c

#14 BigSHinyToys

  • Members
  • 1,001 posts

Posted 20 March 2013 - 08:58 AM

There are a lot of useful function that the wired modem can do when used directly.
open
isOpen
close
closeAll
transmit
isWireless
getNamesRemote
isPresentRemote
getTypeRemote
getMethodsRemote
callRemote

broken down
open
isOpen
close
closeAll
transmit
standard rednet functions
isWireless - checks if the modem is WIFI or wire
getNamesRemote - lists all active devices
isPresentRemote - checks if a device is connected example "drive_0" returns true of false
getTypeRemote - tells you the type of the device using its handle
getMethodsRemote - same as peripheral.getMethods() but for remote devices
callRemote - used to actual call a remote peripheral to do something

example
peripheral.call("back","callRemote","monitor_1","write","hello")

That is the underlying structurer to the peripheral wrap system. I can see some interesting hacks based on these calls.

#15 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 20 March 2013 - 09:42 AM

I was going to make a separate section for those functions, but I figured that I'd wait until the wiki is updated with more comprehensive information than I can come up with.

#16 Angry1987

  • New Members
  • 2 posts

Posted 21 March 2013 - 04:38 AM

hey i have 1 problem
i will starts 2 programs from 1 computer,here the startup:
shell.run("disk/test" and "disk/test2")
test first line
m = peripheral.wrap("monitor_5")
test2 first line
m = peripheral.wrap("monitor_6")
but 1 program only starts on monitor_6

#17 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 March 2013 - 04:53 AM

View PostAngry1987, on 21 March 2013 - 04:38 AM, said:

hey i have 1 problem
i will starts 2 programs from 1 computer,here the startup:
shell.run("disk/test" and "disk/test2")
test first line
m = peripheral.wrap("monitor_5")
test2 first line
m = peripheral.wrap("monitor_6")
but 1 program only starts on monitor_6
You cannot use shell.run in that way....... you need to use coroutines to achieve what you want. or as shown in this example the parallel api.

local function run1()
  shell.run("disk/test")
end

local function run2()
  shell.run("disk/test2")
end

parallel.waitForAny( run1, run2 )


#18 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 21 March 2013 - 05:09 AM

i dont think he wanted them to run simotanously
anyway, your code is broken in so many ways
parallel.waitForAll(
function() shell.run("disk/test") end,
function() shell.run("disk/test2") end
)


#19 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 March 2013 - 05:12 AM

Uhhh the only difference between your code and mine is that you used waitForAll as opposed to waitForAny and that I did it in a way that is the least confusing for people to understand... How does that make it broken in so many ways? O.o

#20 Angry1987

  • New Members
  • 2 posts

Posted 21 March 2013 - 05:15 AM

thx @ theOrginalBIT and PixelToast for the help :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users