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

check to see if modem is open/can send messages?

Started by subzero22, 14 March 2013 - 03:15 AM
subzero22 #1
Posted 14 March 2013 - 04:15 AM
Ok I got a small problem. When ever I have the program check to see if a port is open, I guess ports can be open even if the modem isn't connected so it always comes back as true. I want it to be able to tell if it is able to receive messages or at the very least see if a modem and port is open.

Also I know it's possible but how do you make it so that it will open all sides or check each side to see if the modem is there then open that sides port as this code I made works but if the modem is on another side I have to manually change it lol.

I'm also not always able to look at the monitor so I don't want the messages automatically cleared. Is there a guide on making a program do something when you press a button as I can't seem to find one or even know what it's called lol.

The last thing is I can't get the write() to do all 3 things in one line. I know there's a way to do something like write(ID, ": ", message) or even with print but every time I try it with , or . like it has it in the wiki it doesn't work for me.


term.clear()
rednet.open("back")
textutils.slowPrint ("rednet is starting...")
sleep (2)
local open = rednet.isOpen("back")
  if open == true then
   print("rednet started")
   sleep (0.5)
   print("now recieving messages")
   sleep (0.5)
	while true do
	 local ID, message, distance = rednet.receive()
	 write(ID)
	 write(": ")
	 write(message)
	 print()
	end
  else
   print("rednet failed to start")
   print("check modem and start again")
end
remiX #2
Posted 14 March 2013 - 04:39 AM
I'm not sure about the part to check if the rednet is open ( but does it matter? :P/> ) but the write part you can do this:

print( ID .. ': ' .. message )

Use .. to concatenate strings together.
subzero22 #3
Posted 14 March 2013 - 05:45 AM
ah ok thanks. ya sometimes I forget to put a modem on so i wanted a check for it. apperantly even with no modem on you can still open the ports lol.
Lyqyd #4
Posted 14 March 2013 - 06:13 AM
What version of ComputerCraft are you using?
subzero22 #5
Posted 14 March 2013 - 08:34 AM
1.4 I'd use 1.5 but the server I'm playing on isn't updated :(/>

well I went to look to make sure and it says 1.481
Lyqyd #6
Posted 14 March 2013 - 08:39 AM
Have you tried using rednet.isOpen() to check the status of the sides? You can also check to see if a modem is present with peripheral.getType(), which will return "modem" if a modem is on the side you specify.
subzero22 #7
Posted 14 March 2013 - 09:46 AM
Have you tried using rednet.isOpen() to check the status of the sides? You can also check to see if a modem is present with peripheral.getType(), which will return "modem" if a modem is on the side you specify.

Yes I have. If you look at my code I have rednet.isOpen("back") also like I said for some reason even if a modem is not connected to it you can still open the port on the side. With the peripheral.getType() what would I put to check if it's a modem or not?
GopherAtl #8
Posted 14 March 2013 - 09:56 AM
peripheral.getType(side) returns the type, as a string, of the device on that side. In this case, you want it to == "modem".