问题描述
我用 C++ wxWidgets Framework 创建了一个程序,用于从 Windows 7 中的串口读取数据。
当程序运行并点击读取“Baca”按钮时,数据将显示在文本框中。但此后,无法通过单击停止“Berhenti”按钮来停止程序。甚至程序挂起。如何让它在点击 then 按钮时停止而不挂起程序?
//This is code for read serial button
bool m_Aborted;
void SerialPortFrame::OnbtnBacaSerialClick(wxCommandEvent& event)
{
m_Aborted = false;
HANDLE hComm;
hComm=CreateFileA("\\\\.\\COM4",GENERIC_READ | GENERIC_WRITE,NULL,OPEN_EXISTING,NULL );
BOOL Status;
DCB dcbSerialParams={0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
Status=GetCommState(hComm,&dcbSerialParams);
dcbSerialParams.Baudrate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=OnesTOPBIT;
dcbSerialParams.Parity=nopARITY;
Status=SetCommState(hComm,&dcbSerialParams);
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WritetotalTimeoutConstant=50;
timeouts.WritetotalTimeoutMultiplier=10;
Status=SetCommMask(hComm,EV_RXCHAR);
DWORD dwEventMask;
Status=WaitCommEvent(hComm,&dwEventMask,NULL);
char TempChar;
char SerialBuffer[256];
DWORD NoBytesRead;
int i=0;
do{
Status=ReadFile(hComm,&TempChar,sizeof(TempChar),&NoBytesRead,NULL);
SerialBuffer[i]=TempChar;
wxString mystring1;
mystring1<<SerialBuffer[i];
txtSerial->AppendText(mystring1.c_str());
i++;
wxMilliSleep(300);
wxThread::This()->Sleep(1);
} while(!m_Aborted);
CloseHandle(hComm);
}
//This is code for stop
void SerialPortFrame::OnbtnBerhentiClick(wxCommandEvent& event)
{
m_Aborted = true;
Close(true);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)