|
本帖最后由 tengmo535 于 2011-4-14 08:47 编辑
[attach]153[/attach
主要代码:(附件里的串口连接改用 ACE_DEV_Connector)
1。开启handle_event
LPVOID WorkThread(LPVOID lpParam)
{
int success=1;
ACE_Proactor *p1=new ACE_Proactor;
ACE_Proactor::instance(p1, 1);
ACE_SerialPort_Connector<ACE_Processor> portCon;
if(portCon.connect(p1,_T("COM2"),baud9600,eightBits,noParity,One,NoControl,0))
{
while(success>0)//&&!done
success=p1->handle_events();
}
delete p1;
return NULL;
}
void CMainFrame::OnMenuMainOpenPort()
{
ACE_Thread::spawn(
(ACE_THR_FUNC)WorkThread, //线程执行函数
this, //执行函数参数
THR_JOINABLE | THR_NEW_LWP|THR_USE_AFX,
NULL,
NULL
);
}
2。串口模板类ACE_SerialPort_Connector<ACE_Processor>代码
template <class HANDLER>
class ACE_SerialPort_Connector :public ACE_Handler
{
public:
ACE_SerialPort_Connector(void)
{
m_handle=NULL;
};
virtual ~ACE_SerialPort_Connector(void)
{
// if(m_handle)
// CloseHandle(m_handle);
};
virtual int connect(ACE_Proactor *p,CString sPortNum,BaudRate baud,DataLength data,Parity parity,StopBits stop,FlowControl flow,u_long ict);
HANDLER* make_handler(void);
protected:
virtual int ConfigureCommPort(BaudRate baud,DataLength data,Parity parity,StopBits stop,FlowControl flow,u_long ict = 0);
private:
ACE_HANDLE m_handle;
};
/**********************************************************/
template <class HANDLER>
int ACE_SerialPort_Connector<HANDLER>::connect(ACE_Proactor *p,CString sPortNum,BaudRate baud,DataLength data,Parity parity,StopBits stop,FlowControl flow,u_long ict)
{
if(m_handle)
CloseHandle(m_handle);
m_handle = CreateFile(sPortNum,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
// No specific flags needed
if(m_handle != ACE_INVALID_HANDLE)
{
if(ConfigureCommPort(baud,data,parity,stop,flow))
{
HANDLER * svc = make_handler();
if(svc!=NULL)
{
svc->proactor(p);// Set the proactor for the port;
ACE_Message_Block *mb = NULL ;
ACE_NEW_RETURN(mb,ACE_Message_Block((size_t)0),-1);
svc->open (m_handle,*mb);
mb->release();
return 1;
}
}
}
m_handle = NULL;
return 0;
}
template <class HANDLER>
HANDLER* ACE_SerialPort_Connector<HANDLER>::make_handler(void)
{
HANDLER *tHandle = NULL ;
ACE_NEW_RETURN(tHandle,HANDLER,NULL);
return tHandle;
}
template <class HANDLER>
int ACE_SerialPort_Connector<HANDLER>::ConfigureCommPort(BaudRate baud,DataLength data,Parity parity,StopBits stop,FlowControl flow,u_long ict = 0)
{
DCB dcb;
if(GetCommState(m_handle, &dcb))
{
dcb.BaudRate = baud;
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.ByteSize = data;
dcb.Parity = parity;
dcb.StopBits = stop;
switch(flow)
{
case Software:
dcb.fOutX = 1;
dcb.fInX = 1;
break;
case Hardware:
dcb.fOutxCtsFlow = 1;
dcb.fRtsControl =
RTS_CONTROL_HANDSHAKE;
break;
}
if(SetCommState(m_handle, &dcb))
{
PurgeComm(m_handle, PURGE_TXCLEAR|PURGE_RXCLEAR);
return 1;
}
}
return 0;
}
3.数据接收发送处理类ACE_Processor代码
class ACE_Processor :public ACE_Handler
{
public:
ACE_Processor(void);
~ACE_Processor(void);
virtual void open (ACE_HANDLE handle, ACE_Message_Block &message_block);
CMainFrame* pMain;
// void DataProcessing(ACE_Message_Block* mb);//处理
protected:
virtual void handle_read_file(const ACE_Asynch_Read_File::Result &result);
virtual void handle_write_file(const ACE_Asynch_Write_File::Result &result);
private:
int initiate_read_stream (void);
ACE_Asynch_Read_File rs_;
ACE_Asynch_Write_File wr_;
}; |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?用户注册
×
|