S
S
Sterpa2014-12-08 12:15:14
Arduino
Sterpa, 2014-12-08 12:15:14

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);
}

And it is required to use classical syntax.
Practically it turned out to use the following construction:
in the file myLibrary.h
HardwareSerial *SerialName;
//and right there
HardwareSerial & MyPort = *SerialName;
Then in the file myLibrary.cpp I can already use:
myClass::begin(HardwareSerial *SerialName) {
  HardwareSerial & MyPort = *SerialName;
  MyPort.end();
  MyPort.begin(9600);
}

The problem is that object creation 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 ...
So, it is required that it be 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

2 answer(s)
A
AxisPod, 2014-12-08
@AxisPod

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 question

Ask a Question

731 491 924 answers to any question