Answer the question
In order to leave comments, you need to log in
How to pass a pointer to the Serial port to the Arduino library and ONE TIME to create a reference to it within the entire library?
For Arduino, a library is being created to work with the WiFi board.
The board can work on an arbitrary Hardware or Software port of the Mega 2556.
Therefore, for the library to work, it is required to transfer the Serial port to it, for example, pass a link or a pointer.
For example:
myClass::begin(HardwareSerial *SerialName) {
//Не подходит, поскольку далее придется использовать такой синтаксис чтобы разыменовывать указатель:
(*SerialName).end();
//или:
SerialName <- begin(9600);
}
HardwareSerial *SerialName;
HardwareSerial & MyPort = *SerialName;
myClass::begin(HardwareSerial *SerialName) {
HardwareSerial & MyPort = *SerialName;
MyPort.end();
MyPort.begin(9600);
}
HardwareSerial & MyPort = *SerialName;
has to be done in EVERY method, and it is destroyed after its execution (after execution in the main program myClass.begin();
for example). I can’t create MyPort once (globally) with a reference to the passed Serial for use in the entire library ... HardwareSerial & MyPort = *SerialName;
created in the library ONCE, for use in all methods. Is it possible to organize this when working with the Arduino IDE?
Answer the question
In order to leave comments, you need to log in
First, what is wrong with -> and why do you have <- ?
Secondly, the link is not an object, and when the link is removed, the object will be alive, the link behaves like a pointer.
Thirdly, you do not create any object, but create a link that will not go anywhere further than the compiler and will not be in the final code.
Fourth, you should get familiar with the memory mechanics in C and actually learn what a reference and a pointer are.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question