-3

I have one issue with using the following if-elseif statement in C language, I am using Altera Monitor program as compiler as it is used to program the Nios II processor in the Altera development board. Previously, when I only have four options of value ‘u’ (u can be 1,2,3 or 4) , I use switch statement and it works perfectly. Now, I need three sets of u (‘u1,u2 and u3, each u again can be 1,2,3 or 4), the switch statement does not work anymore, I don’t know why only one switch statement is allowed where I cant use three switch statements, so I changed from switch to if elseif as can be seen below….

The issue here is, it works perfectly for all four cases of ‘u1’ and all four cases of ‘u2’, the problem occurs for the last case of ‘u3’ when ‘u3’ is 4. I send the data to the serial port using UART so I know when u3==4, the data is sent but with some characters missing….

The code I posted here is lengthy but it is actually doing the same thing for three cases of u1, u2 and u3 but I thought it would be appropriate for me to show all of them

The output error is as shown in this image, only data 12 is not in proper value and format, the other eleven sets show the intended format: enter image description here

EDITED: It is important to say that for txdata12, for all the trials, only the last ten ascii characters sent are correctly displayed, the first 16 digits are wrong.

EDITED: The declarations are:

int b=26;
unsigned char txdata1 [b];
unsigned char txdata2 [b];
unsigned char txdata3 [b];
unsigned char txdata4 [b];
unsigned char txdata5 [b];
unsigned char txdata6 [b];
unsigned char txdata7 [b];
unsigned char txdata8 [b];
unsigned char txdata9 [b];
unsigned char txdata10 [b];
unsigned char txdata11 [b];
unsigned char txdata12 [b];

The codes where the values are assigned, each arr1_v[3] for instance is different for each set of data and they are not constant int qq=26;

if (u1==1)

     {int MAT1[]={0,1,10, arr1_v[3], arr1_v[2], arr1_v[1],arr1_v[0],-1,arr1_q[7],arr1_q[6],arr1_q[5],arr1_q[4],arr1_q[3],arr1_q[2],arr1_q[1],arr1_q[0],-1,arr1_soc[3],arr1_soc[2],arr1_soc[1],arr1_soc[0],-1,arr1_index[1],arr1_index[0], -35,-38};
for ( i = 0 ; i < qq ; i++ ) txdata1[i] = MAT1[i];  }

else if (u1==2)

    {int MAT2[]={0,2,10, arr1_v[3], arr1_v[2], arr1_v[1],arr1_v[0],-1,arr1_q[7],arr1_q[6],arr1_q[5],arr1_q[4],arr1_q[3],arr1_q[2],arr1_q[1],arr1_q[0],-1,arr1_soc[3],arr1_soc[2],arr1_soc[1],arr1_soc[0],-1,arr1_index[1],arr1_index[0], -35,-38};
for ( i = 0 ; i < qq ; i++ ) txdata2[i] = MAT2[i];  }

