Fixed:Serial port spewing garbage after uploading this ino package (atMega2560)

Edit: This was resolved by removing all the conditional compilation preprocessor directives from my ino file.
I don’t know why but it works now.

I’m having trouble flashing a program to my arduino mega 2560. I’ve built a low memory json processor for the arduino line, as well as desktops and servers. The same package is compilable for all from 8 to 64 bits.

Well, it seems when I flash the code to my lil mega I only get garbage from the serial port.

I’ve even reduced setup to the following:

void setup() {
  Serial.begin(115200); // also tried 9600
  Serial.println("Hello");
}

And it still fails with garbage.
Yes my baud rate in my serial monitor is correct. I’ve tried this with a regular mega 2560, and a mega 2560+wifi r3 same result.

I’m including the entire package as a zip since it’s pretty substantial. Forgive all the conditional preprocessor stuff in the ino file. That same file works as a cpp file for regular C++ compilation, hence all the conditional stuff.

Keep in mind that aside from declaring some globals, the above is all it does. Something about either the globals, or the program code itself is corrupting the output.

The code works, compiles and runs on my linux pc. It just is corrupting my arduino even though i’m not even calling it

jsonTest.zip (53.6 KB)

And it still fails with garbage.
Yes my baud rate in my serial monitor is correct. I've tried this with a regular mega 2560, and a mega 2560+wifi r3 same result.

I would suggest that it is not.

Is there anything else in the wiring you are not telling us about. That code works on my Mega with no problem. Once you correct the 'mistake' of having no loop function.

I've built a low memory json processor for the arduino line, as well as desktops and servers. The same package is compilable for all from 8 to 64 bits.

This doesn't make sense, you are saying that the same code that runs on a desktop computer and a server also runs on an Arduino Mega? I'm sure it doesn't. Or if it does then if you have that level of knowledge fixing this should be simple for you.

Maybe someone will be willing to download all your code but you are more likely to get help if you post a short program that illustrates the problem.

honey_the_codewitch:
I've even reduced setup to the following:

Post the complete program that goes with that version of setup().

Are you sure that the simple program is actually getting uploaded?

...R

I'm a bit surprised it even compiles, since there is no loop() function in any of the code, but the complexity is beyond my programming knowledge.

Try putting a Serial.flush() after the print statement in setup(), that will at least delay the rest of the code until the serial data has been sent, my guess is that after exiting setup() something else in the code is corrupting the serial buffer before it has time to send the text.

GrumpyMike, if it's not then the Arduino IDE and PlatformIO both have bugs with registering a baud they aren't really communicating in. That doesn't seem likely, so I'd respectfully suggest that it is.

Perry,

A) If I didn't need help I wouldn't be here. There's nothing in it for me. Understand motive

B) I come on here and because you've never done something yourself you're sure I'm lying? That's not very civil of you. Is this really how the adults in this forum behave?

C) If I could reproduce the problem with a short program I would. I've never encountered the problem with any other ino

Do better.

PS: It runs now. Did you need screen shots? A video? Would that elicit an apology?

Robin2, it's attached with a zip. It is several files so the ino will not help you if I just paste it here.

I'm pretty sure it's gettting uploaded because I upload it from the ArduinoIDE and it says it did it. Also I can upload other sketches (and the serial port works fine with them)

Hi all,

This works now after I removed the conditional preprocessor directives from the ino file.

I'm not sure why, but I guess I'll just have a separate ino and CPP file.

I really wish the Arduino IDE didn't mangle your C++ "for you"

It doesn't work right.

Oh well.

honey_the_codewitch:
Perry,

A) If I didn't need help I wouldn't be here. There's nothing in it for me. Understand motive

B) I come on here and because you've never done something yourself you're sure I'm lying? That's not very civil of you. Is this really how the adults in this forum behave?

Do better.

A) Obviously you need help, that's why you are asking for it, what makes you think I think otherwise? I get nothing from helping. Understand motive.

B) Lying? What the??? Where on earth do you get that I am suggesting you are lying? For clarity, I don't think you are lying and you should not infer that from anything I said.

Is this really how the adults in this forum behave?

It is my experience of this forum, and other international spaces such as Facebook, that cultural and language differences sometimes mean that someone can post a well intentioned, polite in the opinion of the person posting, response can be viewed as unhelpful and or rude by their interlocutor. Perhaps this is the case here?

Do you need me to rephrase what I originally said or, now you know I don't think you are lying, do you understand what I meant?

honey_the_codewitch:
Robin2, it's attached with a zip. It is several files so the ino will not help you if I just paste it here.

