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

[Question] [Rednet] Printing Rednet Bradcast

Started by Blueknight1758, 23 November 2012 - 05:05 AM
Blueknight1758 #1
Posted 23 November 2012 - 06:05 AM
Hello, i've been getting into the rednet recently and have found myself in need of printing to the screen exactly what a rednet broadcast says.

For instance PC 1 says
rednet.broadcast("Hello")

and I want PC 2 to do


id = rednet.receive()
if id == 1 then
print("<what rednet.broadcast said>")

how would this be done? I know in this case it would be easier to just do print("hello") but ignore that please.
thanks in advance.
And is it possible to run this at startup
but in the background so the pc is still useable?

Edit: Just realized a spelling mistake in topic title, i dont know how to edit that sorry.
MetalMiner #2
Posted 23 November 2012 - 06:28 AM

id, message = rednet.receive()
print(message)
I think the thing wirh run it in the background isn't possible…
Perhaps someone created a multitasking program, but i don't think so.
Check the programs forum.

Hope i helped!
Blueknight1758 #3
Posted 23 November 2012 - 06:33 AM

id, message = rednet.receive()
print(message)
Doing that gave me an attempt to call nil error, but i don't know where from.

and apparantly according to the wiki the parallel api can be used to multitask.
Dlcruz129 #4
Posted 23 November 2012 - 07:18 AM

id, message = rednet.receive()
print(message)
Doing that gave me an attempt to call nil error, but i don't know where from.

and apparantly according to the wiki the parallel api can be used to multitask.

You don't need the parallel API if you only want to receive. Make sure you are using rednet.recEIve, not rednet.recIEve.

I before E, EXCEPT AFTER C
Grim Reaper #5
Posted 23 November 2012 - 07:21 AM
I'm sure that the code you provided isn't the complete program because it appears that you haven't opened a modem for the rednet traffic to flow through.
Here is a solution I wrote for a program that will listen and print rednet broadcasts in parallel with the shell. If you have any questions about the code I would be happy to explain it to you.



local functionStack = {} -- All of the functions to be executed in parallel.

-- Opens any modem that can be located on a computer.
-- Throws an error if no modem could be found.
function openModem()
        local sides = rs.getSides()
        for index, side in pairs(sides) do
                if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
                        rednet.open(sides[index])
                        return
                end
        end

        error("No modem found to perform rednet reception.")
end

-- Receives broadcasts and prints them upon reception.
-- Uses an infinite loop in order to execute in  parallel with the shell.
function receiveBroadcasts()
        -- Execute in an infinite loop so that the shell does not restart
        -- after every succsefful execution of this function.
        while true do
                local id, message = rednet.receive()
                print(message)
        end
end

-- Add the shell and the broadcast receiving/printing functions to the
-- function stack for parallel execution.
table.insert(functionStack, function() shell.run("shell") end)
table.insert(functionStack, receiveBroadcasts)

-- Open any modem that can be found on the computer, then listen for
-- broadcasts in parallel with the shell.
openModem()
while true do
        parallel.waitForAny(unpack(functionStack))
end
Dlcruz129 #6
Posted 23 November 2012 - 07:25 AM
I'm sure that the code you provided isn't the complete program because it appears that you haven't opened a modem for the rednet traffic to flow through.
Here is a solution I wrote for a program that will listen and print rednet broadcasts in parallel with the shell. If you have any questions about the code I would be happy to explain it to you.



local functionStack = {} -- All of the functions to be executed in parallel.

-- Opens any modem that can be located on a computer.
-- Throws an error if no modem could be found.
function openModem()
		local sides = rs.getSides()
		for index, side in pairs(sides) do
				if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
						rednet.open(sides[index])
						return
				end
		end

		error("No modem found to perform rednet reception.")
end

-- Receives broadcasts and prints them upon reception.
-- Uses an infinite loop in order to execute in  parallel with the shell.
function receiveBroadcasts()
		-- Execute in an infinite loop so that the shell does not restart
		-- after every succsefful execution of this function.
		while true do
				local id, message = rednet.receive()
				print(message)
		end
end

-- Add the shell and the broadcast receiving/printing functions to the
-- function stack for parallel execution.
table.insert(functionStack, function() shell.run("shell") end)
table.insert(functionStack, receiveBroadcasts)

-- Open any modem that can be found on the computer, then listen for
-- broadcasts in parallel with the shell.
openModem()
while true do
		parallel.waitForAny(unpack(functionStack))
end

Why is the parallel function needed though? By the sounds of it, the computer is only supposed to receive and print.
Grim Reaper #7
Posted 23 November 2012 - 07:31 AM
I'm sure that the code you provided isn't the complete program because it appears that you haven't opened a modem for the rednet traffic to flow through.
Here is a solution I wrote for a program that will listen and print rednet broadcasts in parallel with the shell. If you have any questions about the code I would be happy to explain it to you.



local functionStack = {} -- All of the functions to be executed in parallel.

-- Opens any modem that can be located on a computer.
-- Throws an error if no modem could be found.
function openModem()
		local sides = rs.getSides()
		for index, side in pairs(sides) do
				if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
						rednet.open(sides[index])
						return
				end
		end

		error("No modem found to perform rednet reception.")
end

