返回首页

亲爱的同学编码,

我面对这个问题,我的程序。该方案是与另一台机器,这是1Hz时输出串行数据接口。我的程序是MFC的基于一个cpropertypage和处理数据,我已经定义1线程(pRecvDataThread)来处理串行端口的缓冲区中的数据的读取,并显示在我的名单控制在主线程它。

我已经分开的GUI的东西,如清单控制等处理的线程在一些教程的建议。但是我的GUI仍然是难以响应。继承人的代码段,没有人知道什么错吗?


// on serial data arrival

LRESULT MyApp::OnDataArrived(WPARAM wParam,LPARAM lParam) 

{

	Sleep(40);

	const CSerialMFC::EEvent eEvent = CSerialMFC::EEvent(LOWORD(wParam));

	const CSerialMFC::EEvent eError = CSerialMFC::EEvent(HIWORD(wParam));

 

	switch(eEvent)

	{

	case CSerialMFC::EEventRecv:

 

		pRecvDataThread = AfxBeginThread(MyRecvThread, LPVOID(this), THREAD_PRIORITY_NORMAL);

		break;

	}

 

	return 0;

}

 

// my thread proc

UINT MyRecvThread(LPVOID pParam)

{

 

	MyApp *pTab = (MyApp*)pParam;

	WaitForSingleObject(pTab ->MutexRecv, INFINITE);

	DWORD dwBytesRead = 0;

	byte abBuffer[512];

 

	int errorInt = pTab->serialMFC.Read(abBuffer, sizeof(abBuffer), &dwBytesRead, 0, INFINITE);

 

	if(errorInt != ERROR_SUCCESS)

	{

		AfxMessageBox(_T("ERROR in reading serial port"));

		return 0;

	}

 

	char szTimestamp[128];

	SYSTEMTIME st;

	//GetSystemTime(&st);

	GetLocalTime(&st);

	sprintf_s(szTimestamp, 128, "%02d:%02d:%02d:%03d", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

	

 

	pTab->ReadData(abBuffer, szTimestamp, numBytesRead);

 

	ReleaseMutex(MyApp->MutexRecv);

	return 0;

}

void MyApp::ReadData(byte bytesRead [], char* timestamp, int bytesread)

{

    // display the bytes in list control

}

回答

评论会员:游客 时间:2012/02/06
Zsefv2:事情是这样的codeprelang="c++"spanclass="code-keyword"unsigned/spanspanclass="code-keyword"long/spanReceiveThread(CVnTabs*pvnsTab){spanclass="code-keyword"while/span(spanclass="code-keyword"true/span){ spanclass="code-keyword"if/span(!initialisePort) { spanclass="code-comment"///spanspanclass="code-comment"Setuptheserialport(9600,N81)usinghardwarehandshaking/spaninitialisePort=spanclass="code-keyword"true/span;}spanclass="code-keyword"do/span { spanclass="code-comment"///spanspanclass="code-comment"dopollingfordata/spanserial.Read(abBuffer,spanclass="code-keyword"sizeof/span(abBuffer),&dwBytesRead); spanclass="code-comment"///spanspanclass="code-comment"ifbytesmorethan0/span spanclass="code-keyword"if/span(dwBytesRead>spanclass="code-digit"0/span) { spanclass="code-comment"///spanspanclass="code-comment"processdata/span }  }spanclass="code-keyword"while/span(dwBytesRead==spanclass="code-keyword"sizeof/span(abBuffer)); spanclass="code-comment"///spanspanclass="code-comment"::PostMessageya-daya-da/span/pre/code
CPallini:你应该在一个线程中处理所有的通信细节(比如"沟通"之一),而现在,你在主线程(GUI),处理事件
而且(可能是整个点这里)你不应该锁定互斥,然后执行阻塞读。相反,你应该锁定互斥只更新一次阻塞(或长期超时)读已执行的共享数据
评论会员:游客 时间:2012/02/06
barneyman:你旋转和删除每接收线程,这是非常昂贵的,你的。消息泵是积极参与接待开始接收线程,它睡在一个互斥体,信号获取您的OnDataArrived消息处理程序甚至更好,已接收线程使用WaitCommEvent,这将消息泵,它完全脱离
伟业奥尔金:通过这样做您的独立线程内:
pTab->ReadData(abBuffer, szTimestamp, numBytesRead);
这就是你说你在那里做:{B​​R}