2016-11-29 85 views
-1

我不知道我的代碼發生了什麼,但看起來像它有什麼與scanf函數。 每次我打到功能模式1上的功能就像它已經有輸入很久之前,我給它的輸入功能。 我不知道如何告訴細節,只是問我,如果那裏有事端更多你想知道..壞Scanf使功能等待/停止

這是我的代碼

//this is variable for the button function 
uint8_t currentStateM1=0; //mode1 
uint8_t currentStateM2=0; //txon 

int main(void) { 
PORTH.DIRCLR  = PIN0_bm; 
PORTH.PIN0CTRL = PORT_OPC_PULLUP_gc; 

PORTH.DIRCLR  = PIN1_bm; 
PORTH.PIN1CTRL = PORT_OPC_PULLUP_gc; 

while(1) 
    { 
    //this two is to trap an input in button because i need it to be a      
     //switch 
    //mode1 
    if (!(PORTH_IN&PIN0_bm)) //check if button is pressed 
    { 
     onButtonM1(); 
     while(!(PORTH.IN&PIN0_bm)); //trap mcu until button is released 
    } 
    //txon 
    if (!(PORTH_IN&PIN6_bm)) //check if button is pressed 
    { 
     onButtontxon(); 
     while(!(PORTH.IN&PIN6_bm)); //trap mcu until button is released 
    } 
    } 
    } 

    //===============================tx on part=============================// 

    //so this is where i split the string that come in to check if the code 
    //is right or not 
    //dataTXON 
    char txon[12];  
    unsigned char resultxo1,resultxo2,resultxo3,resultxo4,resultxo5,resultxo6; 
    int t = 0; 
    char partxon[6] = {0}; 

    //dataInSTNDBY 
    char stdby[12];  
    unsigned char  resultby1,resultby2,resultby3,resultby4,resultby5,resultby6; 
    int b = 0; 
    char partby[6] = {0}; 


    void onButtontxon(void) //call on button press 
    { 
    if (currentStateM2) 
     { 
     currentStateM2 = 0; 
     _delay_ms(800); 
     if (currentStateM2 == 0) 
     { 
      printf("3C03BB6A02683E = STANDBY\n"); 
      //PORTD.DIRCLR = PIN1_bm; 

      scanf("%s", &stdby); 

      memcpy(partby, stdby + b, 2); b += 2; 
      resultby1 = strtoul(partby, NULL, 16); 
      memcpy(partby, stdby + b, 2); b += 2; 
      resultby2 = strtoul(partby, NULL, 16); 
      memcpy(partby, stdby + b, 2); b += 2; 
      resultby3 = strtoul(partby, NULL, 16); 
      memcpy(partby, stdby + b, 2); b += 2; 
      resultby4 = strtoul(partby, NULL, 16); 
      memcpy(partby, stdby + b, 2); b += 2; 
      resultby5 = strtoul(partby, NULL, 16); 
      memcpy(partby, stdby + b, 2); b += 2; 
      resultby6 = strtoul(partby, NULL, 16); 
      b = 0; 

      int Checkstdby = resultby3^resultby4^resultby5; 

      if ((resultby1 == 0x3C) && (resultby2 == 0x03) && (resultby3 == 0xAA) && (resultby4 == 0x6A) && (resultby5 == 0x02) && (resultby6 == 0x3E) && (Checkstdby == 0xC2)) 
      { 
       printf("awesome!\n"); 
       PORTE.DIRCLR = PIN3_bm; 
      }  
      else if ((resultby1 != 0x3C) || (resultby2 != 0x03) || (resultby3 != 0xAA) || (resultby4 != 0x6A) || (resultby5 != 0x02) || (resultby6 != 0x3E) || (Checkstdby != 0xC2)) 
      {  
       printf("wrong code\n"); 
      } 
    } 
    return 0; 

} 

else 

{ 
    //now this function will read if the M1 is on or not, if yes 
    //this function will turn on a led to indicate that TX is ON in M1 mode 
    currentStateM2 = 1; 
    _delay_ms(800); 
    if (currentStateM2 == 1) 
    { 
     printf("3C03BB6A01D03E = TX ON"); 
     scanf("%s", &txon); 

     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo1 = strtoul(partxon, NULL, 16); 
     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo2 = strtoul(partxon, NULL, 16); 
     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo3 = strtoul(partxon, NULL, 16); 
     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo4 = strtoul(partxon, NULL, 16); 
     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo5 = strtoul(partxon, NULL, 16); 
     memcpy(partxon, txon + t, 2); t += 2; 
     resultxo6 = strtoul(partxon, NULL, 16); 
     t = 0; 

     int Checktx = resultxo3^resultxo4^resultxo5; 

     if ((resultxo1 == 0x3C) && (resultxo2 == 0x03) && (resultxo3 == 0xAA) && (resultxo4 == 0x6A) && (resultxo5 == 0x01) && (resultxo6 == 0x3E) && (Checktx == 0xC1) && (currentStateM1 == 1)) 
      { 
       printf("awesome!\n"); 
       PORTE.DIRSET = PIN3_bm; 
      }      


      else if ((resultxo1 != 0x3C) || (resultxo2 != 0x33) || (resultxo3 != 0xAA) || (resultxo4 != 0x6A) || (resultxo5 != 0x01) || (resultxo6 != 0x3E) || (Checktx != 0xC1) || (currentStateM1 != 1)) 
      {  
       printf("apadah\n"); 
      } 
    } 


} 
} 

