如何通过引用在Arduino我的类传递串行对象
How to pass Serial object by reference to my class in Arduino?
我已经读了几天关于指针,参考和解引用在C/c++针对Arduino,不能完全理解我错过了什么。
我的草图设置为
Serial.begin(9600); //Just for logging.
Serial1.begin(9600); //Arduino Mega-> Other Device
我使用一个包装器类通过调用一个简单的函数,如getStatus()
来发送BYTE的Serial1。
我遇到的问题是我想让我的类更动态,所以我希望我的类使用Serial1, Serial2, Serial3甚至基础串行-但我不知道如何构建我的。h和。cpp文件使用这些作为参考。
目前我的类有一个静态Serial1,但如果有人想使用我的类,他们将不得不重命名为Serial,如果他们使用Arduino Uno。
在myclass。h中有类似
的内容myClass(HardwareSerial *serial);
和myClass.cpp (constructor)中的
myClass::myClass(HardwareSerial &serial) {
_HardSerial = serial;
..
}
但是编译器一直抱怨) expected before & or *
。
我尝试了各种方法,总是得到相同的错误-除了当我引用我的类到串行对象-但它说串行没有定义..
我找不到任何教程,除了指针资源Arduino。
声明和创建
使用#include "Print.h" //datatype* variablename = ⌖ Print* printer = &Serial; Usage
//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");
这正是我想要的-但似乎我不明白它。我做了他们在那里做的事,但这对我不起作用。
编辑1
谢谢,我用参考的方式做了,因为它更好。但是,我仍然得到这些错误:
:88: error: 'HardwareSerial' has not been declared
第88行from .h:
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
来自。h的第136行(这种情况不断发生-我不知道该使用什么,Serial.write?/串行?):
private:
HardwareSerial& _HardSerial;
编辑2
所以,基本上是因为我在文件myClass.h
中导入HardWareserial.h
文件,该文件在我的草图中导入以使用所有myClasses材料-它杀死了草图中的引用,并希望我重新定义串行实例。似乎继承的方向是错的……很烦人。草图中使用的默认构造函数是什么?
它就像在要求我做这个:
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
真的吗?
你混合了c++引用和指针。从这个示例片段来看,您应该使用指针,因此我的回答遵循了这种风格。参考文献可能是更好的选择。
你的代码应该看起来像这样:
myclass.h
myClass(HardwareSerial *serial); // ctor
HardwareSerial * _HardSerial; // member within class
myclass.cpp
// Constructor takes address of serial port
myClass::myClass(HardwareSerial *serial) {
_HardSerial = serial;
...
}
调用代码:
// Gets the address of "Serial1" and passes that to the constructor
myClass(&Serial1);
如果你想把它作为引用,它看起来像这样:
myclass.h
myClass(HardwareSerial& serial); // ctor taking reference
HardwareSerial& _HardSerial; // reference member within class
myclass.cpp
// Constructor takes reference to a serial port object
myClass::myClass(HardwareSerial& serial) :
_HardSerial(serial) // Need to initialise references before body
{
...
}
调用代码:
myClass(Serial1); // Compiler automatically uses reference
在Arduino Leonardo上,我认为Serial
实际上不是HardwareSerial
的实例,而是Serial_
,因为它是一个虚拟USB链接,而不是一个正常的USART。
然而,这两个类都是Stream
的子类。
也许你可以声明Stream*
而不是HardwareSerial*
我对Murdoch先生的代码有一个改进(顺便说一下,非常感谢,遵循您关于如何初始化串行对象的示例使我成为一个更快乐,更少编译受挫的人)。
他的方法将允许你将任何硬件串行对象传递给他的类。默认情况下,我的将允许您传递(并配置)一个硬件对象到类,但是如果您感兴趣的话,也允许您稍后切换到一个软件串行对象。
几乎与他的代码完全相同,除了我用Stream对象替换了他的类中HardwareSerial对象的实例。
myclass.h
myClass(HardwareSerial& serial, int baud, int config); // Constructor taking reference
Stream& _s; // reference member within class
myclass.cpp
// Constructor takes reference to a serial port object
myClass::myClass(HardwareSerial& serial = Serial, int baud = 9600, int config = SERIAL_8N1) :
_s(serial) // Need to initialise references before body
{
serial.begin(baud, config); //We guarantee a hardware interface at first
this->_s = serial; //The constructor uses the hardware serial class
//but we can replace it with a virtual hardware
//class if we have to thanks to stream
}
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 模板,函数使用错误的构造函数来复制我的对象
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- 我可以读取静态对象中的文件.txt吗?C++
- 为什么C++无法识别我的对象实例化?
- 为什么我的 DeviceInformation 对象没有 System.Devices.InterfaceClassGuid 属性?
- 为什么当我尝试返回引用时,我的对象仍然被复制
- 获取我的基类以递增派生类对象整数
- 为什么我的 constexpr 对象在我的函数中不是 constexpr?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 用字符串初始化我的类的对象
- 为什么我的对象声明不调用默认构造函数?
- 为什么我的生成文件没有生成对象文件?
- 为什么我不能更改实例化对象内部的向量?
- 为什么Visual Studio会以不同的方式对待我的对象
- 有什么方法可以检测我的类的对象是否在堆栈上创建
- 为什么STL优先级队列错误地分配了我的类对象
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- 视觉对象 我的客户端连接类出了什么问题?C++
- 按值分配成员对象:我的假设是否正确