问题描述
大家好,
希望有人可以指出我的方式错误。下面是我打开和写入串口数据的代码。我知道它可能远远不够完美,但它就在我的位置!
我已检查串口是否正常使用串口监视工具。
没有编译或者将错误与代码链接,我也没有使用GetLast错误报告任何错误。
我想我已经将错误范围缩小到下面BOLD标记的区域在WriteData函数中。
out缓冲区中有大小为[58]的数据,但是在调用ComStat.cbInQue之后查看时的dwBytesSent等于0,因此没有数据被传输。
我完全猜测重叠操作中有一些错误,好像我删除了这一部分我得到错误代码997.
任何帮助纠正这个问题或者绕过它的方法都是非常值得赞赏的。我在互联网,论坛和MSDN上看了很多例子,但看不出错误的位置。大多数其他例子看起来都一样。
欢呼寻找。
Daz
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
//////////////////////////////////////////////////////
// Opens specified com port at specified rate
//////////////////////////////////////////////////////
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 );
}
//////////////////////////////////////////////////////
// Write data output
//////////////////////////////////////////////////////
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 );
}
推荐答案
这篇关于串口WriteData缓冲区大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!