从 'byte*' 到 'byte' 的无效转换

invalid conversion from 'byte*' to 'byte'

从 'byte*' 到 'byte'

的无效转换

我已经写了这个arduino函数

byte receiveMessage(AndroidAccessory acc,boolean accstates){
    if(accstates){
        byte rcvmsg[255];
        int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
        if (len > 0) {
            if (rcvmsg[0] == COMMAND_TEXT) {
                if (rcvmsg[1] == TARGET_DEFAULT){
                byte textLength = rcvmsg[2];
                int textEndIndex = 3 + textLength;
                byte theMessage[textLength];
                int i=0;
                    for(int x = 3; x < textEndIndex; x++) {
                        theMessage[i]=rcvmsg[x];
                        i++;
                        delay(250);
                    }
                return theMessage;
                delay(250);
                }
            }
        }
    }       
}

这是错误

In function byte receiveMessage(AndroidAccessory, boolean) invalid conversion from byte*' to 'byte"

这个函数是从android和return接收数据作为一个字节数组

你需要使用动态分配,或者将数组作为参数传递给函数,这在你的情况下是更好的解决方案

void receiveMessage(AndroidAccessory acc, boolean accstates, byte *theMessage){
    if (theMessage == NULL)
        return;
    if(accstates){
        byte rcvmsg[255];
        int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
        if (len > 0) {
            if (rcvmsg[0] == COMMAND_TEXT) {
                if (rcvmsg[1] == TARGET_DEFAULT){
                byte textLength = rcvmsg[2];
                int textEndIndex = 3 + textLength;
                int i=0;
                    for(int x = 3; x < textEndIndex; x++) {
                        theMessage[i]=rcvmsg[x];
                        i++;
                        delay(250);
                    }
                return;
                }
            }
        }
    }       
}

这样,您将调用将数组传递给它的函数,例如

byte theMessage[255];

receiveMessage(acc, accstates, theMessage);
/* here the message already contains the data you read in the function */

但是你不能return一个局部变量,因为数据只在变量有效的范围内有效,实际上它在if (rcvmsg[0] == COMMAND_TEXT)块之外是无效的,因为你将其定义为该块的本地。

注意:请阅读Wimmel的评论,或者你可以将最后一个字节设置为'[=13=]',如果它只是文本,然后将数组用作字符串。

就错误而言,您return输入的值不正确。

theMessage is a byte array not a byte 

最后的答案也解释了为什么你不能 return 局部变量指针