else if (u1==3)

    {int MAT3[]={0,3,10, arr1_v[3], arr1_v[2], arr1_v[1],arr1_v[0],-1,arr1_q[7],arr1_q[6],arr1_q[5],arr1_q[4],arr1_q[3],arr1_q[2],arr1_q[1],arr1_q[0],-1,arr1_soc[3],arr1_soc[2],arr1_soc[1],arr1_soc[0],-1,arr1_index[1],arr1_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata3[i] = MAT3[i];  }

else if (u1==4)

    {int MAT4[]={0,4,10, arr1_v[3], arr1_v[2], arr1_v[1],arr1_v[0],-1,arr1_q[7],arr1_q[6],arr1_q[5],arr1_q[4],arr1_q[3],arr1_q[2],arr1_q[1],arr1_q[0],-1,arr1_soc[3],arr1_soc[2],arr1_soc[1],arr1_soc[0],-1,arr1_index[1],arr1_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata4[i] = MAT4[i];  }



u2=h2+1;
if (u2==1)

    {int MAT5[]={0,5,10, arr2_v[3], arr2_v[2], arr2_v[1],arr2_v[0],-1,arr2_q[7],arr2_q[6],arr2_q[5],arr2_q[4],arr2_q[3],arr2_q[2],arr2_q[1],arr2_q[0],-1,arr2_soc[3],arr2_soc[2],arr2_soc[1],arr2_soc[0],-1,arr2_index[1],arr2_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata5[i] = MAT5[i];   }

else if (u2==2)

    {int MAT6[]={0,6,10, arr2_v[3], arr2_v[2], arr2_v[1],arr2_v[0],-1,arr2_q[7],arr2_q[6],arr2_q[5],arr2_q[4],arr2_q[3],arr2_q[2],arr2_q[1],arr2_q[0],-1,arr2_soc[3],arr2_soc[2],arr2_soc[1],arr2_soc[0],-1,arr2_index[1],arr2_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata6[i] = MAT6[i]; }

else if (u2==3)

    {int MAT7[]={0,7,10, arr2_v[3], arr2_v[2], arr2_v[1],arr2_v[0],-1,arr2_q[7],arr2_q[6],arr2_q[5],arr2_q[4],arr2_q[3],arr2_q[2],arr2_q[1],arr2_q[0],-1,arr2_soc[3],arr2_soc[2],arr2_soc[1],arr2_soc[0],-1,arr2_index[1],arr2_index[0], -35,-38};
     for ( i = 0 ; i < qq ; i++ ) txdata7[i] = MAT7[i];  }

else if (u2==4)

    {int MAT8[]={0,8,10, arr2_v[3], arr2_v[2], arr2_v[1],arr2_v[0],-1,arr2_q[7],arr2_q[6],arr2_q[5],arr2_q[4],arr2_q[3],arr2_q[2],arr2_q[1],arr2_q[0],-1,arr2_soc[3],arr2_soc[2],arr2_soc[1],arr2_soc[0],-1,arr2_index[1],arr2_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata8[i] = MAT8[i];  }


u3=h3+1;

if (u3==1)

    {int MAT9[]={0,9,10, arr3_v[3], arr3_v[2], arr3_v[1],arr3_v[0],-1,arr3_q[7],arr3_q[6],arr3_q[5],arr3_q[4],arr3_q[3],arr3_q[2],arr3_q[1],arr3_q[0],-1,arr3_soc[3],arr3_soc[2],arr3_soc[1],arr3_soc[0],-1,arr3_index[1],arr3_index[0], -35,-38};
     for ( i = 0 ; i < qq ; i++ ) txdata9[i] = MAT9[i];  } 

else if (u3==2)

    {int MAT10[]={1,0,10, arr3_v[3], arr3_v[2], arr3_v[1],arr3_v[0],-1,arr3_q[7],arr3_q[6],arr3_q[5],arr3_q[4],arr3_q[3],arr3_q[2],arr3_q[1],arr3_q[0],-1,arr3_soc[3],arr3_soc[2],arr3_soc[1],arr3_soc[0],-1,arr3_index[1],arr3_index[0], -35,-38};
    for ( i = 0 ; i < qq ; i++ ) txdata10[i] = MAT10[i];  }

else if (u3==3)

    {int MAT11[]={1,1,10, arr3_v[3], arr3_v[2], arr3_v[1],arr3_v[0],-1,arr3_q[7],arr3_q[6],arr3_q[5],arr3_q[4],arr3_q[3],arr3_q[2],arr3_q[1],arr3_q[0],-1,arr3_soc[3],arr3_soc[2],arr3_soc[1],arr3_soc[0],-1,arr3_index[1],arr3_index[0], -35,-38};
for ( i = 0 ; i < qq ; i++ ) txdata11[i] = MAT11[i];  }

else if (u3==4)

    {int MAT12[]={1,2,10, arr3_v[3], arr3_v[2], arr3_v[1],arr3_v[0],-1,arr3_q[7],arr3_q[6],arr3_q[5],arr3_q[4],arr3_q[3],arr3_q[2],arr3_q[1],arr3_q[0],-1,arr3_soc[3],arr3_soc[2],arr3_soc[1],arr3_soc[0],-1,arr3_index[1],arr3_index[0], -35,-38};
for ( i = 0 ; i < qq ; i++ ) txdata12[i] = MAT12[i];  }

After the values are assigned, then I send the data to serial port

unsigned char n=26;

uart_multiple (&txdata1, n);
uart_multiple (&txdata2, n);
uart_multiple (&txdata3, n);
uart_multiple (&txdata4, n);
uart_multiple (&txdata5, n);
uart_multiple (&txdata6, n);
uart_multiple (&txdata7, n);
uart_multiple (&txdata8, n);
uart_multiple (&txdata9, n);
uart_multiple (&txdata10, n);
uart_multiple (&txdata11, n);
uart_multiple (&txdata12, n);

EDITED: I received all data perfectly except for txdata12, the first 16 characters shown are wrong, I always got it right for the last 10 characters of txdata12, I have no idea what is wrong…

I really could not figure out after a week of trying, I hope you could give me some hints on what I can work on even with other approach…. Thank you in advance

31
  • 2
    post the declarations of txdata1-12, what type are they and what dimensions? You might be having a buffer overrun
    – samgak
    Jun 5, 2015 at 9:04
  • 1
    are those big long arrays really all identical except for the first 2 lines?
    – M.M
    Jun 5, 2015 at 9:18
  • 1
    maybeu3==4 case is never triggered and so you are sending garbage
    – M.M
    Jun 5, 2015 at 9:19
  • 2
    Declare txdata12 as unsigned char txdata[64]; If that fixes it then it's most likely stack corruption causing the problem. (Only fill and send 26 bytes as before)
    – samgak
    Jun 5, 2015 at 12:28
  • 2
    Also, what is the maximum stack frame size on the device you are using?
    – samgak
    Jun 5, 2015 at 12:32

1 Answer 1

0

One thing I find a bit strange in your code is the first arguments in txtData10,11 and 12. In all the others you have e.g.

{int MAT6[]={0,6,10 

where 6 maps on the name txtdata6 but in the case of say 10 you write

{int MAT10[]={1,0,10,

which seems different, I would expect something like {0, 10,10 but ok I am just guessing on what is basically a pattern.

Other than that I think you should consider restructuring your code a bit, use {} around all statements even the one liner for-statements and put them on their own line to make the code more readable and use a switch statement. Use variable names that are descriptive. Format the values so that they are grouped.

3
  • Also MAT11[]={1,1,10 I think each digit has a single position.
    – LPs
    Jun 5, 2015 at 9:13
  • Yeah it could be, a bit difficult to know what things are for with all these magic numbers.
    – AndersK
    Jun 5, 2015 at 9:15
  • Sorry for the poor readability, I have uploaded the image where you can see each digit represents ascii number, for instance, the digit 3 which is '10' represents the symbol ";" in ascii... Jun 5, 2015 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.