当前位置: 代码迷 >> WinCE >> 请问一下WinCE串口通讯的有关问题,大侠们一看估计就知道原因,但困扰小弟我了
  详细解决方案

请问一下WinCE串口通讯的有关问题,大侠们一看估计就知道原因,但困扰小弟我了

热度:86   发布时间:2016-04-28 13:21:15.0
请教一下WinCE串口通讯的问题,大侠们一看估计就知道原因,但困扰我了
闲话少说,贴出代码来:


C/C++ code
//串口打开函数BOOL OpenPort(HWND hWnd){    DWORD dwError;    //打开串口    hSerPort=CreateFile(TEXT("COM1:"),                        GENERIC_READ | GENERIC_WRITE,                        0,                        NULL,                        OPEN_EXISTING,                        0,                        NULL);    if(hSerPort == INVALID_HANDLE_VALUE)    {        dwError = GetLastError();        MessageBox(hWnd, _T("Open Port (COM1:) Error!"), _T("ERROR"), 0);        return FALSE;    }        //设置串口输入输出缓冲区大小    //SetupComm(hSerPort, _InQue, _OutQue);    //设置串口事件掩码    //SetCommMask(hSerPort, 0);    //设置串口属性    if(ConfigComm())    {        fContinue = TRUE;        return TRUE;    }    else    {        MessageBox(hWnd, TEXT("Config COM1: error!"), TEXT("ERROR"), 0);        CloseHandle(hSerPort);        return FALSE;    }}//串口设置函数BOOL ConfigComm(){    DCB dcb;    COMMTIMEOUTS tmOuts;    //获取串口原始属性    GetCommState(hSerPort, &dcb);    dcb.BaudRate = 19200;        // 设置波特率    dcb.ByteSize = 8;            // 数据位,范围:4-8     dcb.Parity   = NOPARITY;    // 校验模式:无校验    dcb.StopBits = ONESTOPBIT;    // 停止位: 1位    dcb.fParity  = FALSE;        // 不支持奇偶校验    //dcb.fBinary  = TRUE;        // 设置二进制模式,此处必须设置TRUE    dcb.fNull    = FALSE;        // Disable null stripping     /*以下这些设置了之后有用么???    dcb.fOutxCtsFlow  = FALSE;    // No CTS output flow control     dcb.fOutxDsrFlow  = FALSE;    // No DSR output flow control     dcb.fDtrControl   = DTR_CONTROL_DISABLE;    //启用流量控制    // DTR flow control type    dcb.fDsrSensitivity   = FALSE;    // DSR sensitivity     dcb.fTXContinueOnXoff = TRUE;    // XOFF continues Tx     dcb.fOutX             = FALSE;    // No XON/XOFF out flow control     dcb.fInX              = FALSE;    // No XON/XOFF in flow control     dcb.fErrorChar        = FALSE;    // Disable error replacement     dcb.fRtsControl       = RTS_CONTROL_ENABLE;    // RTS flow control    dcb.fAbortOnError     = FALSE;    // 当串口发生错误,并不终止串口读写    */    if(!SetCommState(hSerPort, &dcb))    {        return FALSE;    }    //设置串口超时时间,[color=#FF0000]这样设置有问题吗?超时的设置是不是跟波特率有关才行?[/color]    GetCommTimeouts(hSerPort, &tmOuts);    tmOuts.ReadIntervalTimeout = 0;    tmOuts.ReadTotalTimeoutConstant = 0;    tmOuts.ReadTotalTimeoutMultiplier = 0;    tmOuts.WriteTotalTimeoutConstant = 0;    tmOuts.WriteTotalTimeoutMultiplier = 0;    if(!SetCommTimeouts(hSerPort, &tmOuts))    {        return FALSE;    }    //指定端口监测的事件集    //SetCommMask (hSerPort, EV_RXCHAR);        //分配设备缓冲区    //SetupComm(hSerPort, 10, 10);    //初始化缓冲区中的信息    PurgeComm(hSerPort, PURGE_TXCLEAR | PURGE_RXCLEAR);    return TRUE;}DWORD SendThread (PVOID pArg){    TCHAR szText[TEXTSIZE];    TCHAR szDlg[128];    DWORD dwLen;    BOOL stat, start;    HWND hWnd = (HWND)pArg;    start = TRUE;    while(1)    {        Sleep(5000);        if(fContinue)        {            if(!fContinue)//这句话是浪费的,大家不用管                break;            if(start)            {                lstrcat(szText, TEXT("ATE0\r"));                start = FALSE;                wsprintf(szDlg, TEXT("Sended ATE0. \r\n"));                UpdateHistory((LPARAM)szDlg);            }            else            {                lstrcat(szText, TEXT("AT\r"));                //显示发送的数据                wsprintf(szDlg, TEXT("Sended AT.\r\n"));                UpdateHistory((LPARAM)szDlg);            }            PurgeComm(hSerPort, PURGE_TXCLEAR | PURGE_RXCLEAR);    //发送之前先清空输入输出缓冲区            stat = WriteFile (hSerPort, szText, lstrlen (szText)*sizeof (TCHAR), &dwLen, 0);            if(!stat)            {                wsprintf(szDlg, TEXT("Send ERROR!!\r\n"));                UpdateHistory((LPARAM)szDlg);                return 0;            }        }        else            break;    }    return 0;}DWORD ReadThread (PVOID pArg){    //BYTE szText[TEXTSIZE];    //COMSTAT comstat;    //SetCommMask (hSerPort, EV_RXCHAR);     while (fContinue)    {        /*        if(ReadFile(hSerPort, szText, 10, &dwLength, NULL))        {            if(dwLength != 0)            {                TCHAR dlg[128];                wsprintf(dlg, TEXT("received %d bytes.\r\n"), dwLength);                UpdateHistory((LPARAM)dlg);                UpdateHistory((LPARAM)szText);            }        }*/        /*************************************************        DWORD dwError, dwLength;        COMSTAT comstat;        ClearCommError(hSerPort, &dwError, &comstat);        if(comstat.cbInQue > 0)        {            ReadFile(hSerPort, &szText, comstat.cbInQue, &dwLength, 0);                        //保存数据            TCHAR *tmp = new TCHAR[iCount + 1];            if(tmp == NULL)                return -1;            TCHAR dlg[128];            wsprintf(dlg, TEXT("received %d bytes.\r\n"), comstat.cbInQue);            UpdateHistory((LPARAM)dlg);            memcpy(tmp, szText, iCount);            lstrcat(tmp, TEXT("\r\n"));            UpdateHistory((LPARAM)tmp);            PurgeComm(hSerPort, PURGE_RXCLEAR);        }                Sleep(1);*/        //**************************************************/        //DWORD dwCommStatus;        //WaitCommEvent(hSerPort, &dwCommStatus, 0);        //SetCommMask (hSerPort, EV_RXCHAR);        //PurgeComm(hSerPort, PURGE_RXCLEAR);        //memset(szText, 0, sizeof(szText));        TCHAR szText[TEXTSIZE];        BYTE rByte;    //接收到的一个字节        DWORD dwLength;    //实际接收的字节数        INT iCount=0;        TCHAR tch;        do        {            ReadFile(hSerPort, &rByte, 1, &dwLength, 0);            if(dwLength == 1)            {                szText[iCount] = rByte;                iCount ++;                tch = (tch << 8) & 0xff00;                if (tch == TEXT ('\n'))                    break;                //if(iCount >= TEXTSIZE)                //{                //    return -1;//缓冲区满                //}            }        }while(dwLength == 1);        //ClearCommError(hSerPort,&dwReadErrors,&cmState);//清除错误标志并得到串口状态        //PurgeComm(hSerPort, PURGE_RXCLEAR);        if(iCount == 0)        {            //Sleep(100);            continue;    //没接收到数据,继续下次循环        }        //保存数据        TCHAR dlg[128];        wsprintf(dlg, TEXT("received %d bytes.\r\n"), iCount);        UpdateHistory((LPARAM)dlg);        TCHAR *tmp = new TCHAR[iCount + 1];        if(tmp == NULL)            return -1;        memcpy(tmp, szText, iCount);        lstrcat(tmp, TEXT("\r\n"));        UpdateHistory((LPARAM)tmp);        //iCount = 0;        //Sleep(100);    }    return 0;}
  相关解决方案