//===========================M1 part==============================// 

    //dataIn ON 
    char bc[12];  
    unsigned char result1,result2,result3,result4,result5,result6; 
    int i = 0; 
    char partonin[6] = {0}; 

    //dataIn OFF 
    char bx[12];  
    unsigned char resultx1,resultx2,resultx3,resultx4,resultx5,resultx6; 
    int c = 0; 
    char partofin[6] = {0}; 

    //this is where it all begin, i push a button to trigger this function. 
    //its the function to turn on/off the M1 Mode, indicated by led inside 
    //the button. 
    //the led inside the button turned on if the code i recieve is right 
    //after mode 1 on, i want to active the TX ON funtion to it 
    //back to the top.... 
    void onButtonM1(void) //call on button press 
    { 
    if (currentStateM1) 

    { 
    currentStateM1 = 0; 
    _delay_ms(800); 
    if (currentStateM1 == 0) 
    { 
     printf("3C03BB6A04D53E = M1 OFF\n"); 
     scanf("%s", &bx); 

     memcpy(partofin, bx + c, 2); c += 2; 
     resultx1 = strtoul(partofin, NULL, 16); 
     memcpy(partofin, bx + c, 2); c += 2; 
     resultx2 = strtoul(partofin, NULL, 16); 
     memcpy(partofin, bx + c, 2); c += 2; 
     resultx3 = strtoul(partofin, NULL, 16); 
     memcpy(partofin, bx + c, 2); c += 2; 
     resultx4 = strtoul(partofin, NULL, 16); 
     memcpy(partofin, bx + c, 2); c += 2; 
     resultx5 = strtoul(partofin, NULL, 16); 
     memcpy(partofin, bx + c, 2); c += 2; 
     resultx6 = strtoul(partofin, NULL, 16); 
     c = 0; 

     int CheckInof = resultx3^resultx4^resultx5; 

     if ((resultx1 == 0x3C) && (resultx2 == 0x03) && (resultx3 == 0xAA) && (resultx4 == 0x6A) && (resultx5 == 0x04) && (resultx6 == 0x3E) && (CheckInof == 0xC4)) 
      { 
       printf("awesome!\n"); 
       PORTD.DIRCLR = PIN1_bm; 
      }  
      else if ((resultx1 != 0x3C) || (resultx2 != 0x03) || (resultx3 != 0xAA) || (resultx4 != 0x6A) || (resultx5 != 0x04) || (resultx6 != 0x3E) || (CheckInof != 0xC4)) 
      {  
       currentStateM1 = 1; 
       printf("not the answer\n"); 
      } 
    } 

} 

else 

{ 
    currentStateM1 = 1; 
    _delay_ms(800); 
    if (currentStateM1 == 1) 
    { 
     printf("\n3C03BB6A03D23E = M1 ON\n"); 
     scanf("%s", &bc); 

     memcpy(partonin, bc + i, 2); i += 2; 
     result1 = strtoul(partonin, NULL, 16); 
     memcpy(partonin, bc + i, 2); i += 2; 
     result2 = strtoul(partonin, NULL, 16); 
     memcpy(partonin, bc + i, 2); i += 2; 
     result3 = strtoul(partonin, NULL, 16); 
     memcpy(partonin, bc + i, 2); i += 2; 
     result4 = strtoul(partonin, NULL, 16); 
     memcpy(partonin, bc + i, 2); i += 2; 
     result5 = strtoul(partonin, NULL, 16); 
     memcpy(partonin, bc + i, 2); i += 2; 
     result6 = strtoul(partonin, NULL, 16); 
     i = 0; 

     int CheckIn = result3^result4^result5; 

     if ((result1 == 0x3C) && (result2 == 0x03) && (result3 == 0xAA) && (result4 == 0x6A) && (result5 == 0x03) && (result6 == 0x3E) && (CheckIn == 0xC3)) 
      { 
       printf("awesome!\n"); 
       PORTD.DIRSET = PIN1_bm; 
      }  
      else if ((result1 != 0x3C) || (result2 != 0x03) || (result3 != 0xAA) || (result4 != 0x6A) || (result5 != 0x03) || (result6 != 0x3E) || (CheckIn != 0xC3)) 
      {  
       printf("apadah\n"); 
       currentStateM1 = 0; 
      } 
    } 


} 

}

