Pio*_*ula 11 c c++ pointers arduino
我已经阅读了几天关于针对Arduino的C/C++中的指针,引用和解引用,并且无法完全了解我所缺少的内容.
我的草图有一个设置
Serial.begin(9600); //Just for logging.
Serial1.begin(9600); //Arduino Mega-> Other Device
Run Code Online (Sandbox Code Playgroud)
我使用包装类通过调用一个简单的函数来通过Serial1发送BYTE getStatus()
.
我遇到的问题是我想让我的课程更有活力,所以我希望我的班级使用Serial1,Serial2,Serial3甚至是基本的Serial - 但我不知道如何构建我的.h和.cpp文件使用这些作为参考.
目前我的班级有一个静态的Serial1,但如果有人想使用我的班级,他们必须将所有内容重命名为Serial,如果他们使用Arduino Uno.
在myclass.h我有类似的东西
myClass(HardwareSerial *serial);
Run Code Online (Sandbox Code Playgroud)
在myClass.cpp(构造函数)中:
myClass::myClass(HardwareSerial &serial) {
_HardSerial = serial;
..
}
Run Code Online (Sandbox Code Playgroud)
但编译器一直在呻吟) expected before & or *
.
我已经尝试了各种方法,总是得到相同的错误 - 除非我将我的类引用到Serial对象 - 但它说Serial没有定义..
除了Arduino的Pointer Resource之外,我找不到任何教程.
宣言与创作
Run Code Online (Sandbox Code Playgroud)#include "Print.h" //datatype* variablename = ⌖ Print* printer = &Serial; Usage
用法
Run Code Online (Sandbox Code Playgroud)//this is the equivalent of Serial.print printer->print("print using the Serial object"); //notice the -> as opposed to . //change target address (or which address to point to) printer = &Serial2; //this is the equivalent of Serial2.print printer->print("print using the Serial2 object");
这正是我想要的 - 但似乎我不理解它.我做了他们在那里做的事情,但这对我不起作用.
谢谢,我做了参考方式,因为它更好.但我仍然会遇到这些错误:
:88: error: 'HardwareSerial' has not been declared
Run Code Online (Sandbox Code Playgroud)
.h的第88行:
uint8_t Init(long BaudRate, HardwareSerial& serial);
.h:136: error: ISO C++ forbids declaration of 'HardwareSerial' with no type
.h:136: error: expected ';' before '&' token
Run Code Online (Sandbox Code Playgroud)
来自.h的第136行(这种情况一直在发生 - 我不知道该使用什么,Serial.write?/ Serial?):
private:
HardwareSerial& _HardSerial;
Run Code Online (Sandbox Code Playgroud)
所以,主要是因为我导入HardWareserial.h
文件中的文件myClass.h
导入我的草图中以使用所有myClasses的东西 - 它会杀死草图中的引用并希望我重新定义Serial实例.似乎继承是错误的方式...烦人.草图中使用的默认构造函数是什么?
这就像是要求我这样做:
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
Run Code Online (Sandbox Code Playgroud)
真?我再一次不明白......
tin*_*man 26
你正在混合C++引用和指针.从该示例代码片段看,你应该使用指针,所以我的答案遵循这种风格.尽管参考文献可能是更好的选择.
您的代码应如下所示:
myclass.h
myClass(HardwareSerial *serial); // ctor
HardwareSerial * _HardSerial; // member within class
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
// Constructor takes address of serial port
myClass::myClass(HardwareSerial *serial) {
_HardSerial = serial;
...
}
Run Code Online (Sandbox Code Playgroud)
调用代码:
// Gets the address of "Serial1" and passes that to the constructor
myClass(&Serial1);
Run Code Online (Sandbox Code Playgroud)
如果你想把它作为参考,它看起来像这样:
myclass.h
myClass(HardwareSerial& serial); // ctor taking reference
HardwareSerial& _HardSerial; // reference member within class
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
// Constructor takes reference to a serial port object
myClass::myClass(HardwareSerial& serial) :
_HardSerial(serial) // Need to initialise references before body
{
...
}
Run Code Online (Sandbox Code Playgroud)
调用代码:
myClass(Serial1); // Compiler automatically uses reference
Run Code Online (Sandbox Code Playgroud)
小智 7
在Arduino Leonardo上,我认为Serial
它实际上并不是一个实例HardwareSerial
,而是Serial_
因为它是一个虚拟的USB链接,而不是一个普通的USART.
但是,这两个类都是其子类Stream
.
所以也许你可以申报Stream*
而不是HardwareSerial*
.