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 (
DeviceIoControl(
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 );
else
{
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.
Thanks,
Scott