求个WM下串口操作的MFC的代码
小弟是个比较菜的菜鸟,试图用VS2008编写个WM下的MFC,主要想实现打开特定波特率的COM口,接受该COM的数据,写入到一个文本文件里,因为本人只是个初学者,特别是WM,只是刚刚学习,想求助下各位高手,望大家给予帮助,十分感谢! --------------------编程问答-------------------- 串口操作与MFC有什么关系? --------------------编程问答-------------------- 与PC下的没什么大区别,找个PC上的例子看明白了就会了。CreateFile...
...... --------------------编程问答--------------------
BOOL CRS232Device::OpenCommPort()--------------------编程问答-------------------- 楼上的正解,另外再看看COM口操作的一些文档方便理解。
{
TCHAR szPort[MAX_PATH];
if((m_nPort < 0) || (m_nPort > 10))
return FALSE;
if(m_hDevice != NULL)
CloseDevice();
wsprintf(szPort, _T("COM%d:"), m_nPort);
m_hDevice = CreateFile(szPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING,
0, 0);
if(m_hDevice == NULL)
{
m_hDevice = NULL;
return FALSE;
}
SetupComm( m_hDevice, 4096, 4096 ) ;
COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 100;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 100;
SetCommTimeouts(m_hDevice, &CommTimeOuts );
SetupEx(m_nByteSize, m_nStopBits, m_dwBaudRate, m_nParity);
EscapeCommFunction(m_hDevice, CLRIR);
COMSTAT ComStat;
DWORD dwErrors;
ClearCommError(m_hDevice, &dwErrors, &ComStat);
PurgeComm(m_hDevice, PURGE_TXCLEAR|PURGE_RXCLEAR);
return TRUE;
}
BOOL CRS232Device::CloseCommPort()
{
if (NULL == m_hDevice)
return TRUE;
SetCommMask( m_hDevice, 0);
PurgeComm( m_hDevice, PURGE_TXABORT | PURGE_RXABORT |PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle(m_hDevice) ;
m_hDevice = NULL;
return TRUE;
}
BOOL CRS232Device::ConnectDevice()
{
BOOL bRet;
bRet = OpenCommPort();
return bRet;
}
BOOL CRS232Device::CloseDevice()
{
CloseCommPort();
return TRUE;
}
int CRS232Device::SendData(char *pszData, int nSize)
{
DWORD nTotalWrite = 0, nWrite;
int nTrys = 0;
BOOL bWrite;
if (m_hDevice == NULL)
return 0;
while((nTrys < 4) && (nTotalWrite < (DWORD)nSize))
{
bWrite = WriteFile(m_hDevice, (LPBYTE)(pszData + nTotalWrite), nSize - nTotalWrite, &nWrite, NULL);
if (bWrite)
{
nTotalWrite += nWrite;
nTrys = 0;
}
else
{
nTrys ++;
Sleep(10);
}
}
FlushFileBuffers(m_hDevice);
return nTotalWrite;
}
int CRS232Device::RecvData(char *pszBuff, int nSize)
{
DWORD nTotalRead = 0, nRead, dwError;
BOOL bRead;
COMSTAT ComStat;
int nTrys = 0;
if (m_hDevice == NULL)
return 0;
do
{
bRead = ReadFile(m_hDevice, pszBuff + nTotalRead, nSize - nTotalRead, &nRead, NULL);
if (bRead && nRead > 0)
{
nTotalRead += nRead;
nTrys = 0;
}
else
{
nTrys ++;
Sleep(5);
ClearCommError(m_hDevice, &dwError, &ComStat);
}
}while((nTotalRead < nSize) && (nTrys < 4));
return nTotalRead;
}
void CRS232Device::SetupEx(WORD nByteSize, WORD nStopBits, DWORD dwBaudRate, WORD nParity)
{
m_nByteSize=nByteSize;
m_dwBaudRate=dwBaudRate;
m_nStopBits=nStopBits;
m_nParity=nParity;
// Set the COM Port.
if (m_hDevice != NULL && m_hDevice != INVALID_HANDLE_VALUE)
{
DCB dcb;
dcb.DCBlength = sizeof(DCB);
GetCommState(m_hDevice, &dcb);
dcb.BaudRate = m_dwBaudRate;
dcb.fParity = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.ByteSize = m_nByteSize;
dcb.Parity = m_nParity;
dcb.StopBits = m_nStopBits;
SetCommState(m_hDevice, &dcb);
}
}
补充:移动开发 , Windows Phone