Jump to content




modem.open() and os.pullevent()


4 replies to this topic

#1 m0nstar

  • Members
  • 3 posts
  • LocationUK

Posted 28 July 2016 - 01:03 PM

Hi all.

I've been experimenting with wireless modems to do some cool stuff like turning my Big Reactor on & off and displaying power percentage/reactor stats on a monitor.

My question is this: when I write a program that calls modem.open(3) to listen on channel 3, followed by os.pullEvent('modem_message'), the os.pullEvent gets messages from all wireless modems in my base regardless of what channel they're sending on as opposed to just messages sent on channel 3.

From the documentation I would expect to only get wireless modem messages on channels that I'm listening on, yet all wireless modems receive all messages from all sending modems? Have I misunderstood something here? If I have, what's the point of calling to listen on a given channel when all messages appear to be broadcasts?

Thanks.

#2 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 28 July 2016 - 02:59 PM

can you show us your code?

#3 m0nstar

  • Members
  • 3 posts
  • LocationUK

Posted 28 July 2016 - 03:55 PM

Sure. Snippets here for brevity, and full pastebins at the bottom.

cube_power_monitor
works out the % storage of a bunch of resonant cells. Transmits status to to chanel 1. Transmits reactor control to chanel 3:

local status_channel = 1																			  
local reactor_control_channel = 3
local engine_control_channel = 4
local modem = peripheral.wrap('top')
local wlan = peripheral.wrap('back')
.....
wlan.transmit(status_channel,10,"power_percent="..percent)
.....
function reactor_on()
  print('Turning reactor on!')
  wlan.transmit(reactor_control_channel,10,"REACTOR_ON")
end
....

reactor_status_monitor
monitors reactor stats. logs them to channel 2 to be picked up by another program connected to a display.
local status_channel = 2

local wlan = peripheral.wrap('right')
local modem = peripheral.wrap('top')

function logtransmit(x)
  print(x)
  wlan.transmit(status_channel,12,x)
end
....
logtransmit("heat="..heat)
logtransmit("fueltick="..fueltick)
logtransmit("rftick="..rftick)
...

Reactor control
turns Big reactor on/off based on signals received on channel 3 from cube_power_monitor
local wlan_channel = 3

local wlan = peripheral.wrap('right')
local modem = peripheral.wrap('top')
...
wlan.open(wlan_channel)
...
while true do
  local event, modemSide, senderChan, replyChan, msg, senderDist = os.pullEvent("modem_message")
  print("MSG: "..msg)

  if string.match(msg,"REACTOR_ON") then
	print("REACTOR_ON")																				
	reactor.setActive(true)
  end
....

Now, this "print("MSG: "..msg)" displays ALL messages being sent on all channels. In fact, if I write a quick program like this:

foobar=peripheral.wrap('right')
foobar.open(6000) -- completely unused channel
while true do
  local event, modemSide, senderChan, replyChan, msg, senderDist = os.pullEvent("modem_message")
  print("MSG: "..msg)
end

It will spit out all the modem.transmit chatter occuring from the above programs.


Am I incorrect in assuming that os.pullEvent("modem_message") will ONLY receive events sent to a given channelX previously opened by modem.open(channelX), because this is not what I am seeing. os.pullEvent("modem_message") is seeing ALL events, regardless of channel.

Disclaimer: These are my first LUA programs for Computercraft and they work great for me, I just don't understand why I am seeing this behaviour.

Using Feed The Beast Infinity Evolved 2.5.0 (computercraft 1.75)

Programs in full:

base_status_display http://pastebin.com/7iRjqyT1
cube_power_monitor http://pastebin.com/pAscYVRx
reactor_remote_control_server http://pastebin.com/aKeHnPWz
reactor_status_monitor http://pastebin.com/YQEKS5kq

Edited by m0nstar, 29 July 2016 - 02:35 PM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 04 August 2016 - 05:34 AM

View Postm0nstar, on 28 July 2016 - 03:55 PM, said:

Am I incorrect in assuming that os.pullEvent("modem_message") will ONLY receive events sent to a given channelX previously opened by modem.open(channelX), because this is not what I am seeing. os.pullEvent("modem_message") is seeing ALL events, regardless of channel.

I dunno about "will", but that's certainly how it "should" be. I'm reasonably sure it functions as expected in later CC builds, though I'll recheck later on today.

Given that it's not working as expected for you, the easiest workaround would be to make use of the protocol system in the rednet API.

Edit: Testing under CC 1.75 myself, modems are working as they should. If I were you I'd be looking at the other mods you've got in use - think about anything in your list which might interact with ComputerCraft in any way, and experiment in disabling those extra mods. If you find a culprit, check with the author re an update or somesuch.

Edited by Bomb Bloke, 06 August 2016 - 02:04 PM.


#5 m0nstar

  • Members
  • 3 posts
  • LocationUK

Posted 16 August 2016 - 12:20 PM

Hi

Thanks for the reply (I've been on holiday). It turned out that I had one computer that had "gone rogue" and was apparently broadcasting its messages. My problem went away when I restarted a running program after modifying it, and it has not recurred.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users