|
BOOL CCESeries::OpenPort(CWnd* pPortOwner,/*使用串口类,窗体句柄*/
UINT portNo,/*串口号*/
UINT baud,/*波特率*/
UINT parity,/*奇偶校验*/
UINT databits,/*数据位*/
UINT stopbits/*停止位*/
)
{
DCB commParam;
TCHAR szPort[15];
// 已经打开的话,直接返回
if (m_hComm != INVALID_HANDLE_VALUE)
{
return TRUE;
}
ASSERT(pPortOwner != NULL);
ASSERT(portNo > 0 && portNo < 5);
//设置串口名
wsprintf(szPort, L"COM%d:", portNo);
//打开串口
m_hComm = CreateFile(
szPort,
GENERIC_READ | GENERIC_WRITE, //允许读和写
0, //独占方式(共享模式)
NULL,
OPEN_EXISTING, //打开而不是创建(创建方式)
0,
NULL
);
if (m_hComm == INVALID_HANDLE_VALUE)
{
// 无效句柄,返回。
TRACE(_T("CreateFile 返回无效句柄"));
return FALSE;
}
// 得到打开串口的当前属性参数,修改后再重新设置串口。
// 设置串口的超时特性为立即返回。
if (!GetCommState(m_hComm,&commParam))
{
return FALSE;
}
commParam.BaudRate = baud;// 设置波特率
commParam.fBinary = TRUE;// 设置二进制模式,此处必须设置TRUE
commParam.fParity = TRUE;// 支持奇偶校验
commParam.ByteSize = databits;// 数据位,范围:4-8
commParam.Parity = NOPARITY;// 校验模式
commParam.StopBits = stopbits;// 停止位
commParam.fOutxCtsFlow = FALSE; // No CTS output flow control
commParam.fOutxDsrFlow = FALSE; // No DSR output flow control
commParam.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
commParam.fDsrSensitivity = FALSE;// DSR sensitivity
commParam.fTXContinueOnXoff = TRUE;// XOFF continues Tx
commParam.fOutX = FALSE;// No XON/XOFF out flow control
commParam.fInX = FALSE; // No XON/XOFF in flow control
commParam.fErrorChar = FALSE;// Disable error replacement
commParam.fNull = FALSE;// Disable null stripping
commParam.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
commParam.fAbortOnError = FALSE;// 当串口发生错误,并不终止串口读写
if (!SetCommState(m_hComm, &commParam))
{
TRACE(_T("SetCommState error"));
return FALSE;
}
//设置串口读写时间
COMMTIMEOUTS CommTimeOuts;
GetCommTimeouts (m_hComm, &CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
if(!SetCommTimeouts( m_hComm, &CommTimeOuts ))
{
TRACE( _T("SetCommTimeouts 返回错误") );
return FALSE;
}
m_pPortOwner = pPortOwner;
//指定端口监测的事件集
SetCommMask (m_hComm, EV_RXCHAR);
//分配设备缓冲区
SetupComm(m_hComm,512,512);
//初始化缓冲区中的信息
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
m_hReadCloseEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
m_hWriteCloseEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
//创建读串口线程
m_hReadThread = CreateThread(NULL,0,ReadThreadFunc,this,0,&m_dwReadThreadID);
//创建写串口线程
m_hWriteThread = CreateThread(NULL,0,WriteThreadFunc,this,0,&m_dwWriteThreadID);
TRACE(_T("串口打开成功"));
return TRUE;
}
//串口读线程函数
DWORD CCESeries::ReadThreadFunc(LPVOID lparam)
{
CCESeries *ceSeries = (CCESeries*)lparam;
DWORD evtMask;
BYTE * readBuf = NULL;//读取的字节
DWORD actualReadLen=0;//实际读取的字节数
DWORD willReadLen;
DWORD dwReadErrors;
COMSTAT cmState;
// 清空缓冲,并检查串口是否打开。
ASSERT(ceSeries->m_hComm !=INVALID_HANDLE_VALUE);
//清空串口
PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR );
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
while (TRUE)
{
if (WaitCommEvent(ceSeries->m_hComm,&evtMask,0))
{
SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
//表示串口收到字符
if (evtMask & EV_RXCHAR)
{
ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
willReadLen = cmState.cbInQue ;
if (willReadLen <= 0)
{
continue;
}
readBuf = new BYTE[willReadLen];
ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
//如果读取的数据大于0,
if (actualReadLen>0)
{
//触发读取回调函数
ceSeries->m_OnSeriesRead(ceSeries->m_pPortOwner,readBuf,actualReadLen);
}
}
}
//如果收到读线程退出信号,则退出线程
if (WaitForSingleObject(ceSeries->m_hReadCloseEvent,500) == WAIT_OBJECT_0)
{
break;
}
}
return 0;
}
void CWinceSerialCommDlg::OnSeriesRead(CWnd* pWnd,BYTE* buf,int bufLen)
{
CWinceSerialCommDlg *pDlg = (CWinceSerialCommDlg *)pWnd;
CEdit * pEdit;
pEdit = (CEdit*)pDlg->GetDlgItem(IDC_EDTRECV);
CString strRecv = (char*)buf;
CString strOld;
pEdit->GetWindowText(strOld);
pEdit->SetWindowText(strOld + strRecv);
delete[] buf;
}
为什么wince下串口一直读不到数据啊?高手帮忙啊,急死了~~~
|
|