Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass Serial object by reference to my class in Arduino?
    primarykey
    data
    text
    <p>I have been reading up a couple of days now about pointers, references and dereferences in C/C++ targeted for the Arduino and can't fully udnerstand what I am missing.</p> <p>I have my <a href="http://www.arduino.cc/en/Tutorial/Sketch" rel="nofollow">sketch</a> which has a setup of </p> <pre><code>Serial.begin(9600); //Just for logging. Serial1.begin(9600); //Arduino Mega-&gt; Other Device </code></pre> <p>I use a wrapper class to send BYTE's over Serial1 by calling a simple function like <code>getStatus()</code>.</p> <p>The issue I am having is I would like to make my class more dynamic, so I would like my class to use Serial1, Serial2, Serial3 or even the base Serial - but I do not know how to build my .h and .cpp files to use these as references.</p> <p>At the moment my class has a static Serial1 but if somebody would like to use my class they would have to rename everything to Serial if they use <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino Uno</a>.</p> <p>In myclass.h I have something like</p> <pre><code>myClass(HardwareSerial *serial); </code></pre> <p>and in myClass.cpp (constructor):</p> <pre><code>myClass::myClass(HardwareSerial &amp;serial) { _HardSerial = serial; .. } </code></pre> <p>But the compiler keeps on moaning about <code>) expected before &amp; or *</code>.</p> <p>I have tried various ways and always get the same error- except when I reference my class to the Serial object-- but it says Serial was not defined..</p> <p>I can't find any tutorials, except <em><a href="http://www.arduino.cc/playground/Code/Pointer" rel="nofollow">Pointer Resource for Arduino</a></em>.</p> <blockquote> <p>Declaration and Creation</p> <pre><code>#include "Print.h" //datatype* variablename = &amp;target; Print* printer = &amp;Serial; Usage </code></pre> <p>Usage</p> <pre><code>//this is the equivalent of Serial.print printer-&gt;print("print using the Serial object"); //notice the -&gt; as opposed to . //change target address (or which address to point to) printer = &amp;Serial2; //this is the equivalent of Serial2.print printer-&gt;print("print using the Serial2 object"); </code></pre> </blockquote> <p>Which is exactly what I want - but it seems I do not understand it. I did what they did there, and it does not work for me.</p> <h2>EDIT 1</h2> <p>Thanks, I did it the reference way because it is better yet. I still get these errors, though:</p> <pre><code>:88: error: 'HardwareSerial' has not been declared </code></pre> <p>Line 88 from .h:</p> <pre><code>uint8_t Init(long BaudRate, HardwareSerial&amp; serial); .h:136: error: ISO C++ forbids declaration of 'HardwareSerial' with no type .h:136: error: expected ';' before '&amp;' token </code></pre> <p>Line 136 from .h (this keeps on happening- I don't know what to use, Serial.write? / Serial?):</p> <pre><code>private: HardwareSerial&amp; _HardSerial; </code></pre> <h2>EDIT 2</h2> <p>So, basically because I import the <code>HardWareserial.h</code> file in file <code>myClass.h</code> which is imported in my sketch to use all myClasses stuff - it kills the references in the sketch and wants me to redefine the Serial instances. It seems like the inheritance is the wrong way around…annoying. What's the default constructor used in the sketch?</p> <p>It's like it's asking me to do this:</p> <pre><code>HardwareSerial Serial1(&amp;rx_buffer1, &amp;UBRR1H, &amp;UBRR1L, &amp;UCSR1A, &amp;UCSR1B, &amp;UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1); </code></pre> <p>Really? Again I don't understand...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload