Ardunio Uart class & Libraries

101 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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.