Issues with if elseif statements in C code, only part of statements in last case are executed

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

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);

I received all data perfectly exceot for txdata12, some data are missing especially the first few characters I always got it right for the characters at the end, 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


Source: c#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.