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

Programming Woes with Rednet and Monitors

Started by Dorktoid, 01 December 2013 - 01:59 PM
Dorktoid #1
Posted 01 December 2013 - 02:59 PM
Hello! I'm building a home on a server, and I'm trying to heavily incorporate Computercraft into it. My current project is a lock for my front door that has other players type in their username to open the door. That username then is sent to a computer in my control room via rednet, where the username is displayed both on the computer and on a monitor. The last five or so, maybe even ten, usernames to open the front door are always displayed on the machines.
So far, I have successfully programmed the lock for the door and sending the message. However I am having problems with the control room computer receiving the message and the message being displayed. The receiving computer's sole purpose will be receiving the message, checking to see if the message came from the front door computer, and if yes, displaying it. The modem is on the top of the computer, the monitor is on the right, and the front door computer's ID is 79. The receiving computer's startup file looks like this:

term.clear()
term.setCursorPos(1,1)
rednet.open("top")
monitor = peripheral.wrap("right")
monitor.write("Front door access log:")
print("Front Door Access Log")
id, msg, dis = rednet.receive()
if id == 79 then
	  print(msg)
	  monitor.write(msg)
end
When I start up the computer, it prints on the screen and the monitor "Front door access log", like I want it to. However, the computer's display does not have a cursor. It will do nothing whatsoever unless I hold Ctrl+T. When the front door computer sends a message, the receiving computer's display skips a line and then the > appears with a cursor. The monitor does nothing.
What am I missing or doing wrong?

Thanks much,
Dorktoid
Bomb Bloke #2
Posted 01 December 2013 - 04:58 PM
"rednet.receive()" causes your program to yield ("pause") until a rednet message comes in - you indeed won't see much of anything happening while it's waiting.

Once your program HAS that message, it attempts to print it, but it appears that your client code is sending a blank string - in any case, your server prints what it gets and then the program ends (as it's got nothing left to do).

Wrapping the bottom bit in a "while" loop would have it wait for messages indefinately:

while true do
  id, msg, dis = rednet.receive()
  if id == 79 then
    print(msg)
    monitor.write(msg)  -- Note that unlike "print", "write" does NOT automatically move down to the next line.
  end
end

To comment further on why the message is blank, you'd need to show the sending computer's code.
Dorktoid #3
Posted 02 December 2013 - 10:45 AM
After combing through the sending computer's code, I saw that I hadn't put quotation marks around the messages I wanted to send. That would be why the receiver was getting blank messages. Thanks for suggesting that. I added the quotation marks, as well as taking your advice and making a loop on the receiver, and now the computer receives and prints the messages beautifully. The only problem now is the monitor. When I use "monitor.print", the computer comes up with "attempt to call nil", telling me the command does not exist. "monitor.write" works, but only the "Front door access log" at the beginning is displayed - the rednet messages are not, even though the computer is printing them.
apemanzilla #4
Posted 02 December 2013 - 12:38 PM
After combing through the sending computer's code, I saw that I hadn't put quotation marks around the messages I wanted to send. That would be why the receiver was getting blank messages. Thanks for suggesting that. I added the quotation marks, as well as taking your advice and making a loop on the receiver, and now the computer receives and prints the messages beautifully. The only problem now is the monitor. When I use "monitor.print", the computer comes up with "attempt to call nil", telling me the command does not exist. "monitor.write" works, but only the "Front door access log" at the beginning is displayed - the rednet messages are not, even though the computer is printing them.
The monitor does not have a print function, although it is possible to make one. Try adding this snippet of code right below wrapping the monitor as a peripheral.

local monitor.print = function(text)
  local maxx, maxy = monitor.getSize()
  local x,y = monitor.getCursorPos()
  if y == maxy then
    monitor.scroll(1)
  end
  monitor.write(text)
  monitor.setCursorPos(x,y+1)
end
This function should work similarly to the standard print(), but on the wrapped monitor.
I wrote this on my iPad and haven't tested it, but it should work fine.
Dorktoid #5
Posted 02 December 2013 - 11:21 PM
I've added that code right below where I wrap the monitor. It says that there is an "unexpected symbol" on line 5. Line 5 is

local monitor.print = function(text)
I don't see where it's getting an unexpected symbol in there. I even tried replacing that with

function monitor.print()
and keeping the rest of the code the same. That removed the "unexpected symbol" error. However, when I changed the "monitor.write(msg)" command in the receiving loop to "monitor.print()", it again said "attempt to call nil", showing that the function I'm trying to write ("monitor.print()") is not being recognized.