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

Simple Bridge between wired and wireless programs

Started by MrDoomah, 09 April 2013 - 05:40 PM
MrDoomah #1
Posted 09 April 2013 - 07:40 PM
Hi there!

with the new 1.52 version making wireless modems requiring an enderpearl and the ability to remotely control peripherals through wires I find myself using only very little wireless modems. However to communicate with any turtle you will still need wireless modems on your computer.
So I've written a small program that only serves to form a communication bridge between wired and wireless networks:

http://pastebin.com/SY8dWUnJ

tArgs = {...}
local wmodemSide,lines,count,channelCount = nil,0,0,0
if #tArgs % 2 ~= 0 then error("Please state a start and end of each range") end
for i = 1,#tArgs,2 do
channelCount = channelCount + tArgs[i+1] - tArgs[i] + 1
end
if channelCount > 128 then error("To many channels: You've opened "..channelCount.." while maximum is 128") end
for _,k in pairs(rs.getSides()) do
if peripheral.getType(k) == "modem" then
  m = peripheral.wrap(k)
  if m.isWireless() then
   wmodem = peripheral.wrap(k)
   wmodemSide = k
  else
   modem = peripheral.wrap(k)
  end
end
end
term.clear()
term.setCursorPos(1,1)
print("Currently bridging between wire and wireless on channels:")
if tArgs[1] == nil then
print("1 - 128")
lines = 1
for i = 1,128 do
  modem.open(i)
  wmodem.open(i)
end
else
for i = 1,#tArgs,2 do
  print(tArgs[i].." - "..tArgs[i+1])
  lines = lines + 1
  for j = tArgs[i],tArgs[i+1] do
   modem.open(j)
   wmodem.open(j)
  end
end
end
print("Press Q to quit")
while true do
local event, mSide, sChannel, rChannel, msg, d = os.pullEvent()
if event == "modem_message" then
  if mSide == wmodemSide then
   modem.transmit(sChannel,rChannel,msg)
  else
   wmodem.transmit(sChannel,rChannel,msg)
  end
  term.setCursorPos(1,lines+4)
  count = count + 1
  print(count.." messages so far")
elseif event == "char" and mSide == "q" then
  modem.closeAll()
  wmodem.closeAll()
  break
end
end

By default it will forward channels 1 through 128 but you can set custom channels as arguments, as long as you state a begin and end range, (For a single channel you can make the start and end of a range the same ofc) and don't want more than 128 channels. If you really need more channels you should just use a second computer.

Hope you like it! :)/>
superaxander #2
Posted 09 April 2013 - 10:38 PM
Looks pretty solid
mtwiscool #3
Posted 14 April 2013 - 11:31 AM
anyway to get it to work on fireworlf?
MissionFirewall #4
Posted 14 April 2013 - 02:22 PM
It looks over-complicated. I made a similar program where it took what it heard from wired, and said it back over the wireless. But yours may be more reliable and have some more pros than mine