Jump to content




Need help with Parallel API


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

#1 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 09 June 2016 - 03:00 PM

If I use this code, is doesent works. How can I make it works? I want to do thinks while the compiuter wait for an signal.
function Empfang()
  a,b,c,d,e = os.pullEvent("modem_message")
end
function Senden()
  modem.transmit(2,2,"Test")
end
function start()
  parallel.waitForAny(Empfang,Senden)
  start()
end
modem = peripheral.wrap("right")
modem.open(2)
start()


#2 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 09 June 2016 - 03:11 PM

Well as your code is structured now, it'll start both Empfang() and Senden(), then Senden() will transmit and exit which will allow parallel.waitForAny to exit as well. You could use parallelWaitForAll, but that still wouldn't change the fact that Senden() would quit after one send and Empfang() would quit after receiving one message. Also your start() function has a recursion problem as it refers to itself - this will cause the program to crash eventually. What you were looking for was an infinite loop of some sort, like this...
function Empfang()
  while true do --# start an infinite loop
    a, b, c, d, e = os.pullEvent("modem_message") --# os.pullEvent yields (allowing other computers to run), so this loop is 'safe'
  end
end

function Senden()
  while true do --# start an infinite loop
    modem.transmit(2, 2, "Test")
    sleep(1) --# if you don't sleep (or yield in some way in a loop) it will crash for failure to yield
  end
end

modem = peripheral.wrap("right")
modem.open(2)
parallel.waitForAny(Empfang, Senden)

Edited by Dog, 09 June 2016 - 03:14 PM.


#3 Wilma456

  • Members
  • 187 posts
  • LocationGermany

Posted 09 June 2016 - 04:18 PM

Thankyou





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users