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

Help with rednet

Started by Jedifightbad, 18 July 2013 - 06:32 PM
Jedifightbad #1
Posted 18 July 2013 - 08:32 PM
Hello everyone,

So I've got a question about rednet. Basically what I want is to send a message from multiple computers (6 to be exact) to a single computer which is the "brain" of the program. The problem is I need all 6 messages to come to the main computer, preferably in order, and then the collection of all of them to trigger a function on the main computer. I cant figure out how to receive the messages all at once or one by one in order to get all 6 messages to trigger the main computers function. Any help at all would be much appreciated. Thanks.
ZagKalidor #2
Posted 20 July 2013 - 05:38 AM
check out the computercraft wiki - os(API) rednet_message
GravityScore #3
Posted 20 July 2013 - 06:34 AM
What you can do is use a loop to wait for the receival of the 6 messages, and the once that is done, trigger your main function:


-- A variable to store how many messages we've received
local received = 0

-- Loop until received is greater than 6
while received < 6 do
  -- Wait for a rednet message
  local event, id, message = os.pullEvent("rednet_message")

  -- Check if the message is valid
  if message == "Hello there from a computer" then
    -- Add one to the received amount
    received = received + 1
  end
end

-- Main function stuffs here...
albrat #4
Posted 20 July 2013 - 08:03 AM
the problem with the 6 loop method is that one computer could send twice in the time and one computer would be forgotten.

Is the main computer always going to receive messages from the same 6 computers ?

if it is then we can set up our main computer to receive messages from those 6 computers only.


rednet.open("back")
senders = { 2, 3, 4, 5, 6, 7 } -- our valid senders
recmess = {}
cmpmess = {}
loop = true
count = 1
while loop do
  if count &amp;gt; 6 then loop = false break end
	eventmess = {} -- clear table
	eventmess = {os.pullEvent()} -- grab ospull as a table.
	if eventmess[1] == "rednet_message" then
	  for i = 1,#senders do
		if eventmess[2] == senders[i] and recmess[i] ~= true then
		  recmess[i] = true
		  cmpmess[count][1] = eventmess[3] -- set the message in value 1
		  cmpmess[count][2] = eventmess[2] -- set the cmputer id sending in value 2
		  count = count +1
		  end
	   end
   end
end
That little bit of code will grab the event, check if the computer is valid and store the messages in a table… simply retreive the information from the table cmpmess[1][1] for the first computer to send. and cmpmess[6][1] for the last.
cmpmess[1][2] will give the computer ID that sent the message cmpmess[1][1] so you could sort which computer sent which message
due to the lack of sleep() commands this will grab and store as quickly as I could manage.


I wrote theprogram again but improoved it and made it print results
Spoiler
rednet.open("back")
senders = { 2, 3 } -- our valid senders
recmess = {}
cmpmess = {{};{}}
loop = true
count = 1
while loop do
 -- os.startTimer(10)
  if count &amp;gt; table.maxn(senders) then loop = false break end
  eventmess = {} -- clear table
  eventmess = {os.pullEvent()} -- grab ospull as a table.
  if eventmess[1] == "rednet_message" then
    for i = 1,#senders do
	  if eventmess[2] == senders[i] and recmess[i] ~= true then
	    recmess[i] = true
	    cmpmess[count][1] = eventmess[3]
        cmpmess[count][2] = eventmess[2]
	    count = count +1

	  end
    end
  end
end

ordermess = {{};{}}


  for i = 1,#cmpmess do -- count our cmpmessages event though we know it is 6.

    for n = 1,#senders do  -- check our senders

	  if senders[n] == cmpmess[i][2] then  -- if our Sender matches we copy the information to the senders position in our sender table.

	    ordermess[n][2] = cmpmess[i][1]  -- I think this should set the table to the entire table
	    ordermess[n][1] = cmpmess[i][2]
	  end

    end

  end

for i = 1,#senders do
  print(ordermess[i][1])
  print(ordermess[i][2])
end



Edit :: these code tags are not holding the indentations very well in BBcode. on the screen where i typed it… its all neat and tidy. posted and its a jumble of crap indents
Edited on 20 July 2013 - 07:15 AM
0099 #5
Posted 20 July 2013 - 02:59 PM
If you want to get messages in order, you can send some kind of "ping message"

-- on main computer
for i=1,6 do
  rednet.send(senders[i],"") -- blank message means that i'm ready to receive, "senders" is table with ids
  _, result[i] = rednet.receive()
end

-- now you can do anything with messages, they are in "result" table

-- on other computer
function sendToMain(mes)
  while rednet.receive() ~= main do end -- "main" is id of the main computer
  rednet.send(main,mes)
end