2016-11-29 76 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; 

你需要让所有这些变化对所有的阵列和结果变量。