+0

有多個scanf()。哪裏出問題了,出了什麼問題?在您的代碼中添加一些註釋。此外,如果有任何刪除不必要的代碼。 – MayurK

+0

你已經添加了一些評論,很好。但請格式化您的代碼。閱讀和理解非常困難。還要提及你在哪裏(在哪一行)觀察這個問題。 – MayurK

+0

完成了,我希望你現在可以更輕鬆地閱讀它。感謝您的回覆。 –

回答

0

我認爲這個問題是你的字符數組大小是12,你試圖存儲一個長度爲12的字符串。但是請記住,你只能存儲11個字符+'\ 0'。

因此,請將您的所有字符數組大小更改爲13.還要確保scanf只需要12個字符。

char bc[13] = ""; //Better to initialize the array. Anyway you are doing scanf() and get the proper value. I don't see any issue. 
[...] 
scanf("%12s", bc); //Accept only 12 characters + '\0'. 
[...] 

第二個問題是與strtoul()。它返回unsigned long int值,並且您將它存儲在unsigned char中,這是錯誤的。因此,改變

unsigned char resultxo1,resultxo2,resultxo3,resultxo4,resultxo5,resultxo6; 

unsigned long int resultxo1,resultxo2,resultxo3,resultxo4,resultxo5,resultxo6; 

第三期是的memcpy()。要使用strtoul(),第一個參數必須是一個字符串。因此,改變

 memcpy(partby, stdby + b, 2); b += 2; 
     resultby1 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby2 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby3 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby4 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby5 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby6 = strtoul(partby, NULL, 16); 
     b = 0; 

 partby[2] = '\0'; //You are copying to partby[0] and partby[1] only. 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby1 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby2 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby3 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby4 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby5 = strtoul(partby, NULL, 16); 
     memcpy(partby, stdby + b, 2); b += 2; 
     resultby6 = strtoul(partby, NULL, 16); 
     b = 0; 

你需要讓所有這些變化對所有的陣列和結果變量。