Bad Scanf使功能等待/停止

时间:2016-11-29 04:46:41

标签: c avr

我不知道我的代码会发生什么,但看起来它有扫描函数的某些功能。 每当我在功能上按下rx时,功能上的模式1会像我已经输入的那样已经输入了很长时间。 我不知道如何分辨细节,只要问我是否有一些你想知道的东西..

这是我的代码

//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;
            }
    }


}

}

1 个答案:

答案 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;

第3个问题是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;

您需要对所有数组和结果变量进行所有这些更改。