关于创建线程在win98蓝屏的现象

yangzhao202 2008-04-28 03:46:03
bool CComPort::Open(int nPort,ReceiveMode mode, DWORD dwBaud, Parity parity, BYTE DataBits,
StopBits stopbits,FlowControl fc)
{
//1.新建串口
if(this->m_pPort)
delete this->m_pPort;
this->m_pPort = new CSerialPort();
this->m_bIsOpen = false;
//2.判断收发模式
if(mode==ReceiveMode::ManualReceiveByQuery)
{
this->m_IsOverlapped = false;
}
else
{
this->m_IsOverlapped = true;
}
this->m_RecvMode = mode;

//3.转换参数,打开串口
int index;
index=parity-CComPort::EvenParity;
CSerialPort::Parity spParity=(CSerialPort::Parity)(CSerialPort::EvenParity+index);
index=stopbits-CComPort::OneStopBit;
CSerialPort::StopBits spStopbits=(CSerialPort::StopBits)(CSerialPort::OneStopBit+index);
index=fc-CComPort::NoFlowControl;
CSerialPort::FlowControl spFC=(CSerialPort::FlowControl)(CSerialPort::NoFlowControl+index) ;

try
{
this->m_pPort->Open(nPort,dwBaud,spParity,DataBits,spStopbits,spFC,m_IsOverlapped);
}
catch(CSerialException* pE)
{
//AfxMessageBox(pE->GetErrorMessage());
pE->Delete();
return false;
}

//it is important!!
COMMTIMEOUTS timeouts;
this->m_pPort->GetTimeouts(timeouts);
timeouts.ReadIntervalTimeout=10;
//timeouts.WriteTotalTimeoutConstant = 1000;
this->m_pPort->SetTimeouts(timeouts);

this->m_pPort->Purge(PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
this->m_pPort->Setup(4096,4096);

this->m_CurPortNum = nPort;
//创建关闭事件
this->m_hCloseEvent = CreateEvent(NULL,true,false,NULL);
if(this->m_hCloseEvent==NULL)
{
return false;
}
//4.创建线程类
this->m_pReadThread = new CReadComThread();
this->m_pReadThread->BandSerialPort(this);
this->m_pReadThread->Create();
this->m_pReadThread->Resume();

if(this->IsOverlapped())
{

this->m_hWriteEvent = ::CreateEvent(NULL,true,false,NULL);
if(this->m_hCloseEvent==NULL)
return false;
this->m_WriteOverlapped.hEvent = this->m_hWriteEvent;
}
this->m_bIsOpen = true;
return true;
}

以上是打开串口,调用的是CSerialPort控件!
现在问题是一关闭就会蓝屏,CSerialPort肯定没问题了,我想肯定错误出现在线程,以前没做过没有经验。大家帮帮我,下一页写上close



...全文
58 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangzhao202 2008-04-29
  • 打赏
  • 举报
回复
不是啊,你怀疑是线程的错?为什么XP就通过了呢
Chivalry 2008-04-28
  • 打赏
  • 举报
回复
这个CReadComThread是谁提供的?CSerialPort?
yangzhao202 2008-04-28
  • 打赏
  • 举报
回复
Close函数:


void CComPort::Close()
{
//1.串口
if(this->m_pPort==NULL)
return;
if(!this->m_pPort->IsOpen())
return;
//2.事件
::SetEvent(this->m_hCloseEvent);//通知关闭系统
Sleep(1000);
//3.结束读线程
try
{
this->m_pReadThread->Terminate();
delete this->m_pReadThread;
this->m_pReadThread = NULL;
}
catch(char e[150])
{
::AfxMessageBox(e);
}

//4.结束关闭线程
::CloseHandle(this->m_hCloseEvent);
this->m_pPort->Close();

//5.结束写事件
if(this->m_hWriteEvent)
{
::CloseHandle(this->m_hWriteEvent);
this->m_hWriteEvent = NULL;
}
//6.释放串口对象
if(this->m_pPort)
{
delete this->m_pPort;
this->m_pPort = NULL;
}
}

15,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