Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
BasedPythonScrub

Multiple Modem Signals (again)

Question

Hello,

I need help with seperating modem signals from multiple slave computers on the master computer. I'll attach an image of my current code.

Currently it isn't consistent and I want to add more slaves to the system. Also I know in the image in the code, the syntax of the modem information isn't consistent (the c012, a32a, etc).

image.thumb.png.12da1ee8588f963deb808857ceba16ea.png

 

Hopefully the code and the very brief description illustrates what my problem is and I know I've posted something similar before on my profile but still need help.

Many Thanks :)

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 1
2 hours ago, BasedPythonScrub said:

Can I print both of the incoming messages at the same time with these methods? Using the while loop allows the for recieved information, from the slaves, to be updated as soon as a packet arrives and I want to display, I will produce a GUI later, multiple bits of information on the screen at once that update in real time.

Could I use table.insert and print the table with what I recieve? e.g:

 

if addr == "address" then

  table.insert(table1, 1)

elseif addr == "address" then

table.insert(table1, 2)

print(table1)

Sure, you can do this in multiple ways. You can put the messages into a table, or if you have a given number of slaves, you can have separate variables for this.

local event = require "event"

local reactor1, reactor2 = "", ""		-- # initialize both with an empty string

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addr == "address slave1" then
    reactor1 = message
  elseif addr == "address slave 2" then
    reactor2 = message
  end
  print(string.format("Reactor 1: %s\nReactor 2: %s", reactor1, reactor2))
end

This will print the reactor status and will update in real time.

Keep in mind that the first time it will print the text with only one message, since the second message hasn't been received yet.

Also, you need to add the actual addresses of the modems in the if statement (replacing "address slave1/2" is)

Share this post


Link to post
Share on other sites
  • 1

The inconsistency comes from you pulling events twice and storing them to separate variables. That can mess up the different senders in your network.

So what you need to do is pull an event once, check the sender's address and then perform some tasks based on the addresses.

local event = require "event"

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addr == "one address" then
    -- # do some stuff
  elseif addr == "the other address" then
    -- # do some other stuff
    -- # you can add as many addresses in elseif statements as you want
  end
end

Or you use a a table to map functions to addresses.

local event = require "event"

local addresses = {
  ["address1"] = function (msg)
    -- # do stuff
  end,
  ["address2"] = function (msg)
    -- # do other stuff
  end
  -- # you can add as many addresses and functions as you want
}

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addresses[addr] then
    addresses[addr](message)
  end
end

 

Share this post


Link to post
Share on other sites
  • 0
3 hours ago, CptMercury said:

The inconsistency comes from you pulling events twice and storing them to separate variables. That can mess up the different senders in your network.

So what you need to do is pull an event once, check the sender's address and then perform some tasks based on the addresses.


local event = require "event"

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addr == "one address" then
    -- # do some stuff
  elseif addr == "the other address" then
    -- # do some other stuff
    -- # you can add as many addresses in elseif statements as you want
  end
end

Or you use a a table to map functions to addresses.


local event = require "event"

local addresses = {
  ["address1"] = function (msg)
    -- # do stuff
  end,
  ["address2"] = function (msg)
    -- # do other stuff
  end
  -- # you can add as many addresses and functions as you want
}

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addresses[addr] then
    addresses[addr](message)
  end
end

 

Thanks for the reply.

I will try this when I hop on and get back to you :)

Share this post


Link to post
Share on other sites
  • 0
5 hours ago, CptMercury said:

The inconsistency comes from you pulling events twice and storing them to separate variables. That can mess up the different senders in your network.

So what you need to do is pull an event once, check the sender's address and then perform some tasks based on the addresses.


local event = require "event"

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addr == "one address" then
    -- # do some stuff
  elseif addr == "the other address" then
    -- # do some other stuff
    -- # you can add as many addresses in elseif statements as you want
  end
end

Or you use a a table to map functions to addresses.


local event = require "event"

local addresses = {
  ["address1"] = function (msg)
    -- # do stuff
  end,
  ["address2"] = function (msg)
    -- # do other stuff
  end
  -- # you can add as many addresses and functions as you want
}

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addresses[addr] then
    addresses[addr](message)
  end
end

 

Can I print both of the incoming messages at the same time with these methods? Using the while loop allows the for recieved information, from the slaves, to be updated as soon as a packet arrives and I want to display, I will produce a GUI later, multiple bits of information on the screen at once that update in real time.

Could I use table.insert and print the table with what I recieve? e.g:

 

if addr == "address" then

  table.insert(table1, 1)

elseif addr == "address" then

table.insert(table1, 2)

print(table1)

Share this post


Link to post
Share on other sites
  • 0
2 hours ago, CptMercury said:

Sure, you can do this in multiple ways. You can put the messages into a table, or if you have a given number of slaves, you can have separate variables for this.


local event = require "event"

local reactor1, reactor2 = "", ""		-- # initialize both with an empty string

while true do
  local _, _, addr, port, _, message = event.pull("modem_message")
  if addr == "address slave1" then
    reactor1 = message
  elseif addr == "address slave 2" then
    reactor2 = message
  end
  print(string.format("Reactor 1: %s\nReactor 2: %s", reactor1, reactor2))
end

This will print the reactor status and will update in real time.

Keep in mind that the first time it will print the text with only one message, since the second message hasn't been received yet.

Also, you need to add the actual addresses of the modems in the if statement (replacing "address slave1/2" is)

Goddamn you're the best :):). It seems pretty consistent which is really nice.  

 

I assume its expandable by adding more strings as well as more elseif statements.

 

What does "%s" do?

Also, do you think I could use one port, to keep the code tidy, since the slaves aren't listening to the broadcast?

 

Thanks :) !!

Share this post


Link to post
Share on other sites
  • 0
9 hours ago, BasedPythonScrub said:

Goddamn you're the best :):). It seems pretty consistent which is really nice.  

Thanks :) I'm glad I could help.

9 hours ago, BasedPythonScrub said:

I assume its expandable by adding more strings as well as more elseif statements.

Yes, you can add as many elseif statements with other slaves. But if you have a lot, then you might think about other solutions like using a table, as I mentioned above. This then has better readability and better performance.

9 hours ago, BasedPythonScrub said:

What does "%s" do?

The string containing "%s" is a format string. The "%s" is a place holder and at that position a string is filled in. There are multiple types of placeholders, for example "%d" and "%f", which are placeholders for integers and floats. You can read more on that at http://www.lua.org/manual/5.3/manual.html#pdf-string.format and https://en.wikipedia.org/wiki/Printf_format_string.

9 hours ago, BasedPythonScrub said:

Also, do you think I could use one port, to keep the code tidy, since the slaves aren't listening to the broadcast?

Yeah, this is totally possible since you distinguish the slaves by their addresses, so use only 1 port. 

Share this post


Link to post
Share on other sites
  • 0
5 hours ago, CptMercury said:

Thanks :) I'm glad I could help.

Yes, you can add as many elseif statements with other slaves. But if you have a lot, then you might think about other solutions like using a table, as I mentioned above. This then has better readability and better performance.

The string containing "%s" is a format string. The "%s" is a place holder and at that position a string is filled in. There are multiple types of placeholders, for example "%d" and "%f", which are placeholders for integers and floats. You can read more on that at http://www.lua.org/manual/5.3/manual.html#pdf-string.format and https://en.wikipedia.org/wiki/Printf_format_string.

Yeah, this is totally possible since you distinguish the slaves by their addresses, so use only 1 port.

Alright neat.

Thanks again :)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.