如何通过引用在Arduino我的类传递串行对象

How to pass Serial object by reference to my class in Arduino?

本文关键字:对象 我的 何通过 引用 Arduino      更新时间:2023-10-16

我已经读了几天关于指针,参考和解引用在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
}