|
串口ReadPortThread读到数据后,怎样正确显示出来?
[复制链接]
DWORD WINAPI ReadPortThread(LPVOID lpvoid)
{
BYTE Byte;
DWORD dwCommModemStatus,
dwBytesTransferred;
CString tempStr;
// Specify a set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR);
WaitCommEvent (hPort, &dwCommModemStatus, 0);
if (dwCommModemStatus & EV_RXCHAR)
{
DWORD length=0;
COMSTAT ComStat;
DWORD dwErrorFlags;
char *buf=NULL;
ClearCommError(hPort,&dwErrorFlags,&ComStat);
length=ComStat.cbInQue;
ReadFile(hPort,buf,length,&length,NULL);
CString temp=buf;
MessageBox(NULL,temp,TEXT("Result"),MB_OK);
}
// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
}
则消息框里为空。
在下查到些方案,如
if(dwLength>0)
{
//从串口读取数据
TCHAR* buf=new TCHAR[256];
fReadState=ReadFile(hPort,buf,dwLength,&dwLength,NULL);
if(!fReadState)
{
//不能从串口读取数据
MessageBox(NULL,TEXT("Error in read from serial port"),TEXT("Read Error"),MB_OK);
}
else
{
//把数据赋值给全局变量
strInChar=buf;
}
delete[] buf;
}
问题抖出在类型转化、缓冲区之间数据拷贝的问题,不知道怎样解决。 各位帮帮忙看看。
|
|