There was little point in simplifying the setup() function as in your Original Post without also simplifying the rest of the program.

...R

honey_the_codewitch:
This works now after I removed the conditional preprocessor directives from the ino file.

At that point, I'd add the preprocessor directives back one at a time until I found the one that caused a problem.

Perry, you said a claim I made didn't make sense. If you're not accusing me of lying then I don't know what you meant by that. Anyway, if you meant something else, then I misunderstood you, and I'm sorry.

PS: The claim is accurate and makes perfect sense and the code is now working on an 8-bit mega. It also compiles and runs on my 64-bit desktop machine - where it screams. C++ is made for this sort of thing.

honey_the_codewitch:
Perry, you said a claim I made didn't make sense. If you're not accusing me of lying then I don't know what you meant by that. Anyway, if you meant something else, then I misunderstood you, and I'm sorry.

PS: The claim is accurate and makes perfect sense and the code is now working on an 8-bit mega. It also runs on my desktop machine. C++ is made for this sort of thing.

Possible meanings of 'didn't make sense':

  • What you said was perfectly true but badly stated.
  • What you said was perfectly true, well stated but I didn't properly understand it.

I know C++ is perfectly good on a micro-controller and a PC, however, the thing makes a big difference is the available interfaces on the 2 devices. Micro-controllers typically have individual input and output pins, I2C, SPI and all sorts of other goodies not typically found on a PC. A PC has a nice, big, easy to read monitor, a micro-controller doesn't. For these reasons code that is good on one will not be good on the other in many cases.

Anyway, you seem to have got good advice from others, so good luck with your project.

Thanks.

As far as the screen and all that noise, this isn’t device dependent. It’s a pull parsing JSON library with some in-memory tree support, designed to run on machines with as little as 2kB

I’ll give you an example output (from my ESP32):
… (clipped)
S07E10 Things Unseen
S07E11 Tipping Point
S07E12 Sea Change
S07E13 Reckoning
Scanned 112 episodes and 191288 characters in 2333 milliseconds using 71 bytes of the pool

on a desktop (my rickety old linux machine):
… (clipped)
S07E10 Things Unseen
S07E11 Tipping Point
S07E12 Sea Change
S07E13 Reckoning
Scanned 112 episodes and 191288 characters in 4 milliseconds using 23 bytes of LexContext and 71 bytes of the pool

on the ATMega2560 (the position overflows Serial.print() but other than that it works)
…(clipped)
S07E09 Bitter Pill
S07E10 Things Unseen
S07E11 Tipping Point
S07E12 Sea Change
S07E13 Reckoning
Scanned 112 episodes and -5320 characters in 9425 milliseconds using 23 bytes of LexContext and 50 bytes of the pool

The memory usage is slightly less on the mega because pointer sizes are smaller.

Basically what it’s doing is querying an approx 200kB file looking for episode data, extracting it, and formatting that output.

The memory usage for that was to perform a query like this $…episodes.(episode_id, name and season_id)

honey_the_codewitch:
GrumpyMike, if it's not then the Arduino IDE and PlatformIO both have bugs with registering a baud they aren't really communicating in. That doesn't seem likely, so I'd respectfully suggest that it is.

So you said that

I've even reduced setup to the following:

void setup() {

Serial.begin(115200); // also tried 9600
  Serial.println("Hello");



}

And it still fails with garbage.

That has nothing to do with any compiler directives, I read that as just the code you were using, nothing else except put an empty loop function in it to make it compile. So I'd respectfully suggest you to ask better formed questions.

"I came up with a suggestion that you already said wasn't the case so you need to ask better questions" - paraphrasing an adult in the room.

In case you missed it, I solved it by removing the preprocessing directives from the ino file

Ah, finally had a good look at your code, you misspelled ARDUINO on line 32 in the .ino file. Cannot get it to compile here unless I add an empty loop() function.

#ifndef ARDUNIO  // <<<< misspelling of ARDUINO 
int main(int argc, char** argv) {
#else
void setup() {
    Serial.begin(115200);
    Serial.println("Hello");
    return;
     // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(53, OUTPUT);     // change this to 53 on a mega


    if(!SD.begin(53)) {
      Serial.println("Unable to mount SD");
      while(true); // halt
    }
#endif
    //showFileNodes();
    //showSkipVsRead();
    //showShowName();
    //showProductionCompanies();
    //showSeasonIndex2EpisodeIndex2();
    //showNetworksArray();
    //showShowExtraction(false);
    //showShowExtraction(true);
    showEpisodes();
}