M
Michael Varghese Thomas
Guest
I just started working on serial communication in VC++ and am facing an issue while reading the data (surprise surprise) from the receiver port.
All the code is supposed to do is take input from one edit box, send it to COM2, COM2 is linked with COM3, and this data should be read from COM3. I have used the Serial Class made by Tom Archer and Rick Leinecker, from this link http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm
In their definition of their ReadData function, they use the ComStat.cbInQue function determine the number of bytes read. This always remained 0. So I added a WaitforData function to wait for some data to arrive. But the number of bytes read is still 0.
Any ideas why this might be happening?
This is the definition for the ReadData function:
int CSerial::ReadData( void *buffer, int limit )
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
CCommTestDlg::WaitForData(17, 50); // I have specified the exact number of bytes it is supposed to receive: 17
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 );
dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;
bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );
}
This is where it applies in the main code:
CSerial serial;
CSerial serial2;
serial.Open(2, 9600); // Handle.Open(port number, baudrate)
serial2.Open(3, 9600);
if (serial.Open(2, 9600))
{
static char szMessage[] = "This is test data";
int nBytesSent = serial.SendData(szMessage, strlen(szMessage));
ASSERT(nBytesSent == strlen(szMessage));
}
else
AfxMessageBox("Failed to open port!");
if (serial2.Open(3, 9600))
{
char* lpBuffer = new char[500]; // Things start going downhill from here.
int nBytesRead = serial.ReadData(lpBuffer, 500);
if(nBytesRead>0)
AfxMessageBox("Done");
else
AfxMessageBox("Not Done");
delete []lpBuffer;
}
else
AfxMessageBox("Failed to open port!");
serial.Close();
serial2.Close();
PS:
I can see that COM3 is receiving the exact number of bytes, I just am not able to gain access to it.
Continue reading...
All the code is supposed to do is take input from one edit box, send it to COM2, COM2 is linked with COM3, and this data should be read from COM3. I have used the Serial Class made by Tom Archer and Rick Leinecker, from this link http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm
In their definition of their ReadData function, they use the ComStat.cbInQue function determine the number of bytes read. This always remained 0. So I added a WaitforData function to wait for some data to arrive. But the number of bytes read is still 0.
Any ideas why this might be happening?
This is the definition for the ReadData function:
int CSerial::ReadData( void *buffer, int limit )
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
CCommTestDlg::WaitForData(17, 50); // I have specified the exact number of bytes it is supposed to receive: 17
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 );
dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;
bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );
}
This is where it applies in the main code:
CSerial serial;
CSerial serial2;
serial.Open(2, 9600); // Handle.Open(port number, baudrate)
serial2.Open(3, 9600);
if (serial.Open(2, 9600))
{
static char szMessage[] = "This is test data";
int nBytesSent = serial.SendData(szMessage, strlen(szMessage));
ASSERT(nBytesSent == strlen(szMessage));
}
else
AfxMessageBox("Failed to open port!");
if (serial2.Open(3, 9600))
{
char* lpBuffer = new char[500]; // Things start going downhill from here.
int nBytesRead = serial.ReadData(lpBuffer, 500);
if(nBytesRead>0)
AfxMessageBox("Done");
else
AfxMessageBox("Not Done");
delete []lpBuffer;
}
else
AfxMessageBox("Failed to open port!");
serial.Close();
serial2.Close();
PS:
I can see that COM3 is receiving the exact number of bytes, I just am not able to gain access to it.
Continue reading...