Hi All,
Hopefully someone can point out the error of my ways. Below is the code I have for opening and writing data to a serial port. I know its propbably nowhere near perfect but its where I am!.
I have checked the serial port is working correctly with a serial port monitor tool.
There are no compilation or linking errors with the code, I also do not get any errors reported using GetLast Error.
I think I have narrowed the error down to there area marked in BOLD below in the WriteData function.
There is data in the out buffer of size [58], but the dwBytesSent when looked at after the call to ComStat.cbInQue, equals 0, so no data is then tramsmited.
I'm totally guessing that theres some error in the overlapped operation as if I delete this section I get error code 997.
Any help to rectify this or a way round it would be most appreciated. I have looked at many examples on the internet, forums and MSDN but cannot see where the error may be. Most other examples look the same.
cheers for looking.
Daz
BOOL CSerial::OpenComPort( const char* nPort, int nBaud )
{
if( m_bOpened ) return( TRUE );
DCB dcb;
comport = CreateFile( nPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
if( comport == NULL )
return( FALSE );
if (comport == INVALID_HANDLE_VALUE)
{
printf("Create file failed with error %d.\n", GetLastError());
return (FALSE);
}
memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts( comport, &CommTimeOuts );
m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
dcb.DCBlength = sizeof( DCB );
GetCommState( comport, &dcb );
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.fParity = FALSE;
dcb.StopBits = ONESTOPBIT;
unsigned char ucSet;
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
if( !SetCommState( comport, &dcb ) || !SetupComm( comport, 10000, 10000) ||
m_OverlappedRead.hEvent == NULL || m_OverlappedWrite.hEvent == NULL )
{
DWORD dwError = GetLastError();
if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent );
CloseHandle( comport );
printf("SetCommState file failed with error %d.\n", GetLastError());
return( FALSE );
}
m_bOpened = TRUE;
printf(TEXT("baud = %d, databits = %d, Parity = %d, Stop = %d\n"),
dcb.BaudRate, dcb.ByteSize, dcb.fParity, dcb.StopBits);
printf(TEXT("Port = %s\n"), nPort);
return( m_bOpened );
}
int CSerial::WriteData( void *OutBuffer, int limit )
{
if( !m_bOpened || comport == NULL ) return( 0 );
BOOL bWriteStatus;
DWORD dwBytesSent, dwErrorFlags;
COMSTAT ComStat;
ClearCommError( comport, &dwErrorFlags, &ComStat );
printf("limit in is = %i\n", limit);
if( !ComStat.cbInQue)
{
dwBytesSent = (DWORD) ComStat.cbInQue;
}
if( limit < (int) dwBytesSent )
{
dwBytesSent = (DWORD) limit;
} printf("data buffer to be sent is = %s\n", OutBuffer);
printf("dwBytesSent = %i\n", dwBytesSent);
bWriteStatus = WriteFile (comport, OutBuffer, dwBytesSent, &dwBytesSent, &m_OverlappedWrite);
GetOverlappedResult(comport, &m_OverlappedWrite, &dwBytesSent, TRUE);
if( !bWriteStatus )
{
printf("writestatus failed with error %d.\n", GetLastError());
}
else
{
if( GetLastError() == ERROR_IO_PENDING )
{
WaitForSingleObject( m_OverlappedRead.hEvent, 17 );
printf("writestatus failed with error %d.\n", GetLastError());
return( (int) dwBytesSent );
return( 0 );
}
}
printf("dwBytesSent is %i\n", dwBytesSent);
return( (int) dwBytesSent );
}