16
Technical support / Small behavioral issue with CHR$(n) handling in Simulator
« on: February 05, 2017, 02:03:41 PM »
The simulator's handling of CHR$(n) does not match the PLC's behavior.
The simulator accepts 16-bit values for n and can create strings that appear to be an array of 16-bit values.
As an example:
A$ = CHR$(&H4548)
Creates a string that is 1 character in length and this only character has a value of &H4548 when run with the simulator.
When run on a PLC this same code creates a string that is 1 character in length and this character has a value of &H48.
I vote that the PLC's handling is correct.
I suspect that the Java library that you are using supports Unicode in some general way (localization) and that the java code that is close to CHR$() will accept 16-bit Unicode values.
I think that you should mask off and only use the least significant 8 bits of the "n" argument to CHR$(n). This would make the simulator behavior match the PLC.
I don't care that the simulator implements strings as arrays of 16-bit integers rather than 8-bit integers as the PLC does. This difference is not important and only shows up because of the implementation of CHR$(n) is flawed.
Gary D*ckinson
The simulator accepts 16-bit values for n and can create strings that appear to be an array of 16-bit values.
As an example:
A$ = CHR$(&H4548)
Creates a string that is 1 character in length and this only character has a value of &H4548 when run with the simulator.
When run on a PLC this same code creates a string that is 1 character in length and this character has a value of &H48.
I vote that the PLC's handling is correct.
I suspect that the Java library that you are using supports Unicode in some general way (localization) and that the java code that is close to CHR$() will accept 16-bit Unicode values.
I think that you should mask off and only use the least significant 8 bits of the "n" argument to CHR$(n). This would make the simulator behavior match the PLC.
I don't care that the simulator implements strings as arrays of 16-bit integers rather than 8-bit integers as the PLC does. This difference is not important and only shows up because of the implementation of CHR$(n) is flawed.
Gary D*ckinson