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

Computer Communication Anomaly

Started by ZallCaTor, 05 November 2015 - 10:24 PM
ZallCaTor #1
Posted 05 November 2015 - 11:24 PM
Here's my setup. Basically, I have two computers, the first is connected to a redstone input, the second to a monitor.
When the first receives a redstone signal it sends a redstone signal, as well as a rednet message to the second computer.
When the second computer receives the message it displays a message on the monitor.
At least, in theory.
Both computers are connected via networking cable and activated wired-modems, so I see no reason why this shouldn't be working.

These programs have no errors, so I don't know where the problem is. But I believe it's most likely in the communication, either the first is not sending, or the second is not receiving.

Here are the codes of the two computers.

First Computer (sender)

rednet.open("left")
while true do
	 if redstone.getInput("right") then
		rednet.broadcast("GenOn") else
		rednet.broadcast("GenOff")
end
while true do
   redstone.setOutput("back", not redstone.getInput("right"))
   os.pullEvent("redstone")
   end
end

Second Computer (Receiver)

monl = peripheral.wrap("left")
monr = peripheral.wrap("right")
mont = peripheral.wrap("top")
monl.setCursorPos(1,1)
monl.write("==RFG==")
rednet.open("back")
while true do
id, msg = rednet.receive()
   if msg == "GenOn" then
   monl.clear()
   monl.setCursorPos(1,2)
   monl.write("Online") else
   monl.write("Offline")
   end
end
Edited on 05 November 2015 - 10:25 PM
Bomb Bloke #2
Posted 05 November 2015 - 11:59 PM
Your first script (with fixed indentation, to make it easier to read):

rednet.open("left")

while true do
	if redstone.getInput("right") then
		rednet.broadcast("GenOn")
	else
		rednet.broadcast("GenOff")
	end
	
	while true do
		redstone.setOutput("back", not redstone.getInput("right"))
		os.pullEvent("redstone")
	end
end

See that second loop, inside the first loop? It'll never end, so the first loop will never get to repeat at all. Which means that you'll be sending exactly one broadcast every time you run the script - you may be sending multiple redstone signals, but the second script isn't rigged to listen to those.

Fixing that, and using the ternary technique described here, we can pack it down to:

rednet.open("left")

while true do
	rednet.broadcast(redstone.getInput("right") and "GenOn" or "GenOff")
	redstone.setOutput("back", not redstone.getInput("right"))
	os.pullEvent("redstone")
end

The second script may encounter a bug with monitors, if you're using CC 1.74 (where parts of them won't render text). Doing this up the top of it should work around that, if so:

local monl = peripheral.wrap("left")
mon1.setTextScale(0.5)
mon1.setTextScale(1)
Edited on 05 November 2015 - 11:01 PM
ZallCaTor #3
Posted 06 November 2015 - 08:06 AM
Aha, that was it. Thank you. I had a feeling it might be a bug of sorts. I found a few more errors in my code after that and was able to fix them. Thanks again, you’re a big help in the learning process. :P/>