问题如题,打开界面进行数据接收的时候,EDIT控件是已经禁止键盘和鼠标输入的,接收数据完成后有一个按钮是打开允许键盘鼠标输入的,没有进行串口通信接收数据时,这个按钮是有效的,可是接收数据之后,这个按钮就没有反应了,这是为什么呀!
解决方案 »
- 在用vc操作Word时,如何改写Word的宏
- 原来可以运行的程序,加了一点就不行了,大侠来看看哪错了
- 第一次发帖,希望高手帮忙解决问题VC++CSrollview的问题
- 怎样拦截其他窗口消息WM_ACTIVATE
- ADO:可否通过数字索引0、1、2来取得fields中的field对象?
- Intel IPP的问题。急!!!!!!(刚入职,第一个程序55555555555)
- 各位要注意身体啊,2015-2020年要打倭寇了!!~~~
- MFC多文档应用程序,如何取得所有视的内容?
- 不使用文档类,能否把视图类与框架类相关联?
- VC+SQL 有关无效的描述符索引
- MFC调用DOS命令,为何不能将参数传递给DOS啊
- vb代码翻译vc代码 人民币100元
然后操作成员函数(public member function)
或者友员
com.closeconnection();
没一点用,是怎么回事,关串口写在COM类里的
急 急急..........!
BOOL CComStatus::OpenConnection()
{
char csCom[10];
COMMTIMEOUTS CommTimeOuts ;// if((m_bComId < 0) || (m_bComId > 4))
// return FALSE;//从COM1到COM4
if(m_hCom)//if already open
return FALSE;
//OVERLAPPED包含异步I/O信息
m_rdos.Offset = 0;
m_rdos.OffsetHigh = 0;
m_rdos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_rdos.hEvent == NULL)
return FALSE;
m_wtos.Offset = 0;
m_wtos.OffsetHigh = 0;
m_wtos.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(m_wtos.hEvent == NULL)
{
CloseHandle(m_rdos.hEvent);
return FALSE;
}
wsprintf(csCom,"COM%d",m_bComId);
m_hCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL
); if(m_hCom == INVALID_HANDLE_VALUE) {
//dwError = GetLastError();
// handle error
return FALSE;
}
else
{
// get any early notifications SetCommMask( m_hCom, EV_RXCHAR ) ; // setup device buffers SetupComm( m_hCom, 4096, 4096 ) ; // purge any information in the buffer PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ; // set up for overlapped I/O
DWORD dwTemp = 1000 / (this->m_dwBaudRate / 8);
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;//((dwTemp > 0) ? dwTemp : 1);
CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
// CBR_9600 is approximately 1byte/ms. For our purposes, allow
// double the expected time per character for a fudge factor.
CommTimeOuts.WriteTotalTimeoutMultiplier = 2*CBR_9600/this->m_dwBaudRate;//( npTTYInfo ) ;
CommTimeOuts.WriteTotalTimeoutConstant = 0;//1000 ;
SetCommTimeouts( m_hCom, &CommTimeOuts ) ;
}
if(!SetupConnection())
{
CloseConnection();
return FALSE;
}
EscapeCommFunction( m_hCom, SETDTR );
m_bConnected = TRUE;
return TRUE;
}
BOOL CComStatus::CloseConnection()
{
if (NULL == m_hCom)
return ( TRUE ) ; // set connected flag to FALSE m_bConnected = FALSE;
// disable event notification and wait for thread
// to halt SetCommMask( m_hCom, 0 ) ; // block until thread has been halted //while(THREADID(npTTYInfo) != 0); // kill the focus //KillTTYFocus( hWnd ) ; // drop DTR EscapeCommFunction( m_hCom, CLRDTR ) ; // purge any outstanding reads/writes and close device handle PurgeComm( m_hCom, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle( m_hCom ) ;
m_hCom = NULL;
// change the selectable items in the menu
CloseHandle(m_rdos.hEvent);
CloseHandle(m_wtos.hEvent); return ( TRUE ) ;
}还有一个问题就是,如果我有判断语句if(::ReadCommBlock(m_comDev,inbuf,57)==57)后面的是从缓冲区读出来的语句,如果下位机缓冲区没有写到57这么大,譬如只写了24个字符,则我的界面上读不到发送过来的数据,是怎么回事
{
BOOL fReadStat ; COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
char szError[ 10 ] ;
/*NPTTYINFO npTTYInfo ; if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
return ( FALSE ) ;
*/
// only try to read number of bytes in queue
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ; if (dwLength > 0)
{
fReadStat = ReadFile( comDev.m_hCom, lpszBlock,
dwLength, &dwLength, &(comDev.m_rdos) ) ;
/* if(WaitForSingleObject(comDev.m_rdos.hEvent,5000) == WAIT_OBJECT_0)
{
GetOverlappedResult( comDev.m_hCom ,
&(comDev.m_rdos), &dwLength, TRUE );
//lpszBlo[255] = '0';
//AfxMessageBox(buf);
}*/
if (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
OutputDebugString("\n\rIO Pending");
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( comDev.m_hCom ,
&(comDev.m_rdos), &dwLength, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
// normal result if not finished
continue;
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
break;
} } }
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
}
}
}// CComStatus::CloseConnection(); return ( dwLength ) ;}