Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

How to know the IP address

JAPIB
2015-06-06
2020-12-01
  • JAPIB

    JAPIB - 2015-06-06

    Hello
    My raspberry get an IP address by a DHCP server. I want to show it on a visualisation page.
    What function block and what library should J use to find the IP address by programme......
    Thank you in advance for your reply

     
  • eschwellinger

    eschwellinger - 2015-06-07

    Hi Jabib,

    use SysSocket lib following codesnippet or check attached Project example

    PROGRAM PLC_PRG
    VAR
       sHostName   : STRING;
       Host : SysSocket.SOCK_HOSTENT;
       pIPAddress  : POINTER TO DWORD;
       in_addr     : INADDR;
       sIPAddress  : STRING := 'aaa.bbb.ccc.ddd';
       sTextEingabe: String;
    END_VAR
    (* Get own hostname. *)
    SysSockGetHostName(szHostName:=sHostName, diNameLen:=SIZEOF(sHostName));
    (* Get own IP address by its name. *)
    SysSockGetHostByName(szHostName:=sHostName,pHost:= ADR(Host));
    pIPAddress := Host.pAddrList[0];// select the adapter of addrList[]
    in_addr.ulAddr := pIPAddress^;
    SysSockInetNtoa(pInAddr := ADR(in_addr),szIPADDR := sIPAddress, diIPAddrSize:=SIZEOF(sIPAddress));
    

    BR
    Edwin

    PiFaceIoDrv_LadderExample_andSysProcess.project [104.28 KiB]

     
    • agea28 - 2020-12-01

      And is this the same way for wlan0 address ?
      Host.pAddrList[0] returns the eth0 address but wlan0 is another adapter of addrList[], no ?

       
  • JAPIB

    JAPIB - 2015-06-10

    Hi Edwin,
    Thank you. Its work well.
    How to discover the possibilitése all those libraries that are not documented in the online help ?
    It lacks a Wikipedia section !

     
  • mike197678 - 2016-11-10

    great,
    and the mac adress?

    thx

     
  • eschwellinger

    eschwellinger - 2016-11-10

    Hi,
    yes even this is possible:
    You need to ad 'CmpSysEthernet' to the LibraryManager
    then then the following code snippet should help.

    
    
    getnumberofadapters(ADR(diNumber));
    FOR iAdapter := 0 TO diNumber DOPROGRAM PLC_PRG
    VAR
       diNumber: DINT;
       iAdapter: DINT;
       stName: STRING(255);
       stDescription: STRING(255);
       aby_address:ARRAY[1..6]OF BYTE;
    END_VAR
       getadapterinfo(
       iAdapterNum := iAdapter, 
       pbName := ADR(stName), 
       pbDescrition := ADR(stDescription), 
       iBuffersize := SIZEOF(stName), 
       iMacLength := 6, 
       sMacAdress := ADR(aby_address));
    END_FOR
    

    BR
    Edwin

    IMG: mac.jpg

     
  • mike197678 - 2016-11-11

    Thank You,

    work perfect.

    Thank you very much

     
  • switcher - 2017-02-14

    Great!

    Is there possible to get a Serial number from CPU?

    Thank You!

    Best regards,
    Zdenko.

     
  • nothinrandom - 2017-06-01

    Great info! So is there a way to combine these two and get the IP address from eth0 always? Thanks

     

Log in to post a comment.