Array index problem
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.
IF (FIND_STRING(mzcrecbuf,"$55",1)) { ss = FIND_STRING(mzcrecbuf, "$55", 1) //find sync byte sl = mzcrecbuf[(ss+1)] //get string length if (mzcrecbuf[(ss+sl+1)]="$55") //check for full string - strings not terminated { replya = mid_string(mzcrecbuf,ss,(sl+1)) remove_string(mzcrecbuf,replya,1) zone = mid_string(replya,4,1) // tried type_cast here SEND_STRING 0,"'zone',zone" ZonePower[zone]="1" //tried atoi itoa hextoi } else { //REMOVE_STRING(MZCRECBUF,"$55",1) // look at this later } }DEFINE_DEVICE dvTP = 10001:1:0 DEFINE_VARIABLE INTEGER x INTEGER nTest[50] DEFINE_START //fill in with numbers 2-100 FOR (x=1; x<=50; x++) { nTest[x] = x*2 } DEFINE_EVENT BUTTON_EVENT[dvTP,1] { PUSH: { SEND_STRING 0,"'Array Position $10 = ',ITOA([b]nTest[$10][/b])" //element 16 SEND_STRING 0,"'Array Position ''0'' = ',ITOA([b]nTest[TYPE_CAST('0')][/b])" //element 48 } }And the output when button 1 is pushed: That?s absolutely true.
SEND_STRING 0,"'Array Position ''32'' = ',ITOA(nTest[TYPE_CAST(' ')])" //element 32 or SEND_STRING 0,"'Array Position ''63'' = ',ITOA(nTest[TYPE_CAST('?')])" //element 63But you can. I know if I came across this in a block of code I'd be scratching my head for awhile. I keep forgetting that real programmers do think differently and can look at letters and numbers and automatically see their equivelent in another data type. I still have a hard time remembering that in the end they all are binary 1's & 0's. Some can read binary like I read Ascii, heck some programmers probably read binary better than I read ascii.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.