多线程,多接收模式串口类LsComm
2010-06-23 20:40:59 来源:WEB开发网写串口数据的过程同上
注意:第3,4种模式为自动接收模式,在用以前你必须先定义回调函数,然后,设置类的接收函数
/* 回调函数定义 */
void OnReceiveData(LPVOID pSender,void* pBuf,DWORD InBufferCount)
{
CString c;
byte a[100];
char b[4]="";
memcpy(a,pBuf,InBufferCount);
CLsCommDemoDlg* pDlg = (CLsCommDemoDlg*) pSender;
CListBox*pList =(CListBox*)pDlg->GetDlgItem(IDC_LIST1);
for(int i=0;i<(int)InBufferCount;i++)
{
::sprintf(b,"%2.2X",a[i]);
c+=" ";
c+=b;
}
c="接收(Receive):"+c;
pList->AddString(c);
}
3.AutoReceiveBySignal 自动信号接收模式
#include "ComPort.h"
LsComm::CComPort m_ComPort;//LsComm is namespace in c++
m_ComPort.Open(2,LsComm::CComPort::AutoReceiveBySignal );
m_ComPort.SetReceiveFunc((FOnReceiveData)OnReceiveData,this);
m_ComPort.SetBreakHandleFunc(OnComBreak);
//ReCeive Com Data:接收数据函数
in OnReceiveData(LPVOID pSender,void* pBuf,DWORD InBufferCount) //above
//write data
the same as the first mode;
4.AutoReceiveByBreak 中断接收模式
#include "ComPort.h"
LsComm::CComPort m_ComPort;//LsComm is namespace in c++
m_ComPort.Open(2,LsComm::CComPort::AutoReceiveByBreak );
m_ComPort.SetReceiveFunc((FOnReceiveData)OnReceiveData,this);
//ReCeive Com Data:接收数据函数
in OnReceiveData(LPVOID pSender,void* pBuf,DWORD InBufferCount) //above
//write data
the same as the first mode;
另外说明2点:
(1)如果你需要处理中断事件,你可以在每种模式中设置中断接收事件:如下
//定义中断事件接收函数
void OnComBreak(LPVOID pSender,DWORD dwMask,COMSTAT stat)
{
//deal with the break of com here
}
m_ComPort.SetBreakHandleFunc(OnComBreak); //设置中断事件
(2)如何处理如,改变波特率,以及其它参数呢?
m_ComPort.GetSerialPort()可以获得一个CSerialPort类的指针,如何就可以操作各种com属性了.
DCB dcb;
this->m_ComPort.GetSerialPort()->GetState(dcb);
更多精彩
赞助商链接