Невозможно устранить ожидаемую ошибку выражения - PullRequest
1 голос
/ 24 февраля 2020

Я застрял на час с ошибкой, и это меня бесит. У меня есть следующий код (конечно, внутри основной функции):

    if (GSM_Available()){
       //1st, we'll see if some SMS are stored in the memory.
        if(send_AT_command_UART("AT+CMGR=1,0","OK\r") == 1){
            //If we receive an ok, it means that there is no sms received recently.
            continue;
        }else{
            //If we arrive here, we know that there is at least one recently received sms.
            //We have to read the UART string received (the answer from the AT command) in order to retrieve the SMS from it.
            char *answer;
            int y=0;
            char *newSensorAdress;
            //char bidule[2][64]; //Array which will contain the alert threshold temperature, the humid
            UARTReadString(answer,128);
            //Ok now we have the answer. We have to take the "data" field from the answer.
            for(int i=0;i<strlen(answer);i++){
                //Ok, so from m66 datasheet we know that the text of the received sms is located right after a carriage return plus a line feed characters.
                if(answer[i-1] == '\r' || answer[i] == '\f'){
                    //We now know that the sms starts at this index, after these 2 characters.
                    y=i;
                }
            }
            //Now, we iterate over the sms field to find what kind of sms it is (if it is an expected sms, or a bullshit sms)
            //1st character of the sms (e.g y+1) will determine the kind of frame we receive.
            switch(answer[y+1]){
                case 1:
                    //Here, it is the frame wich says the adress of the sensor to add to the network. We'll write this sensor into the EEPROM
                    //The frame is in this format : 1;abcdabcd. With the sensor's adresse starting at the first 'a'
                    //So, we'll iterate, and the index starts at y+3 (y+2 is ';')
                    int indexNewSensor=0;
                    for(int i = y+3;i<strlen(answer);i++){
                        //The adress is for now received as a string
                        strcpy(newSensorAdress[indexNewSensor],answer[i]);//Filling the newSensorAdress string. Sensor
                        indexNewSensor++;
                    }
                    //Then, once filled, this adress must be written into the EEPROM for the central station to remember it
                    break;
                case 2:
                    //Here, it is the frame which says the adress of the sensor to delete of the network. We have to delete the adresse of the network from the EEPROM
                    //For now, I don't know
                    break;
                case 3:
                    //Here, it is the frame which sends the boundary temperature, boundary humidity temperature, measurement frequency
                    char *sub_string="";
                    int newIndex=0;
                    //1st, we'll fill a new string containing ;ab;ab;abcd
                    for(int i = y+2;i<strlen(answer);i++){
                        strcpy(sub_string[newIndex],answer[i]);
                        newIndex++;
                    }
                    //Now, we'll look for the delimitters, and put in a array of string each substring
                    char *pch = strtok(sub_string,";");
                    break;
                default:
                    //Here, we have to deal when the 1st character is not a number, which means it does not match any known frame
                    //We juste have to ignore the sms
                    break;
            }
        }
    }

И вот ошибка, которую я получаю:

 make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
 make[1]: Entering directory 'D:/Projet_vigne/vigne_last'
 make  -f nbproject/Makefile-default.mk dist/default/production/vigne_last.production.hex
 make[2]: Entering directory 'D:/Projet_vigne/vigne_last'
 "C:\Program Files (x86)\Microchip\xc8\v2.10\bin\xc8-cc.exe"  -mcpu=18F46K22 -c  -fno-short-double       -fno-short-float -memi=wordwrite -fasmfile -maddrqual=ignore -xassembler-with-cpp -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx032 -Wl,--data-init -mno-keep-startup -mno-download -mdefault-config-bits   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto:auto     -o        build/default/production/main.p1 main.c 
 make[2]: *** [build/default/production/main.p1] Error 1
 make[1]: *** [.build-conf] Error 2
 make: *** [.build-impl] Error 2
 main.c:212:25: error: expected expression
                         int indexNewSensor=0;
                         ^
 main.c:215:52: error: use of undeclared identifier 'indexNewSensor'
                             strcpy(newSensorAdress[indexNewSensor],answer[i]);//Filling the      newSensorAdress string. Sensor
                                               ^
 main.c:216:29: error: use of undeclared identifier 'indexNewSensor'
                             indexNewSensor++;
                             ^
 3 errors generated.
 (908) exit status = 1
 nbproject/Makefile-default.mk:196: recipe for target 'build/default/production/main.p1' failed
 make[2]: Leaving directory 'D:/Projet_vigne/vigne_last'
 nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
 make[1]: Leaving directory 'D:/Projet_vigne/vigne_last'
 nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

 BUILD FAILED (exit value 2, total time: 859ms)

GSM_available (), send_command_AT_UART () являются функцией OF COURSE, уже определенный в другом файле (который бессмысленно показывать, поскольку тесты, выполненные в прошлом, показывают, что эти две функции работают ОТЛИЧНО).

1 Ответ

4 голосов
/ 24 февраля 2020

Вы не можете иметь объявление сразу после метки, которая включает метку case.

Это можно исправить, поместив соответствующий код в блок кода:

            case 1:
            {   
                int indexNewSensor=0;
                for(int i = y+3;i<strlen(answer);i++){
                    //The adress is for now received as a string
                    strcpy(newSensorAdress[indexNewSensor],answer[i]);
                    indexNewSensor++;
                }
            }
            break;
...