-- Receives broadcasts and prints them upon reception.
-- Uses an infinite loop in order to execute in  parallel with the shell.
function receiveBroadcasts()
		-- Execute in an infinite loop so that the shell does not restart
		-- after every succsefful execution of this function.
		while true do
				local id, message = rednet.receive()
				print(message)
		end
end

-- Add the shell and the broadcast receiving/printing functions to the
-- function stack for parallel execution.
table.insert(functionStack, function() shell.run("shell") end)
table.insert(functionStack, receiveBroadcasts)

-- Open any modem that can be found on the computer, then listen for
-- broadcasts in parallel with the shell.
openModem()
while true do
		parallel.waitForAny(unpack(functionStack))
end

Why is the parallel function needed though? By the sounds of it, the computer is only supposed to receive and print.

In the original topic he asked for it to run in the background:
how would this be done? I know in this case it would be easier to just do print("hello") but ignore that please.
thanks in advance.
And is it possible to run this at startup
but in the background so the pc is still useable?
Blueknight1758 #8
Posted 23 November 2012 - 08:34 AM
Thank you,
And I should of been more clear.
I need it to be in parallel so people can use the PC while it is running this function.
So they would be doing program's here and there and then I could send them a message saying "hey, here's a message"

Also with the code you posted as I'm trying to learn can you say if these observations are correct or if they're wrong correct them?

Local functionStack = {} - does the "{}" need the functions to stack inside it?
What does the index in the open modem function mean or do?

That appears to be it, I think I can get my head around the rest.
Grim Reaper #9
Posted 23 November 2012 - 08:58 AM
Thank you,
And I should of been more clear.
I need it to be in parallel so people can use the PC while it is running this function.
So they would be doing program's here and there and then I could send them a message saying "hey, here's a message"

Also with the code you posted as I'm trying to learn can you say if these observations are correct or if they're wrong correct them?

Local functionStack = {} - does the "{}" need the functions to stack inside it?
What does the index in the open modem function mean or do?

That appears to be it, I think I can get my head around the rest.

The functionStack is stacking functions in the sense that one is on top of each other; top to bottom; one executed after the other. This way, when we
unpack the functions into the parallel call, all of the functions are passed as parameters.
The index in the open modem function references the index of the element within the sides table. For example, sides[1] = "right", or some other side.
Blueknight1758 #10
Posted 23 November 2012 - 09:15 AM
Is there a way to get the cursor position and set the position to (X,Y+1) so the message is on a seperate line to the cursor?
Grim Reaper #11
Posted 23 November 2012 - 09:22 AM
Here is a solution that will achieve what you have asked.
Is there a way to get the cursor position and set the position to (X,Y+1) so the message is on a seperate line to the cursor?



local functionStack = {} -- All of the functions to be executed in parallel.

-- Opens any modem that can be located on a computer.
-- Throws an error if no modem could be found.
function openModem()
        local sides = rs.getSides()
        for index, side in pairs(sides) do
                if peripheral.isPresent(sides[index]) and peripheral.getType(sides[index]) == "modem" then
                        rednet.open(sides[index])
                        return
                end
        end

        error("No modem found to perform rednet reception.")
end

-- Receives broadcasts and prints them upon reception.
-- Uses an infinite loop in order to execute in  parallel with the shell.
function receiveBroadcasts()
        -- Execute in an infinite loop so that the shell does not restart
        -- after every succsefful execution of this function.
        while true do
                local id, message = rednet.receive()
                -- Print the cursor on the previous line or the next one if the
                -- cursor is on the first line.
                local xCursorPos, yCursorPos = term.getCursorPos()
                local screenWidth, screenHeight = term.getSize()

                if yCursorPos == 1 then
                        term.setCursorPos(1, yCursorPos + 1)
                        term.write(string.rep(' ', screenWidth))
                        term.setCursorPos(1, yCursorPos + 1)
                else
                        term.setCursorPos(1, yCursorPos - 1)
                        term.write(string.rep(' ', screenWidth))
                        term.setCursorPos(1, yCursorPos - 1)
                end
                term.write(message)
                -- Reset the cursor position.
                term.setCursorPos(xCursorPos, yCursorPos)
        end
end

-- Add the shell and the broadcast receiving/printing functions to the
-- function stack for parallel execution.
table.insert(functionStack, function() shell.run("shell") end)
table.insert(functionStack, receiveBroadcasts)

-- Open any modem that can be found on the computer, then listen for
-- broadcasts in parallel with the shell.
openModem()
while true do
        parallel.waitForAny(unpack(functionStack))
end
Blueknight1758 #12
Posted 23 November 2012 - 09:50 AM
Here is a solution that will achieve what you have asked.



		while true do
				local id, message = rednet.receive()
				-- Print the cursor on the previous line or the next one if the
				-- cursor is on the first line.
				local xCursorPos, yCursorPos = term.getCursorPos()
				local screenWidth, screenHeight = term.getSize()

				if yCursorPos == 1 then
						term.setCursorPos(1, yCursorPos + 1)
						term.write(string.rep(' ', screenWidth))
						term.setCursorPos(1, yCursorPos + 1)
				else
						term.setCursorPos(1, yCursorPos - 1)
						term.write(string.rep(' ', screenWidth))
						term.setCursorPos(1, yCursorPos - 1)
				end
				term.write(message)
				-- Reset the cursor position.
				term.setCursorPos(xCursorPos, yCursorPos)
		end
end
Works perfectly thanks!