Using "localhost" address

alonohev
2019-04-28
2019-05-07
  • alonohev - 2019-04-28

    P.S.
    Would it be easier if I wrote the specific IP address in an *.XML file and read it from there?
    If so, do codesys has an XML library?
    Thanks

     
  • Lo5tNet - 2019-04-29

    Could you just use the loopback address 127.0.0.1?

     
  • dFx

    dFx - 2019-05-07

    Best is to use loopback, but if you need to know your device IP, you could either use this :

    // Gets specific interface IP address
    FUNCTION GetOwnIp : STRING(15) // Returns IP address from selected adapter. Need SysSocket library
    VAR_INPUT
       IndexOfAdapter : INT := 0; // Index of the ethernet adapter to get IP from (base 0) 
    END_VAR
    VAR
       sHostName   : STRING; // Own hostname
       Host : SysSocket.SOCK_HOSTENT; // Own host description
       pIPAddress  : POINTER TO DWORD; // Pointer to INADDR containing IP address
       in_addr     : INADDR; // Strong typed INADDR containing IP address
       sIPAddress  : STRING; // IP address in string format
    END_VAR
    (* Get own hostname. *)
    SysSockGetHostName(szHostName:=sHostName, diNameLen:=SIZEOF(sHostName));
    (* Get own IP address by its name. *)
    SysSockGetHostByName(szHostName:=sHostName,pHost:= ADR(Host));
    (* Get adress of first adapter *)
    pIPAddress := Host.pAddrList[IndexOfAdapter];
    (* Cast pointer data to INADDR type *)
    in_addr.ulAddr := pIPAddress^;
    (* Get string format *)
    SysSockInetNtoa(pInAddr := ADR(in_addr),szIPADDR := sIPAddress, diIPAddrSize:=SIZEOF(sIPAddress));
    (* Function return *)
    GetOwnIp:=sIPAddress;
    
     

Log in to post a comment.