当前位置: 代码迷 >> WinCE >> 串口ReadPortThread读到数据后,怎样正确显示出来?解决方法
  详细解决方案

串口ReadPortThread读到数据后,怎样正确显示出来?解决方法

热度:67   发布时间:2016-04-28 13:31:30.0
串口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);
                                                        }
  相关解决方案