Serial sux

Why does this not work?

#define MYSERIAL Serial

error: 'Serial' was not declared in this scope

I tried with a nano and it gives no compilation error

#define MYSERIAL Serial

void setup() {
}

void loop() {
}

@lesept - he was probably then doing something like MYSERIAL.println() - which definitely should work, assuming he's using a board where Serial exists.

What board are you using?

Sure, I cant guess what he doesn't say...

Why does this not work?

Who knows?

The tiny scrap you posted is not the problem. Start your journey here.

schwack:
#define MYSERIAL Serial

error: 'Serial' was not declared in this scope

If it helps, because I want flexibility for different ways of wiring, I use a block

// now a device to send monitor messages
#if dbgTyp==HARDWARE
HardwareSerial &monSerial = Serial;
#elif dbgTyp==ALTSOFTSERIAL
AltSoftSerial monSerial;
#elif dbgTyp==SOFTSERIAL
SoftwareSerial monSerial;
#elif dbgtTyp=PIGGYBACK
AltSoftSerial &monSerial = btSerial;
#else dbgTyp==NONE
//we *could* just not have  monSerial, but this will get optimized out, so that we can keep debug code
nullSerial monSerial;
#endif  //monitor hardware #if

so that my code can simply refer to monSerial and biserial without regard to what kind they really are (or possibly nullSerial to get optimized out on compile.

Anyway, note the dereferencing for HardWareSerial.

hawk

It turns out that I am encountering unexplainable errors no matter what I try to compile (except for the most simple basic scripts). Even code that I have uploaded several times before. For example, I cannot upload the Repetier Host OR Marlin firmware to my 3d printer. (The Serial error is from Marlin). And this is software I have uploaded 50 times or more.

I tried switching back to an earlier IDE (1.8.8, 1.6.?) but I can't seem to shake the problem.

I have also tried using Visual Studio and VS Code, but had no luck.

I reverted back to the same revision I uploaded before in Git, and even it will not compile.

I do not even know where to begin looking for the problem.

schwack:
I do not even know where to begin looking for the problem.

Try posting your entire code in tags :wink:

// now a device to send monitor messages

#if dbgTyp==HARDWARE
HardwareSerial &monSerial = Serial;
#elif dbgTyp==ALTSOFTSERIAL
AltSoftSerial monSerial;
#elif dbgTyp==SOFTSERIAL
SoftwareSerial monSerial;
#elif dbgtTyp=PIGGYBACK
AltSoftSerial &monSerial = btSerial;
#else dbgTyp==NONE
//we could just not have  monSerial, but this will get optimized out, so that we can keep debug code
nullSerial monSerial;
#endif  //monitor hardware #if

So this looks nothing like your first question.

I did note the dereference. But you only do that on one. If that one is active then you would have to write  monSerial->println("hello");and that would fail for all of the non-dereference versions.

What is nullSerial? Where is that declared?

Is there a similar block above this which decides to #include the correct library? You can't just include all those libraries as they will clash with each other.

MorganS:
So this looks nothing like your first question.

I did note the dereference. But you only do that on one. If that one is active then you would have to write

  monSerial->println("hello");

and that would fail for all of the non-dereference versions.

What is nullSerial? Where is that declared?

Is there a similar block above this which decides to #include the correct library? You can't just include all those libraries as they will clash with each other.

I think you will find that is an example from @dochawk and not code from @schwak the original poster.
Tom.. :slight_smile:

Oops I wasn't payin attention.

schwack:
I do not even know where to begin looking for the problem.

Find the simplest example which displays the problem. Post it here. Copy-paste the entire error message.

MorganS:
So this looks nothing like your first question.

err, it's not my question . . .

I did note the dereference. But you only do that on one.

correct. If the dbg type is HARDWARE, monSerial becomes an alias to the Serial device--you can't declare others of these. The others, however, are classes that need to be declared.

If that one is active then you would have to write

 monSerial->println("hello");

and that would fail for all of the non-dereference versions.

It works for all of them; that's how my cellphone talks to the device over BLT.

This is just an example clip out of a full-size sketch, to deal with his (apparent) desire to alias the Serial device (or otherwise to call it by another name)

What is nullSerial?

Nothing, of course ::slight_smile:

It's a subclass whose sole purpose is to get optimized out by the compiler.

While developing and debugging, I set the dbgTyp to HARDWARE, connect the nano by USB and use the Serial device for testing/output, and btTyp to ALTSOFTSERIAL.

Implemented, I'll end up directly connecting the BLE device to the UART (leaving me the option of waking up the device by a BLE signal, as I expect to have a couple of dozen solar/battery devices about the yards).

I need to get around to modifying a cable to supply power by USB but not serial, and monitoring of AltSoftSerial, but getting these plants into the ground is much higher priority. I still have two or three dozen tomatoes, peppers, and such, and a dozen each of four types of strawberry!

Where is that declared?

Is there a similar block above this which decides to #include the correct library? You can't just include all those libraries as they will clash with each other.

Yes, there is another block that determines which to include. Including that would have obfuscated the example, I think.

hawk