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

Redstone Networking.

Started by gamax92, 18 March 2013 - 01:08 PM
gamax92 #1
Posted 18 March 2013 - 02:08 PM
Just the basics of networking. Use for whatever reason.
Perhaps you wanted to use wireless redstone modules to get transdimensional communication.
Perhaps you like being slow and perfer 10 bits per second instead of instanious wireless modems.

Works like the rednet api.
open(side) open side
close(side) close last opened side.
send(msg) send a message
receive() receive a message, returns senderID, message.

pastebin get S54Rz18M rsnet

local sSide

local toBits = function(num)
	local t = {}
	while num > 0 do
		rest = math.fmod(num, 2)
		t[#t + 1] = rest
		num = (num - rest) / 2
	end
	return tabrev(t)
end

tabrev = function(tInput)
	local iSize = #tInput
	local tOutput = {}
	for k,v in ipairs(tInput) do
		tOutput[iSize - k + 1] = v
	end
	return tOutput
end

local string_rpad = function(str, len, char)
	if char == nil then char = ' ' end
	return string.rep(char, len - #str) .. str
end

local function sendbits(sBits)
	for i = 1, string.len(sBits) do
		redstone.setOutput(sSide,string.sub(sBits,i,i) == "1")
		sleep(0.1)
	end
end

local function toFBits(num,len)
	local num2tab = toBits(num)
	local tab2str = table.concat(num2tab)
	local str2fmt = string_rpad(tab2str,len,"0")
	return str2fmt
end

function open(side)
	sSide = side
end

function close(side)
	sSide = nil
end

function receive()
	if sSide == nil then
		error("No open sides")
	end
	while true do
		if redstone.getInput(sSide) == false then break end
		sleep(0.05)
	end
	while true do
		if redstone.getInput(sSide) == true then break end
		sleep(0.05)
	end
	sleep(0.1)
	local iCID = ""
	for i = 1, 16 do
		iCID = iCID .. (redstone.getInput(sSide) == true and "1" or "0")
		sleep(0.1)
	end
	iCID = tonumber(iCID,2)
	local iSize = ""
	for i = 1, 16 do
		iSize = iSize .. (redstone.getInput(sSide) == true and "1" or "0")
		sleep(0.1)
	end
	iSize = tonumber(iSize,2)
	local sBuffer = ""
	local sMessage = ""
	for i = 1, iSize * 8 do
		sBuffer = sBuffer .. (redstone.getInput(sSide) == true and "1" or "0")
		if string.len(sBuffer) == 8 then
			sMessage = sMessage .. string.char(tonumber(sBuffer,2))
			sBuffer = ""
		end
		sleep(0.1)
	end
	return iCID, sMessage
end

function send(message)
if sSide == nil then
error("No open sides")
end
redstone.setOutput(sSide, true) sleep(0.1)
sendbits(toFBits(os.computerID(),16))
sendbits(toFBits(string.len(message),16))
for i = 1, string.len(message) do
sendbits(toFBits(string.byte(string.sub(message,i,i)),8))
end
redstone.setOutput(sSide, false)
end
immibis #2
Posted 18 March 2013 - 04:56 PM
20 bits/s is possible with a direct WRCBE connection, I don't know if it still works with redstone wire.
gamax92 #3
Posted 18 March 2013 - 06:09 PM
I had to drop it down to 10 bits/s because of redstone wire.
Kept getting random garbage out of it.