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

Rednet receive question

Started by nynoray, 13 July 2012 - 02:17 AM
nynoray #1
Posted 13 July 2012 - 04:17 AM
I am trying to make a program that i can have a computer receiving rednet messages for ever.

It would listen for a message, when it receives one it would have that on screen then listen for another message.

I started doing lua a day ago so I am very bad at it B)/>/>

This probably shows how horrible I am at this but I tried to do the first simple part in a program


rednet.receive()

when I run the program it acts the same running it in the lua program except for one thing… when i receive the message it doesn't display the message :)/>/> (i do have the modem open)

I also tried changing the times for the rednet.receive to (5) and also (10) same thing happened

It would be nice if someone could help me out, I really need to learn this stuff lol.
Grim Reaper #2
Posted 13 July 2012 - 04:44 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!

'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.

For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site.

Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.

Here is some simple code that will accomplish what you're asking for:

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

while true do
	 local function clear() term.clear(); term.setCursorPos(1, 1); end

	 sender, message = rednet.receive();
	 clear();
	 print( message );
end


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

Quick rundown on the code:


rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

These statements would open a modem on the respective side, meaning that if there isn't a modem on the side opened there is no harm done. This allows for flexibility meaning that the user could put their modem on any side they want without having to edit the code.


while true do

This code basically states that while true do whatever we put between this line and the end statement. True will obviously always be true and therefore will run until terminated by the user.



local function clear() term.clear(); term.setCursorPos(1, 1); end

This is a little function for clearing the screen every time a message is received.



sender, message = rednet.receive();

This code here runs the rednet.receive() function with no arguments, meaning there is no timeout and therefore will listen for a message forever, preventing all other program execution.


clear();

This line will call the clear() function that we defined earlier, clearing the screen.


print( message );

This statement will do just that: print the message once received.


end

This statement is required at the 'end' of a loop declaration. It defines the end of all the code that should be executed by the loop. Once the program reaches the 'end' statement it will "loop" back to the beginning of the loop.


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

And finally, this few statements here close all of the modems open, if there wasn't a modem to begin with there is still no harm done. These 'close' statements are not required but are strongly suggested.

Hope I helped! :)/>/>
Lyqyd #3
Posted 13 July 2012 - 05:35 AM

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

This is bad practice. If the names of the sides were to change in the future, this code would magically start throwing errors. A better way to do the same inelegant, brute-force side opening:


for k,v in pairs(redstone.getSides()) do
	rednet.open(v)
end

Or better yet,


local modemFound = false
for k,v in pairs(redstone.getSides()) do
	if peripheral.getType(v) == "modem" then
		rednet.open(v)
		modemFound = true
		break
	end
end
if not modemFound then
	print("No modem found!")
	return
end
Grim Reaper #4
Posted 13 July 2012 - 05:39 AM

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

This is bad practice. If the names of the sides were to change in the future, this code would magically start throwing errors. A better way to do the same inelegant, brute-force side opening:


for k,v in pairs(redstone.getSides()) do
	rednet.open(v)
end

Or better yet,


local modemFound = false
for k,v in pairs(redstone.getSides()) do
	if peripheral.getType(v) == "modem" then
		rednet.open(v)
		modemFound = true
		break
	end
end
if not modemFound then
	print("No modem found!")
	return
end

I understand that the code I had written previously is inefficient, specific, and inapplicable, however it was written for a less experienced user to understand and learn. Thank you for your contribution, but I don't think throwing loops and other logic a beginners way is a good way to teach, rather this would intimidate them.
Lyqyd #5
Posted 13 July 2012 - 05:45 AM
Avoidance and/or misunderstanding of loops accounts for a significant portion of the questions asked here. Perhaps the second example is too advanced, but the first is certainly simple enough to explain. Teach good practices is better than simplifying code unnecessarily.
nynoray #6
Posted 13 July 2012 - 05:45 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!

'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.

For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site.

Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.

Here is some simple code that will accomplish what you're asking for:

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

while true do
	 local function clear() term.clear(); term.setCursorPos(1, 1); end

	 sender, message = rednet.receive();
	 clear();
	 print( message );
end


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

I forgot to mention that I did have the modem on both computers open (fail).

I tested the code and it worked perfectly :)/>/>. I just took out the clear after a message received because im using this as a program for a receiver only computer.
The only thing that i would like is for it to show the id of the sender.
Lyqyd #7
Posted 13 July 2012 - 05:48 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!

'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.

For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site.

Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.

Here is some simple code that will accomplish what you're asking for:

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

while true do
	 local function clear() term.clear(); term.setCursorPos(1, 1); end

	 sender, message = rednet.receive();
	 clear();
	 print( message );
end


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

I forgot to mention that I did have the modem on both computers open (fail).

I tested the code and it worked perfectly :)/>/>. I just took out the clear after a message received because im using this as a program for a receiver only computer.
The only thing that i would like is for it to show the id of the sender.

Change the print line to:


print(sender..": "..message)

This will string together the sender ID, followed by a colon and a space, and then whatever the message was. The two periods together is called the concatenation operator. Basically, it smushes strings together into one string.
Grim Reaper #8
Posted 13 July 2012 - 05:48 AM
All you have to do is edit the current print statement:


rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");
while true do
		 local function clear() term.clear(); term.setCursorPos(1, 1); end
		 sender, message = rednet.receive();
		 clear();
		 print( sender..": "..message );
end

rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

