Revisiting old code but struggling with Serial within a class

I wrote a spectrum analyser program for a Teensy (using the Arduino IDE) a very long time ago, got a job and subsequently didn't program for a very long time. I'm revisiting it and trying to adapt it for a RPi Pico but I've fallen foul of my own terrible memory of some self taught coding on top of a shaky foundation to begin with.
I'm trying to use Serial.print to debug from within a class but if I enable any of the Serial functions, the program fails to even upload. Commenting out all of the Serial commands (just in the class) loads and runs.
I've tried to pass HardwareSerial into the constructor but exactly the same happens.

My abridged class definition is:

class Channel
{
  public:

  Channel(uint8_t channel, uint8_t led_pin, HardwareSerial* serial) :
    channel(channel)
    {
      stream = serial;
    };

 ~Channel()
    {}

  public:

  private:
    Stream* stream;
    uint8_t channel;
    void dbgprint(const char* a);
    void dbgprintln(const char* a);
};

I create the objects before setup():

  Channel channel0(0, LED_GPIO_L, &Serial);
  Channel channel1(1, LED_GPIO_R, &Serial);

and then initialise serial in setup:

  Serial.begin(115200);
  while(!Serial);

All Serial calls in setup() and loop() are fine, but if I call dbgprint(a) or dbgprintln(a) from within the class, it doesn't work.

These two functions are simply:

void Channel::dbgprint(const char* a)
{
  #ifdef DEBUG
  stream->print(a);
  #endif
}

void Channel::dbgprintln(const char* a)
{
  #ifdef DEBUG
  stream->println(a);
  #endif
}

Can someone shed some light on what I'm doing wrong?

That doesn't make any sense. How do you know it's not uploading? Did you try putting a "Hello World" print in setup() after Serial.begin()?

Did it even compile? I'm wondering because this is a problem:

  Channel(uint8_t channel, uint8_t led_pin, HardwareSerial* serial) :

On T3.x (and I assume T4.x) the 'Serial' object is not a member of the 'HardwareSerial' class. Use this instead:

  Channel(uint8_t channel, uint8_t led_pin, Stream* serial) :

You forgot to add a #include of a file where Serial is declared.

Please show the error messages which prevent compilation and upload.

It actually compiles and uploads but a pop-up appears that made it appear that the upload had failed, whereas it was actually just failing to open the serial port. With the serial port not opening, the wait loop isn't completing so the board appears dead and can't be reprogrammed until reset.

Even with your changes I get the same.

The error is:

Port monitor error: command 'open' failed: no such file or directory. Could not connect to /dev/cu.usbmodem14101 serial port.

I don't think that's a coding problem. Something is wrong with the IDE, Windows, etc.

1 Like

I thought it a possibility but wanted to check since I'm still trying to figure out what drugs I must have been taking when I wrote the original. I'm using a Mac and resisting having to switch to Visual Studio Code. I've used Eclipse in the past but it's been so long even that looks so insurmountably alien again.

I don't recall having a problem with the Teensy so perhaps it's a Pico issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.