Change port at run time
logantv
Posts: 31
I'm 99.99% sure the answer is just going to be "you can't do that because it's in the compiler", but is it possible to change a define_device port number programmaticaly? I want to be able to dev_info the controller and set the dvRelay port based on the reply.
My workaround would be to define all of the options and then switch/case the commands later, unless anyone has a better idea. Thanks in advance.
0
Comments
You don't have to define devices in the DEFINE_DEVICE section. You can define them in the DEFINE_CONSTANT and DEFINE_VARIABLE sections as well. The variable type is DEV. For example:
DEFINE_VARIABLE
DEV dvRelays = 5001:21:0
And then later you can define that variable with the standard D:P:S and it will work just like an object defined in the DEFINE_DEVICE section. JUST BE AWARE that if you do something like this and have a DATA_EVENT or BUTTON_EVENT or any type of event that uses a device variable and you change it, you MUST call REBUILD_EVENT, otherwise the event table will still be using the old device DPS. This isn't really a problem with relays, but just wanted you to know.
Awesome, thanks!!
Just be aware that there are some required internal processes enabled with DEFINE_DEVICE that don't exist within the DEFINE_CONSTANT or DEFINE_VARIABLE sections. Not so much an issue with real NetLinx devices connected locally but something to watch out for with master to master real devices and any virtual device.
This is only pertinent to devices referenced as event parameters (ie, the device in a channel event), where the event tables mentioned above are affected. For controlled devices, as long as the address can be resolved locally or across a master to master connection the d:p:s can be referenced in any manner (constant, variable, literal, hybrid).
So I went searching for this again and found my own question. At least I'm consistent?
Ian thank you for adding the additional info, and that's actually what my question is about this time around.
I have a bunch of very similar rooms but they're a mix of NI-700, NI-2100-4100, and NX masters. I'm going to attempt to dynamically assign an I/O(input) port and an IR(serial) port on every system. From what you described I feel confident in the IR(serial), but the I/O kinda falls between your scenarios. Is it ok because it's a local port, or will it be an issue because I'm referencing it as an event parameter and I'll be watching for PUSH/RELEASE?
Thanks in advance for any help on this.
I didn't think I'd be able to test it out while I'm working from home, but I was able to answer my own question. It works great with the IR ports, relays, and IO (input).
Simplified example:
define_variable
dev_info_struct MasterInfo;
volatile integer nMasterDeviceID;
dev dvRelay;
dev dvIO;
dev dvIR;
data_event[dvMaster]
{
online:
{
device_info(dvMaster, MasterInfo);
nMasterDeviceID= MasterInfo.DEVICE_ID;
switch(nMasterDeviceID)
{
case 298: //NI-700
{
dvIR = 5001:3:0;
dvIO = 5001:4:0;
}
case 299: //NI-2100,3100,4100
{
dvRelay = 5001:4:0;
dvIR = 5001:5:0; //5-8
dvIO = 5001:9:0;
}
case 396: //NX
{
dvIR = 5001:11:0; //11-18
dvRelay = 5001:21:0;
dvIO = 5001:22:0;
}
}
}
As a correction to my previous post NI-2100,NI-3100, and NI-4100 all show up as 299, but the ports aren't the same. Ports 4,5,9 from the example only match up with the 2100.
It turns out I do have another question about this though, and I don't think I'll answer it myself:
Is it possible to change a device programatically but also use it with a module(.axs)?
Debugger shows the correct d:p:s, and commands from the main program to the module are making it to the devices, but I lose all Rx from the device. I can't find a way to get define_module to run after define_event, so I'm assuming the port to module mapping is locked in at define_module(), and rebuild_event() isn't updating it. Is there a correct way to handle this? Thanks in advance and I can post a simplified example if my explanation is only making sense to me
Interesting idea.
Maybe this?
Nice, I hadn't thought of trying them individually like that. I was stuck on trying to get the devices defined before the modules. I'll update after I get a chance to try it.