Array index problem
xrmichael
Posts: 79
I am trying to update an array by referencing the array index via a var such as ZonePower[var]="1" the code complies but on run time generates an error "index too large". The var is updated from a incoming hex string denoting the zone number and ranges from $01 to $06.
If i alter the code to ZonePower[1]="1" it runs fine but if i go back to ZonePower[var]="1" it fails, even though in the debug window var holds 1 as its value.
i have tried assigning the "var" via different string manipulation types atoi,type cast etc but still seem to be at a loss.
Any pointers gratefully received.
If i alter the code to ZonePower[1]="1" it runs fine but if i go back to ZonePower[var]="1" it fails, even though in the debug window var holds 1 as its value.
i have tried assigning the "var" via different string manipulation types atoi,type cast etc but still seem to be at a loss.
Any pointers gratefully received.
0
Comments
To further ellaborate on jjames's comment...
It's a common error. You'll grab a zone value from a text string. Say something along the lines of
"input3output4"
and you then do the
nZone=remove_string bla bla bla where nZone is an integer. to get the value 4. But you forget to do an ATOI so the value entered into nZone is the hex value for the charactor '4' which is Hex 34.
I would check on the size of the ZonePower array.
Also, in your debugger, where it says var is 1... drag a little to the right. Does it say Decimal or ASCII?
Finally I would throw a SEND_STRING 0, "'var=',var" right before the offending line to make absolutely sure which cell of the array you're trying to access.
Post the code.
It shows as DEC or HEX in the debug the incoming data is in hex format, but when referencing an array index i presume you could use hex or dec. Also the inoming data has no termination byte, it has a start but i frequently find the start byte appears again as valid data in the string hence the if line below.
And the output when button 1 is pushed: That?s absolutely true.
I got it working with the following line, ZonePower[zone] = mid_string(replya,6,1). A tweak to array declaration must have been the problem.
On to the next issue, the code runs but slow i am missing data my buffer is always full so data is getting pushed out before i have acted on it,
The unit does not send a Termination Character just a start and also a length, so i figured these lines used in a while loop would on 1st pass remove any incomplete strings and then keep the buffer in check
if(mzcrecbuf[(ss+sl+1)]="$55") //ss marks the 1st 55 sl contains the length
{
replya = mid_string(mzcrecbuf,ss,(sl+1))
remove_string(mzcrecbuf,replya,1)
}
else
{
send_string 0,"'incomplete string'" //
}
I am getting the 'incomplete string" in my debug 2 times a second so something is wrong.
Am going about this the correct way, what further complicates it is that a $55 can appear mid string as valid data.
Thanks. for all the pointers they are invaluable information on the long road to becoming a real programmer.