This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

Receiver never start receiving

Started by FuuuAInfiniteLoop(F.A.I.L), 31 March 2013 - 07:23 AM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 31 March 2013 - 09:23 AM
Im making a redstone rednet(yes i know that i can use modems but its funniest!)

im trying to find why the computer that has run the program receiving, never show the message that says that it its receiving

the code:


look down
Engineer #2
Posted 31 March 2013 - 09:47 AM
The problem lays here:


while true do
  if rs.getOutput(PingReceive) then
   break
  else
   if not received then
	if rs.getOutput(line) then
		 received = "1"
		 term.write("1")
	else
		 received = "0"
		 term.write("0")
	end
   else
	if rs.getOutput(line) then
		 received = received.."1"
		 term.write("1")
	else
		 received = received.."0"
		 term.write("0")
	end
   end
  end
  sleep(0)
end

received is not defined, so its always true. That condition will never hit because of the not, wich makes it false.
I suggest using this:

local received = ""
while true do
	if rs.getOutput(PingReceive) then
		break
	end
	if rs.getOutput(line) then
		received = received .. "1"
		term.write("1")
	elseif not rs.getOutput(line) then
		received = received .. "0"
		term.write("0")
	end
	sleep(0)
end

I think it does what you want. If you have questions about this, ask them! :)/>
Edited on 31 March 2013 - 07:51 AM
FuuuAInfiniteLoop(F.A.I.L) #3
Posted 31 March 2013 - 09:56 AM
The problem lays here:


while true do
  if rs.getOutput(PingReceive) then
   break
  else
   if not received then
	if rs.getOutput(line) then
		 received = "1"
		 term.write("1")
	else
		 received = "0"
		 term.write("0")
	end
   else
	if rs.getOutput(line) then
		 received = received.."1"
		 term.write("1")
	else
		 received = received.."0"
		 term.write("0")
	end
   end
  end
  sleep(0)
end

received is not defined, so its always true. That condition will never hit because of the not, wich makes it false.
I suggest using this:

local received = ""
while true do
	if rs.getOutput(PingReceive) then
		break
	end
	if rs.getOutput(line) then
		received = received .. "1"
		term.write("1")
	elseif not rs.getOutput(line) then
		received = received .. "0"
		term.write("0")
	end
	sleep(0)
end

I think it does what you want. If you have questions about this, ask them! :)/>
still not working….

Im sure that the problem is in

while run do
sleep(0)
if rs.getOutput(PingReceive) then
sleep(1)
run = false
end
end
cause it dont display the ms that is displayed after that ends…

Im was tryiing to resolve the problem, the new code:

line = "back"
PingSend = "front"
PingReceive = "front"
local floor,modf, insert = math.floor,math.modf, table.insert
local char,format,rep = string.char,string.format,string.rep
local function basen(n,B)/>
if n < 0 then
n = -n
end
local t = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~"
if n < b then
local ret = ""
ret = ret..string.sub(t, (n%B)/>+1,(n%B)/>+1)
return ret
else
local tob = tostring(basen(math.floor(n/B)/>, B)/>)
local ret = tob..t:sub((n%B)/>+1,(n%B)/>+1)
return ret
end
end
function toBase(str, base) –Encodes @str in @base
if not base then return nil end
str = tostring(str)
local res = ""
for i = 1, str:len() do
if i == 1 then
res = basen(str:byte(i), base)
else
res = res..":"..basen(str:byte(i), base)
end
end
return res
end
function fromBase(str, base) –Decodes @str from @base
if not base then return nil end
str = tostring(str)
local bytes = seperate(str, ":")
local res = ""
for i = 1, #bytes do
res = res..(string.char(basen(tonumber(bytes, base), 10)))
end
return res
end
function toBinary(str) –Encodes @str in binary
if not str then return nil end
str = tostring(str)
return toBase(str, 2)
end
function fromBinary(str) –Decodes @str from binary
if not str then return nil end
str = tostring(str)
return fromBase(str, 2)
end
function send(tosend)
rs.setOutput(PingSend, true)
sleep(1)
rs.setOutput(PingSend, false)
print("Sending")
for i = 1, #tosend do
local c = tosend:sub(i,i)
sleep(0)
if c=="1" then
rs.setOutput(line, true)
else
rs.setOutput(line, false)
end
end
rs.setOutput(PingSend, true)
rs.setOutput(line, false)
sleep(3)
rs.setOutput(PingSend, false)
end
function receive()
run = true
while run do
sleep(0)
if rs.getOutput(PingReceive) then run = false end
end
print("Receiving")
sleep(1)
local received = ""
while true do
if rs.getOutput(PingReceive) then
break
end
if rs.getOutput(line) then
received = received .. "1"
term.write("1")
elseif not rs.getOutput(line) then
received = received .. "0"
term.write("0")
end
sleep(0)
end
term.clear()
return fromBinary(received)
end
args = {…}
if args[1]=="-send" then
file = fs.open(args[2], "r")
a = toBinary(file.readAll())
file.close()
send(a)
elseif args[1]=="-receive" then
a = receive()
term.clear()
term.write("Name: ")
b = read()
file = fs.open(b, "w")
file.write(a)
file.close()
else
print("-receive | -send filename")
end
FuuuAInfiniteLoop(F.A.I.L) #4
Posted 31 March 2013 - 10:14 AM
bump