When I copy a short array into a longer array, I get a “Variable out of bounds” error, even when the length of the longer array is not exceeded. I am using Campbell’s "new" array notation to do this, perhaps incorrectly.
I wouldn’t care, except that “Variable out of bounds” is also used when a Modbus master asks for registers on the slave (my CR1000x) that don’t exist. So later, in the field, it may make troubleshooting more difficult.
Code below. This copies the 4-element array “Sensor()” into positions (n...n+3) of the 50-element array “ModbusRegisters()”. (Note: I will have 9 dataloggers, and I am trying to reuse this code. The element-length of "Sensor()" will change among the 9 dataloggers, so I'm trying to write code that isn't specific to the array size).
Public Sensor(4), ModbusRegister(50), n BeginProg n = 35 Scan (1, Sec, 0, 0) ModbusRegister() = 0 'so user can adjust n and see data move Battery (Sensor(1)) Battery (Sensor(2)) Battery (Sensor(3)) Battery (Sensor(4)) ModbusRegister(n)() = Sensor() NextScan EndProg
The array notation was introduced as a shortcut way to populate a destination array, starting at the beginning index, from a source of greater or equal size.
The Move instruction is more suitable to accomplish what you are looking for.
The above would be done by:
Move(ModBusRegister(n),4,Sensor,4)
or
Move(ModBusRegister(n),ArrayLength(Sensor),Sensor,ArrayLength(Sensor))
The other sensors can be inserted into the ModBusRegister array at their desired locations with additional Move instructions. The swath parameters can accommodate your differing element length. This will eliminate the variable out of bounds warnings.
Perfect! Thank you.