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

Rednet Problem

Started by unobtanium, 23 May 2013 - 03:27 PM
unobtanium #1
Posted 23 May 2013 - 05:27 PM
Hello pros :D/>

Need your help. Sitting here and want to finish an allmost finished and written project, but i am having trouble with the rednet :s

I am having a computer and a turtle. My goal is to let them communicate with no risk of failing, which means that i want to let them know about each other if they received the message or not.
I tried these functions:

TURTLE

computerID = 3
turtleID = 4
function receiveMessage()
while true do
  local senderChannel, msg = rednet.receive()
  if senderChannel == computerID then
   rednet.send(turtleID,"successful")
   return msg
  end
end
end
print(receiveMessage())

COMPUTER

computerID = 3
turtleID = 4
function sendMessage(sendingmessage)
while true do
  rednet.send(computerID,sendingmessage)
  local senderChannel, msg = rednet.receive(2)
  if senderChannel == turtleID then
   return msg
  end
end
end
print(sendMessage("Hello"))

The computer sends a rednet message to the turtle. The turtle might work and cant receive a message until it finished its job. That means the computer has to send the message (over and over again) until the turtle received it.
If the turtle is receiving it, it sends a rednetmessage back ("successful").
The computer waits for this message and if it doesnt appear after a given amount of time it sends the message again. If the computer gets a message it is fine and exits the function.

If i test this now, the turtle spits out "Hello", but the computer doesnt care about the safety message and keeps going and sends "Hello" over rednet. What i am doing wrong?
Are there even better ways to do this…

Thank you
unobtanium
Spongy141 #2
Posted 24 May 2013 - 10:10 AM
Try this for your computer code.


computerID = 3
turtleID = 4
function sendMessage(sendingmessage)
    while true do
        repeat
            event, id, msg = os.pullEvent()
        until event == 'rednet_message'
        if id == turtleID then
            print(msg)
            return
        end
    end
end
sendMessage('Hello')
it waits until it receives a message, then it compares the ID to the turtle ID, then it prints the message before it exits the program. Hope I helped. Also I do not think doing
 id, msg = rednet.receive(2) 
will wait 2 seconds before, so that might be your error. Try using an alarm instead.