1

I am trying and failing to covert a sketch into a library.

Using an Arduino Zero.

In sketch form:

Variable of type Uart is defined as follows:

Uart* serPort = &serial2;

There is a function that switches between &serial1 and &serial2.

This works.

Now I am trying to implement the same into the library:

Defined in the main code as follows:

Sender Sender(Serial2);

Here is the header file:

class Sender
{
 public:

   Sender(Uart PortIn);

   private:

   Uart* serPort;
};

Here is the .cpp file:

Sender::Sender(Uart PortIn)
{

   byte data[]={0xff,0xaa};
   serPort = &PortIn;

   serPort->write(data,2);

}

This complies but something must crash on startup as the USB port is lost. Reset button has to be pressed to start the bootloader.

I cannot seem to find what is wrong.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Jeremy
  • 111
  • 1
  • 5
  • 11

1 Answers1

0

I think you need to send the argument as a reference or a pointer to the constructer.

In main, It should look something like:

Uart* serPort = new Uart();
serPort.setName("COMX");
serPort.setBaud(9600);
Sender s = new Sender(serPort);

....

delete serPort;

Hope it helps.

unnamed
  • 840
  • 9
  • 26
  • 38
  • Thanks - How would you specify the port in this way? – Jeremy Aug 02 '18 at 11:18
  • @Jeremy I do not know the details of the Uart class, but there should be an argument while creating the object or you could call a function to set the port name and baud rate such as serPort.setName("COM3") and serPort.SetBaud(9600) etc.. Please have a look at my updated result – unnamed Aug 02 '18 at 12:16