ChZeman (Customer) asked a question.

Best Way to Split a String (IP Address) Into Bytes for SETUPIP?

I'm requesting JSON data from Node-RED and receiving the IP address the PLC should be using. I'm having trouble figuring out how to convert that string into 4 bytes SETUPIP can use.


  • HOST_franji1 (HOST Engineering)

    Use STR2INT to extract the first octet into D42:UB3, utilizing the "index where stopped" say to D99. This is the 0-based index where the "dot" is after the first octet

    INC D99 will adjust the index past the dot

    Then use STRSUB to get the substring started at D99. You can put it to a new string, or just back into SS2

    Do STR2INT SS2 to put the 2nd octet into D42:UB2 (stop index in D99)

    INC D99

    STRSUB D99 back into SS2

    STR2INT SS2 to put the 3rd octet into D42:UB1 (stop index D99)

    INC D99

    STRSUB D99 back into SS2

    STR2INT SS2 to put the last octet into D42:UB0

     

    You may want to use other string IDs and different D "stop index" for all 4 octets to help debug/understand what's going on.

     

    D42 should now contain the DWORD value for 10.0.5.42.

     

    Do-more PLCs use little endian, so the IP Address will be in that form in the DWORD (0x0A00052A), which is what the PLC firmware expects for an IP Address.

    Expand Post
    Selected as Best
  • HOST_franji1 (HOST Engineering)

    How far have you gotten? Have you used JSONPARSE to extract the string value from the JSON object? That is, you have a string in (say) SS0 equal to the IP ipaddress "10.0.5.42". Or do you need help with the JSONPARSE? If so, please provide a sample Node-RED JSON object/array string.

  • ChZeman (Customer)

    Yes, I'm able to extract the data just fine from JSON and am storing the IP address in SS2. It's what to do with it from there that's giving me problems. I had attempted to use JSONPARSE to store the values in a data block but that provided me with values that were of a different type that I couldn't figure out how to convert from.

  • HOST_franji1 (HOST Engineering)

    Use STR2INT to extract the first octet into D42:UB3, utilizing the "index where stopped" say to D99. This is the 0-based index where the "dot" is after the first octet

    INC D99 will adjust the index past the dot

    Then use STRSUB to get the substring started at D99. You can put it to a new string, or just back into SS2

    Do STR2INT SS2 to put the 2nd octet into D42:UB2 (stop index in D99)

    INC D99

    STRSUB D99 back into SS2

    STR2INT SS2 to put the 3rd octet into D42:UB1 (stop index D99)

    INC D99

    STRSUB D99 back into SS2

    STR2INT SS2 to put the last octet into D42:UB0

     

    You may want to use other string IDs and different D "stop index" for all 4 octets to help debug/understand what's going on.

     

    D42 should now contain the DWORD value for 10.0.5.42.

     

    Do-more PLCs use little endian, so the IP Address will be in that form in the DWORD (0x0A00052A), which is what the PLC firmware expects for an IP Address.

    Expand Post
    Selected as Best
  • ChZeman (Customer)

    Thank you! I had a different solution in mind but this is much easier. 😊