Spurious data going out over serial port that I haven't sent deliberately.

Is it possible for data to go out of the Serial port without explicitly being sent by your code?
Or is it possible to accidentally interfere with the Serial buffer through addressing out of bounds memory in an array etc in code?

I have an Arduino based midi instrument which generates midi commands when I press buttons. I've been updating the code for a few days now without testing which was a bad idea!
When I did test it I encountered a problem that while it worked in general sometimes it would turn on a whole bunch of notes at once. I've been using this code professionally for many years now with no problems. Something I did in the last few days programming caused this problem!! When I revert back to old code this bug doesn't manifest itself!
I have essentially three references to the offending serial port in the code. To try to figure out what is going on I attempted to print to the console "Serial" ANY data sent out on "Serial3".(Arduino mega)

void noteOn(byte channel, byte note, byte velocity) {
    Serial.println();
    midiMsg((0x90 | 0), note, velocity);
}
void midiMsg(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, HEX);
  Serial.print(data1, HEX);
  Serial.print(data2, HEX);

  Serial3.write(cmd);
  Serial3.write(data1);
  Serial3.write(data2);
}

void setup() {
  Serial3.begin(31250);// midi
  Serial.begin(250000);// console
}

So I play the instrument for a few minutes and suddenly a bunch of notes are turned on. But looking through the console output shows no extraneous "note on" commands being sent.
I attached a jumper wire from Serial3"TX" to Serial2 "RX" and added code to print this data back to the main Serial console in the loop().

if(Serial2.available()){
    Serial.print(Serial2.read(), HEX);
}

I can play for a few minutes and the output on the console matches the input. i.e. I get two identical midi messages (the one I intend to send out matches the one actually sent out), but when eventually the random bunch of notes gets turned on I can see there are extra commands being sent out that don't match what I'm trying to send out! I don't have access to the device to post the actual midi message here unfortunately but from what I've heard it's always an ascending sequence of notes (i.e. the note numbers in the command are going up).

To reiterate: the above functions contain the ONLY reference to Serial3 in my entire code! (9,000 lines!) Somehow more data is being sent out from outside this function. I don't think it's interference in the real world (as I said I've used this system for many years and it's been extremely reliable). I Don't think it's a problem with that specific serial port as I have tried a different serial port and got the same results. I also reduced the size of my RAM usage down to just 50% in case it was a stability issue but that made no difference.

I would really appreciate any help/suggestions on this as I'm getting sick of trying to debug and I can't use the new code professionally as it loudly turns on a lot of notes that I can't turn of without pressing the "panic"(all notes off) button on the synthesizer!
Many thanks in advance.

There's nothing much anybody is going to be able to do by looking at a half dozen print statements. The least you should do is post the bits of code that you changed along with the the original. Maybe something can be derived from seeing that.

It is nearly 9000 lines of code though! I would have changed maybe 100-200 lines of code over those few days. I could use a text comparator to figure out what I changed but, again, nothing I changed was anything to do with Serial3 or indeed anything to do with the performance/playing side of the software. I'm happy to do this if you think it would help but it would be a lot of code for you to look through. I'm concerned that maybe I'm changing the n+?th element of an array of size n and somehow corrupting other memory but again, I have 3600 references to arrays in my code so it would take days to check them all! I encountered this problem before with updating code and to the best of my knowledge it wasn't similar updates. I was under time constraint so I ended up having to backtrack to an earlier stable version and used that for further updates a year or so later. I'm still bemused that it's possible to corrupt the serial port though.

Or is it possible to accidentally interfere with the Serial buffer through addressing out of bounds memory in an array etc in code?

Absolutely it is - anything can happen!

Can also happen if you have crashed the stack into the heap, checkout: Arduino Playground - HomePage

Still, 100-200 lines of code is better than a) no code, b) 9000 lines.

Hi,

Something I did in the last few days programming caused this problem!! When I revert back to old code this bug doesn't manifest itself!

What did you add, and when you did it , were any new variables created that may be to small for their memory allocation.
Like int when you needed unsigned int or long ?

Tom... :slight_smile:

Ok Tom. I'll have a trawl through the code and check. I thought that any attempts to store a number larger than the datatype would simply wrap though rather than interfering with adjoining memory space?
Many thanks.