Hope I helped! :)/>/>
nynoray #9
Posted 13 July 2012 - 05:48 AM
Avoidance and/or misunderstanding of loops accounts for a significant portion of the questions asked here. Perhaps the second example is too advanced, but the first is certainly simple enough to explain. Teach good practices is better than simplifying code unnecessarily.
well I have only been using lua for a day, and have no other programming background.
nynoray #10
Posted 13 July 2012 - 05:52 AM
All you have to do is edit the current print statement:


rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");
while true do
		 local function clear() term.clear(); term.setCursorPos(1, 1); end
		 sender, message = rednet.receive();
		 clear();
		 print( sender..": "..message );
end

rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

Hope I helped! :)/>/>

yea that's exactly what i was looking for B)/>/>
nynoray #11
Posted 13 July 2012 - 05:54 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!

'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.

For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site.

Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.

Here is some simple code that will accomplish what you're asking for:

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

while true do
	 local function clear() term.clear(); term.setCursorPos(1, 1); end

	 sender, message = rednet.receive();
	 clear();
	 print( message );
end


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

I forgot to mention that I did have the modem on both computers open (fail).

I tested the code and it worked perfectly :)/>/>. I just took out the clear after a message received because im using this as a program for a receiver only computer.
The only thing that i would like is for it to show the id of the sender.

Change the print line to:


print(sender..": "..message)

This will string together the sender ID, followed by a colon and a space, and then whatever the message was. The two periods together is called the concatenation operator. Basically, it smushes strings together into one string.
Thanks! I really need to learn this but I don't really know where I can. (exept the lua site but i don't know what parts of the tutorial there are the more needed parts for computercraft)
Grim Reaper #12
Posted 13 July 2012 - 05:56 AM
Check out the tutorials section and the wiki. The wiki provides you with a lot of description of each function defined specifically by ComputerCraft. The rest of Lua you can find on the internet :)/>/>

Try going into the Program Library section and reading the source code and trying to figure out what it does and how it works!

Hope I helped! B)/>/>
Lyqyd #13
Posted 13 July 2012 - 05:59 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!

'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.

For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site.

Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.

Here is some simple code that will accomplish what you're asking for:

rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");

while true do
	 local function clear() term.clear(); term.setCursorPos(1, 1); end

	 sender, message = rednet.receive();
	 clear();
	 print( message );
end


rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");

I forgot to mention that I did have the modem on both computers open (fail).

I tested the code and it worked perfectly :)/>/>. I just took out the clear after a message received because im using this as a program for a receiver only computer.
The only thing that i would like is for it to show the id of the sender.

Change the print line to:


print(sender..": "..message)

This will string together the sender ID, followed by a colon and a space, and then whatever the message was. The two periods together is called the concatenation operator. Basically, it smushes strings together into one string.
Thanks! I really need to learn this but I don't really know where I can. (exept the lua site but i don't know what parts of the tutorial there are the more needed parts for computercraft)

Well, the lua manual has basically everything you'll want to know. It's useful to look stuff up in if you kind of know what you need already. For instance, if you knew that the string smushing we just did was called concatenation, you'd look in the manual under concatenation. Knowing what to look up is half the battle when learning new things in programming. The tutorials section of the forums here also has some great stuff for people just starting out.
nynoray #14
Posted 13 July 2012 - 05:59 AM
Check out the tutorials section and the wiki. The wiki provides you with a lot of description of each function defined specifically by ComputerCraft. The rest of Lua you can find on the internet :)/>/>

Try going into the Program Library section and reading the source code and trying to figure out what it does and how it works!

Hope I helped! B)/>/>
Yea, most of my knowledge so far (very little tho) is from looking at coding, but I can't learn from coding i don't understand at all so I should check out the tutorial thread
nynoray #15
Posted 13 July 2012 - 06:03 AM
Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it! 'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself. For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out this tutorial on rednet by one of the Administrators of this site. Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine. Here is some simple code that will accomplish what you're asking for:
 rednet.open("right"); rednet.open("left"); rednet.open("back"); rednet.open("front"); rednet.open("top"); rednet.open("bottom"); while true do local function clear() term.clear(); term.setCursorPos(1, 1); end sender, message = rednet.receive(); clear(); print( message ); end rednet.close("right"); rednet.close("left"); rednet.close("back"); rednet.close("front"); rednet.close("top"); 
I forgot to mention that I did have the modem on both computers open (fail). I tested the code and it worked perfectly :)/>/>. I just took out the clear after a message received because im using this as a program for a receiver only computer. The only thing that i would like is for it to show the id of the sender.
Change the print line to:
 print(sender..": "..message) 
This will string together the sender ID, followed by a colon and a space, and then whatever the message was. The two periods together is called the concatenation operator. Basically, it smushes strings together into one string.
Thanks! I really need to learn this but I don't really know where I can. (exept the lua site but i don't know what parts of the tutorial there are the more needed parts for computercraft)
Well, the lua manual has basically everything you'll want to know. It's useful to look stuff up in if you kind of know what you need already. For instance, if you knew that the string smushing we just did was called concatenation, you'd look in the manual under concatenation. Knowing what to look up is half the battle when learning new things in programming. The tutorials section of the forums here also has some great stuff for people just starting out.
Yea, I was looking at the manual a bit but I didn't get that far in yet. The thing is I wouldn't know to look up concatenation before because I didn't know that existed.
I'd say I'm decent if I can do what I'm trying with the things I know but if I don't know them I would never figure it out because i wouldn't know what to search really