10 years, 2 months ago.

c++ help , with serial library

I need a little help with my c++ , a have two libraries which use the serial port , so have a setup routine on each labrary . Do i set up a serial class instance with different name ,

eg Serial serial_PIN(UART_TX, UART_RX); in one labray and a different name for the other eg Serial serial_PIN1(UART_TX, UART_RX);

Because I can`t using the name twice or only define it in one labrary ,

Is there a extern for class instance ..

any help please

1 Answer

10 years, 2 months ago.

Afaik you can use extern the same for an object as for a variable.

Alternatively you can just define two serial objects on the same pins. As long as you don't use interrupts that shouldn't be an issue. In principle not the nicest way, but the memory footprint of serial isn't that significant. For example for SPI and I2C that is also the normal method, since there different libraries can for example use different speeds on the same pins.

What you can also do is use pointers, where you give the class a pointer to the serial object, so it can use the functions without requiring its own object, an example is here: https://mbed.org/users/sam_grove/code/TLV320AIC1110/. (For I2C, but same works for Serial).