使用指针 Arduino 调用函数时出现问题
Problem calling a function with a pointer Arduino
我对 C++ 和 Arduino 的基础知识还很陌生,所以我坚持这个。我收到了一个函数,要包含在我的代码中,以对我正在读取的传感器值进行一些计算。我在这里和网上搜索并阅读了很多类似的主题,但没有找到任何可以帮助我解决我缺乏理解的东西。
我设置了一个带有循环的基本 Arduino 草图来读取传感器值,一切正常。然后我#included 头文件并将函数添加到循环外我的代码的底部,只是为了看看它是否会编译,它没有任何错误。所以现在需要调用该函数并将 2 个值传递给它,millis()
和我刚刚读取的传感器值。
部分头文件...
struct TCS1000v {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
.....
};
typedef struct TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime);
草图的循环部分
void loop() {
// reading the sensor...
unsigned short int u16CycleTime = millis();
unsigned short int u16RawVal = adc.readsensor(channel);
// the function to call - not sure about this?
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
}
提供的函数...
void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
{
ptCS1000->u16RawVal = u16RawVal;
ptCS1000->u16CycleTime = u16CycleTime;
//.... and the rest of the code in the function
}
编译错误...
test.ino: In function 'void loop()':
cs1000:30:25: error: expected primary-expression before '*' token
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:27: error: 'ptCS1000' was not declared in this scope
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:37: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:67: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
exit status 1
expected primary-expression before '*' token
所以头文件中设置了结构,但问题似乎出在指针上?或者我只是没有正确调用函数?
您在头文件中定义了一个名为 TCS1000v 的新类型,但定义不正确。使用
typedef struct {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
} TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000,
unsigned short int u16RawVal,
unsigned short int u16CycleTime);
最后我不得不设置一个全局变量,现在一切正常了。感谢观看:)
TCS1000v g_tCS1000v;
vCalcCS1000v2(&(g_tCS1000v), RawVal, CycleTime);
我对 C++ 和 Arduino 的基础知识还很陌生,所以我坚持这个。我收到了一个函数,要包含在我的代码中,以对我正在读取的传感器值进行一些计算。我在这里和网上搜索并阅读了很多类似的主题,但没有找到任何可以帮助我解决我缺乏理解的东西。
我设置了一个带有循环的基本 Arduino 草图来读取传感器值,一切正常。然后我#included 头文件并将函数添加到循环外我的代码的底部,只是为了看看它是否会编译,它没有任何错误。所以现在需要调用该函数并将 2 个值传递给它,millis()
和我刚刚读取的传感器值。
部分头文件...
struct TCS1000v {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
.....
};
typedef struct TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime);
草图的循环部分
void loop() {
// reading the sensor...
unsigned short int u16CycleTime = millis();
unsigned short int u16RawVal = adc.readsensor(channel);
// the function to call - not sure about this?
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
}
提供的函数...
void vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
{
ptCS1000->u16RawVal = u16RawVal;
ptCS1000->u16CycleTime = u16CycleTime;
//.... and the rest of the code in the function
}
编译错误...
test.ino: In function 'void loop()':
cs1000:30:25: error: expected primary-expression before '*' token
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:27: error: 'ptCS1000' was not declared in this scope
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:37: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
cs1000:30:67: error: expected primary-expression before 'unsigned'
vCalcCS1000v2(TCS1000v* ptCS1000, unsigned short int u16RawVal, unsigned short int u16CycleTime)
^
exit status 1
expected primary-expression before '*' token
所以头文件中设置了结构,但问题似乎出在指针上?或者我只是没有正确调用函数?
您在头文件中定义了一个名为 TCS1000v 的新类型,但定义不正确。使用
typedef struct {
unsigned short int u16RawVal;
unsigned short int u16RawValPrev;
unsigned short int u16CycleTime;
unsigned short int u16Iso4um;
unsigned short int u16Iso6um;
unsigned short int u16Iso14um;
} TCS1000v;
extern void vCalcCS1000v2(TCS1000v* ptCS1000,
unsigned short int u16RawVal,
unsigned short int u16CycleTime);
最后我不得不设置一个全局变量,现在一切正常了。感谢观看:)
TCS1000v g_tCS1000v;
vCalcCS1000v2(&(g_tCS1000v), RawVal, CycleTime);