I originally posted this on Microsoft. They suggested I post this here too:
I wrote a serial communication terminal emulation program, like PuTTY that I have been using without issue for many years. It uses CreateFile, ReadFile and CloseHandle to access virtual serial ports, and literally works with hundreds of devices I've developed over the years (I am a hardware/firmware developer).
When connecting to a MEGA 2560 R3 board (that uses the MEGA16U2 USB to serial interface) at 9600,N,8,1, I receive randomly and intermittently garbled data from the Arduino. Perhaps a dozen non-text characters every few hundred or so. The exact same code runs correctly on a Arduino Feather M0 and Feather M0 WiFi.
I have used an oscilloscope to monitor the main CPU's serial output to the 16U2 interface and know its data is intact, which means the problem lies somewhere between the 16 and the PC code. I have used both the Arduino serial monitor and PuTTY and don't see the same corruption with these programs.
I've updated the 16U2 firmware according to several online posts discussing this exact problem, but that hasn't changed the problem. The Arduino code is simple. In setup I initialize Serial.begin(9600), and the data I'm writing is written with Serial.print() and Serial.println() statements.
I've been forward and backward through my code and can't seem to track down the problem. I know it isn't a serial communication error handling problem: parity, frame errors, BAUD rate mismatch, etc. because I have connected with other serial systems to generate these kinds of errors and my program correctly identifies and reports them.
I'd appreciate any comments as to the root cause of the communication problem.