Jump to content




Help with rednet


  • You cannot reply to this topic
4 replies to this topic

#1 Jedifightbad

  • New Members
  • 1 posts

Posted 18 July 2013 - 07: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.

#2 ZagKalidor

  • Members
  • 111 posts

Posted 20 July 2013 - 04:38 AM

check out the computercraft wiki - os(API) rednet_message

#3 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 20 July 2013 - 05: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...


#4 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 20 July 2013 - 07: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 &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


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 by albrat, 20 July 2013 - 08:15 AM.


#5 0099

  • Members
  • 52 posts

Posted 20 July 2013 - 01: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users