Home AMX User Forum NetLinx Studio

FIRST_LOCAL_PORT use ??

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

Comments

  • travtrav Posts: 188
    Hi Vincen,

    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.
  • viningvining Posts: 4,368
    I personally don't see any benefit to the use of FIRST_LOCAL_PORT. I keep track of all ports being used in my main file, although most are commented out and acutally declared in the include file that I use to initialize the module and make all declarations and then past to the module.

    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.
  • travtrav Posts: 188
    Yeah, I have to say when I came across the keyword, I thought it would be alot more useful than it actually is.

    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.
  • Joe HebertJoe Hebert Posts: 2,159
    vincen wrote:
    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.
    FIRST_LOCAL_PORT is simply a constant defined in Netlinx.axi
    INTEGER FIRST_LOCAL_PORT = 2;
    vining wrote:
    I personally don't see any benefit to the use of FIRST_LOCAL_PORT.
    Me either.
  • DHawthorneDHawthorne Posts: 4,584
    I believe the original idea was that the designers thought they may need to use ports beyond port 1 for internal functions, and they would just update FIRST_LOCAL_PORT to reflect that. It never happened, so it's always resolved to 2. But even if it had, it's usefulness was limited because you couldn't define such things as DEV dvMyPort = 0:FIRST_LOCAL_PORT + 1:0. You can't do arithmetic in a definition like that. So I think I may have used it once or twice in the beginning, but since abandoned it.
  • vincenvincen Posts: 526
    Yep that's why I dream if a developer at AMX follows that thread that FIRST_LOCAL_PORT works that way:

    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
  • Joe HebertJoe Hebert Posts: 2,159
    vincen wrote:
    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 ;)
    Are you DEFINE_DEVICEing in a module? I?ve always passed in the devices I want a module to use. I don?t think I?ve ever used DEFINE_DEVICE inside a module. Maybe I?ve been missing something?

    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?
  • DHawthorneDHawthorne Posts: 4,584
    Joe Hebert wrote:
    Are you DEFINE_DEVICEing in a module? I?ve always passed in the devices I want a module to use. I don?t think I?ve ever used DEFINE_DEVICE inside a module. Maybe I?ve been missing something?

    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.
  • Joe HebertJoe Hebert Posts: 2,159
    DHawthorne wrote:
    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.
    Point well taken if FIRST_LOCAL_PORT was a function and not the constant that it is. Following that train of thought I can?t help but think of FIRST_SERIAL_PORT(), FIRST_RELAY_PORT(), FIRST_IR_PORT(), FIRST_IO_PORT() :)
    DHawthorne wrote:
    you can define a device inside a module. It doesn't really matter as far as the module is concerned?
    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.
  • vincenvincen Posts: 526
    Joe Hebert wrote:
    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?

    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 :D

    Vinc
Sign In or Register to comment.