Hi Fabio, I ran into this one a while back and it confused me too, because your logic is fine β an IPv4 address can never be more than 15 characters, so nothing is actually being cut off here.
What SA0026 is really flagging is the sizes the analyser assumes, not the value at runtime. Your strIPAddress is a plain STRING, so it defaults to 80 characters, while the result of that nested CONCAT is treated as a much longer string. The analyser doesn't work out that your four STRING(10) pieces plus three dots can only add up to 43 characters, it just sees a longer string going into a shorter one and warns about possible truncation.
The fix I ended up using was simply to give the target enough room, for example:
strIPAddress : STRING(255);
That cleared it for me straight away. If you want to keep things a bit tighter you could also build the address step by step into the same sized variable instead of nesting all those CONCATs, which is a lot easier to read when you come back to it later β but honestly the declaration change on its own is enough here.
One thing worth doing is trying it in simulation with a few edge cases, like 255.255.255.255 and 0.0.0.0, just to see the string come out as expected before it goes anywhere near live equipment. Don't let a warning like this push you into disabling the check for the whole project, it's actually catching a real class of mistake, just not in this case.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello Everyone,
I am really struggling to understand why codesys would present an error regarding the following:
VAR
strByte1: STRING(10):= '192';
strByte2: STRING(10):= '168';
strByte3: STRING(10):= '1';
strByte4: STRING(10):= '0';
strIPAddress: STRING:= '192.168.1.1';
END_VAR
//Updating Variables
strByte1:= BYTE_TO_STRING(GVL.abyPSsIpAddress[0]);
strByte2:= BYTE_TO_STRING(GVL.abyPSsIpAddress[1]);
strByte3:= BYTE_TO_STRING(GVL.abyPSsIpAddress[2]);
strByte4:= BYTE_TO_STRING(GVL.abyPSsIpAddress[3]);
strIPAddress:= CONCAT(strByte1, CONCAT('.', CONCAT(strByte2, CONCAT('.',
CONCAT(strByte3, CONCAT('.', strByte4))))));
Any help would be much appreciated.
Hi Fabio, I ran into this one a while back and it confused me too, because your logic is fine β an IPv4 address can never be more than 15 characters, so nothing is actually being cut off here.
What SA0026 is really flagging is the sizes the analyser assumes, not the value at runtime. Your strIPAddress is a plain STRING, so it defaults to 80 characters, while the result of that nested CONCAT is treated as a much longer string. The analyser doesn't work out that your four STRING(10) pieces plus three dots can only add up to 43 characters, it just sees a longer string going into a shorter one and warns about possible truncation.
The fix I ended up using was simply to give the target enough room, for example:
That cleared it for me straight away. If you want to keep things a bit tighter you could also build the address step by step into the same sized variable instead of nesting all those CONCATs, which is a lot easier to read when you come back to it later β but honestly the declaration change on its own is enough here.
One thing worth doing is trying it in simulation with a few edge cases, like 255.255.255.255 and 0.0.0.0, just to see the string come out as expected before it goes anywhere near live equipment. Don't let a warning like this push you into disabling the check for the whole project, it's actually catching a real class of mistake, just not in this case.