Difference between revisions of "Modem (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Confirming CCWiki is un-broken (also, typo fix).)
m (Change to use newer table style.)
Line 6: Line 6:
  
 
Here is a list of functions available from modems:
 
Here is a list of functions available from modems:
{| border="1" cellpadding="2" cellspacing="0"
+
 
!style="background:#EEE" width="200px"|Function
+
 
!style="background:#EEE" width="*"|Description
+
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
|-
+
<tr><td colspan="2" style="font-weight: bold; font-size: large; padding-bottom: .3em; border-bottom: solid #C9C9C9 1px; background: #D3FFC2; line-height:28px;">
|isOpen( [[int_(type)|int]] channel )
+
[[File:Grid_disk.png|24px]]&nbsp;&nbsp;
|Check to see if the specified channel is open
+
Modem (API)
|-
+
</td></tr>
|open( [[int_(type)|int]] channel )
+
 
|Opens the specified channel to allow for listening. The channel specified must be larger than 0 and less than 65535.
+
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
|-
+
 
|close( [[int_(type)|int]] channel )
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.isOpen]]({{type|int}} channel)</td>
|Closes an open channel to disallow listening
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Checks to see if ''channel'' is open</td></tr>
|-
+
 
|closeAll( )
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.open]]({{type|int}} channel)</td>
|Closes all open channels
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Opens ''channel'' to allow for listening. The channel specified must be larger than 0 and less than 65535</td></tr>
|-
+
 
|transmit( [[int_(type)|int]] channel, [[int_(type)|int]] replyChannel, [[string_(type)|string]] message )
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.close]]({{type|int}} channel)</td>
|Transmits a message on the specified channel
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Closes an open channel to disallow listening</td></tr>
|-
+
 
|isWireless( )
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.closeAll]]()</td>
|Returns true if the modem is wireless; false if it is wired.
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Closes all open channels</td></tr>
|}
+
 
 +
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.transmit]]({{type|int}} channel, {{type|int}} replyChannel, {{type|string}} message)</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Transmits a message on the specified channel</td></tr>
 +
 
 +
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[modem.isWireless]]()</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns true if the modem is wireless; false if it is wired</td></tr>
 +
</table>
 +
 
 
==Sending Messages==
 
==Sending Messages==
 
Sending messages is simple and does not require that you open any channels. Simply use the transmit function like so:
 
Sending messages is simple and does not require that you open any channels. Simply use the transmit function like so:

Revision as of 15:34, 2 May 2013

This page is for the modem API. For the blocks, see Modem.

With the advent of ComputerCraft 1.5, the behavior of modems has changed somewhat drastically. Although the Rednet API still has the same functionality, it is really just a wrapper for what is a completely different system underneath.

The major change introduced to modems in this version of ComputerCraft is channels: Channels are essentially open networks which can be opened, closed, and listened on by any computer within range. To interact with channels, one must wrap/interact directly with the peripheral as opposed to the previous interaction with the rednet API.

Here is a list of functions available from modems:


Grid disk.png   Modem (API)

Method NameDescription
modem.isOpen(int channel) Checks to see if channel is open
modem.open(int channel) Opens channel to allow for listening. The channel specified must be larger than 0 and less than 65535
modem.close(int channel) Closes an open channel to disallow listening
modem.closeAll() Closes all open channels
modem.transmit(int channel, int replyChannel, string message) Transmits a message on the specified channel
modem.isWireless() Returns true if the modem is wireless; false if it is wired

Sending Messages

Sending messages is simple and does not require that you open any channels. Simply use the transmit function like so:

 
 local modem = peripheral.wrap("right") --Wraps the modem on the right side.
 modem.transmit(3, 1, "Hello world!")  
 peripheral.call("right", "transmit", 3, 1, "This will also work!")
 

What did that do? First I wrapped the peripheral in order to interact with it. Second, I used modem.transmit(channel, replyChannel, message) in order to send my message. In case you were wondering, the reply channel is captured by the listening computer and suggests which channel they should reply on.

Receiving Messages

Receiving messages requires that you be familiar with events. As of this moment, there is no api which cuts out events from the process. Here is an example of how to receive messages:

 
 local modem = peripheral.wrap("left")
 modem.open(3)--Open channel 3 so that we can listen on it
 local event, modemSide, senderChannel, 
   replyChannel, message, senderDistance = os.pullEvent("modem_message")
 print("I just received a message from: "..senderChannel)
 print("I should apparently reply on: "..replyChannel)
 print("The modem receiving this is located on the "..modemSide.." side")
 print("The message was: "..message)
 print("The sender is: "..senderDistance.." blocks away from me.")
 

So what did I do? Quite simply, I called os.pullEvent() with the string argument "modem_message", which blocks all other events from being returned. When the "modem_message" event is captured, it returns the arguments: event, modemSide, senderChannel, replyChannel, message, senderDistance. I captured these and then printed them out.

Keep in Mind...

  • You can only open 128 channels at any given time.
  • The max channel you can open is 65535.
  • You can listen on more than one channel at a time. For example, if I have channel 3 and channel 5 open and somebody sends a message on channel 5, I will still receive it. If they send a message on channel 3, I will receive that.
  • Sending messages does not require you to open any channels prior to sending it.
  • If you aren't receiving a message when you think you should, check to make sure that you have opened the channel first!
  • Modems and channels are not secure! If you are sending a message over rednet, it is available to any computer listening on the sent channel.