FIRST_LOCAL_PORT use ??
vincen
Posts: 526
Hi,
Just wanted to share an issue and know if someone has a solution I was thinking to use FIRST_LOCAL_PORT keyword in a module that uses IP communication. I thought it would assign dinamycally when system starts first avalaible local port for my IP Communication. In fact it looks it returns all the time 1 so I misunderstood use of that keyword.
Would someone how to tell program to use fist avalaible local port for my IP communication ? I'd like to do that to avoid hassle to pass in parameter of module device used for IP link
Thanks for your ideas, tip, ...
Vinc
Just wanted to share an issue and know if someone has a solution I was thinking to use FIRST_LOCAL_PORT keyword in a module that uses IP communication. I thought it would assign dinamycally when system starts first avalaible local port for my IP Communication. In fact it looks it returns all the time 1 so I misunderstood use of that keyword.
Would someone how to tell program to use fist avalaible local port for my IP communication ? I'd like to do that to avoid hassle to pass in parameter of module device used for IP link
Thanks for your ideas, tip, ...
Vinc
0
Comments
I use first local port alot, the only gotcha I've come across is with the DATA_EVENTs. They should be specified as 0:FIRST_LOCAL_PORT+1:0 (etc etc) Rather than the device name.
So some code : (I wish someone could import the syntax list from Netlinx so I don't have to use PHP highlighting:
[php]
DEFINE_DEVICE
dvIP1 = 0:FIRST_LOCAL_PORT:0
dvIP2 = 0:FIRST_LOCAL_PORT+1:0
dvIP3 = 0:FIRST_LOCAL_PORT+2:0
DEFINE_CONSTANT
CHAR sIP1Address[] = '10.1.1.122'
svPort = 4152 // The IP port to listen to connections on
IP_TCP = 1 // Is it UDP or TCP 1=TCP 2=UDP
DEFINE_START
wait 25
{
IP_CLIENT_OPEN(dvIP1.PORT, sIPAddress, SvPort, IP_TCP)
}
DEFINE_EVENT
DATA_EVENT[0:dvIP1.PORT:0]
{
STRING:
{
// process incoming string (Data.Text)
}
ONERROR:
{
//Do Stuff
}
OFFLINE:
{
//Do More Stuff
}
}
[/php]
You can also use FIRST_LOCAL_PORT in a constant declaration, so PORT1=FIRST_LOCAL_PORT you can use that when doing server initiation. Does this help ? Or have I once again completely missed the point of your question ^_^
You get it to return other 'ports' by just incrementing the FIRST_LOCAL_PORT Keyword, as above.
It would be nice if at compile time the use of FIRST_LOCAL_PORT would sequentially assign numbers dynamically as the code is pulled into the compiler like you want and one would logically think it does but doesn't.
Heh, I'm still trying to get over the call by value V Call by reference pointed out the other day.. That really irritates me, even though I've never run into a problem with it, I've always thought it called it by copying the value, rather than a pointer into the memory.
Slighty annoying.
INTEGER FIRST_LOCAL_PORT = 2; Me either.
Each time you use it in DEFINE_DEVICE it allocates a new port number (a short of DHCP for each local port declared) so it would ease declaration of ports in module
Thanks
Vince
Regarding the NEXT_LOCAL_PORT() discussion and playing devils? advocate, I?m curious as to why defining IP devices should be different than defining any other real or virtual device. We have to keep track of what device IDs are assigned to every real and virtual device, right? We have to keep track of what buttons, channels, and levels are used. What?s different about IP?
Second question first: there is no difference. The concept behind a constant shortcut, however, was that you might not know what was defined and in use by the system. Since they never really went in that direction, it became meaningless. But, for example, if there were variations among the master cards where one type used port one, and another type used 1-3, then FIRST_LOCAL_PORT would be helpful making sure your code was portable without having to worry about it. However, as mentioned, it falls apart because you can't just add to it to get other free ports.
First question: you can define a device inside a module. It doesn't really matter as far as the module is concerned. One problem, though, is keeping track of what device numbers are in use without flipping through multiple files (and forget it if you only have a tko of the module). The other, and I think this is the bigger issue, is if you have multiple modules running the same type of device; if they were hard-coded, they would all to all to the same one. So, I like to pass devices as parameters; it just seems neater.
Right. I know it can be done, I just don?t understand why one would want to or what benefit it serves. Thought I might be missing an angle somewhere. I only see the downsides as you mentioned.
The idea is the following, I hope to be clear in my explanations:
You create a module that do some IP stuffs so you'll need to declare an IP port such as: dvIP = 0:2:0 BUT
if you code that in hard in module, it can go in conflict with a similar definition in main program for something else.
So to avoid hassle to need to pass that device as parameter of module, it would nice to just declare IP port in module such as dvIP = 0:First_Local_Port:0 and system automatically subtitutes Fist_Local_Port by first port avalaible in master at startup.
I know it's probably ask too much just for a little confort but as you know programmers as me are just too lazy sometimes
Vinc