Received Serial communication from Arduino is randomly garbled

Scott 1 Reputation point

Hi All,

I wrote a serial communication terminal emulation program, like PuTTY that I have been using without issue for many years without issue. It uses CreateFile, ReadFile and CloseHandle to access virtual serial ports.

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.

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.

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 ran across an article discussing IOCTL_SERIAL_LSRMST_INSERT IOCTL that I briefly thought might be enabled and inserting "random" data into the communication stream, so I tried adding code to disable it. Unfortunately, the code fails with system error 50 when it runs:

UCHAR   lpInBuffer[ 1 ] = { 0 };
DWORD   lpBytesReturned = 0;

if (
                            portinit[ i ],                  // handle to device
                            IOCTL_SERIAL_LSRMST_INSERT,     // dwIoControlCode
                            (LPVOID) lpInBuffer,            // input buffer 
                            (DWORD) sizeof( UCHAR ),        // size of input buffer 
                            NULL,                           // lpOutBuffer
                            0,                              // nOutBufferSize
                            (LPDWORD) lpBytesReturned,      // number of bytes returned
                            (LPOVERLAPPED) NULL             // OVERLAPPED structure
    TRACE( "DeviceIOControl Success %d\n", lpBytesReturned );
    TRACE( "DeviceIOControl failed %d\n", lpBytesReturned );
    TRACE( "Error %d (%s)\n", GetLastError( ), strerror( GetLastError( ) ) );

I'd appreciate any comments as to the root cause of the communication problem, or the correct use of this device IO control function.


Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
9,487 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Miles 1,236 Reputation points


    Since your program worked well until connected to a MEGA 2560 R3 board, we could check if your program adapt to this board.
    Also , we could take a look at whether the 16U2 causes the problem.

    For more specific information, you could ask at to get more help.

    Best Regards

    0 comments No comments

  2. Scott 1 Reputation point

    Hi Miles,

    Thanks for your reply. I am hesitant to take this approach because both the Arduino serial monitor and PuTTY work correctly. I'd bet there's some subtlety I am missing in handling the serial port control, but I don't know what it might be.


    0 comments No comments

  3. Miles 1,236 Reputation points

    Hi Scott

    We could check the 16U2 information in advance to check if there are conflicts with your program.
    The correlative information can only be found in their offical website.

    Best Regards

    0 comments No comments