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

modem.open() and os.pullevent()

Started by m0nstar, 28 July 2016 - 12:03 PM
m0nstar #1
Posted 28 July 2016 - 02: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.
Lupus590 #2
Posted 28 July 2016 - 03:59 PM
can you show us your code?
m0nstar #3
Posted 28 July 2016 - 04: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 on 29 July 2016 - 01:35 PM
Bomb Bloke #4
Posted 04 August 2016 - 06:34 AM
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 on 06 August 2016 - 01:04 PM
m0nstar #5
Posted 16 August 2016 - 01: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.