| | |
| | | GetCommTimeouts(hCom1, &comtimeout1);
|
| | |
|
| | | comtimeout1.ReadIntervalTimeout = 1; //两个字符之间的时间
|
| | | comtimeout1.ReadTotalTimeoutMultiplier = 1; //每个字符等待的时间,读多个字符则等待时间为单个时间乘以字符个数
|
| | | comtimeout1.ReadTotalTimeoutMultiplier = 0; //每个字符等待的时间,读多个字符则等待时间为单个时间乘以字符个数
|
| | | comtimeout1.ReadTotalTimeoutConstant = 2; //读取时,再另外多等待的时间。
|
| | | comtimeout1.WriteTotalTimeoutMultiplier = 0; //写入时,每个字符等待的时间,写入多个字符则等待时间为单个时间乘以字符个数。
|
| | | comtimeout1.WriteTotalTimeoutConstant = 0; //写入时,再另外多等待的时间。
|
| | |
| | | HANDLE hEventArr[2];
|
| | | memset(&osRead, 0, sizeof(OVERLAPPED));
|
| | | osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
| | | SetCommMask(hCom1, EV_RXCHAR| EV_TXEMPTY);
|
| | | SetCommMask(hCom1, EV_RXCHAR | EV_TXEMPTY);
|
| | |
|
| | | hEventArr[0] = osRead.hEvent;
|
| | | // hEventArr[1] = *g_OutPutList.GetEvent();
|
| | |
| | | int LastSpeedTime = (int)GetTimemS();
|
| | | int nThisRecv = 0;
|
| | |
|
| | | // COMSTAT cs;
|
| | | // OVERLAPPED os;
|
| | | // DWORD dwTrans;
|
| | | // DWORD dwReaded;
|
| | | COMSTAT cs;
|
| | | OVERLAPPED os;
|
| | | DWORD dwTrans;
|
| | | DWORD dwReaded;
|
| | | for (; MyThreadProc1ToRun == 1;)
|
| | | {
|
| | |
|
| | | DWORD dwEvtMask = 0;
|
| | | /*
|
| | | BOOL result = WaitCommEvent(hCom1, &dwEvtMask, &os);
|
| | | if (!result)
|
| | | ///*
|
| | | // BOOL result = WaitCommEvent(hCom1, &dwEvtMask, NULL); // &os);
|
| | | // if (!result)
|
| | | {
|
| | | if (GetLastError() == ERROR_IO_PENDING)
|
| | | // if (GetLastError() == ERROR_IO_PENDING)
|
| | | // {
|
| | | // GetOverlappedResult(hCom1, &os, &dwTrans, true);
|
| | |
|
| | | // }
|
| | | // if (dwEvtMask & EV_RXCHAR)
|
| | | if (1) { Sleep(1); } else
|
| | | {
|
| | | GetOverlappedResult(hCom1, &os, &dwTrans, true);
|
| | | if (dwEvtMask & EV_RXCHAR)
|
| | | ClearCommError(hCom1, &m_dwError, &cs);
|
| | | int bytes = cs.cbInQue;
|
| | | dwReaded = 0;
|
| | | if (bytes > 0)
|
| | | {
|
| | | ClearCommError(hCom1, &m_dwError, &cs);
|
| | | int bytes = cs.cbInQue;
|
| | | dwReaded = 0;
|
| | | ReadFile(hCom1,RecvBuf,bytes,&dwReaded,NULL);
|
| | | int j = ReadFile(hCom1, RecvBuf2, bytes, &dwReaded, NULL);
|
| | | if (j > 0)
|
| | | {
|
| | | m_CS1.Lock();
|
| | | memcpy(RecvBuf + RecvBufDataLen, RecvBuf2, dwReaded);
|
| | | RecvBufDataLen += dwReaded;
|
| | | m_CS1.Unlock();
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | Sleep(1);
|
| | | }
|
| | | }
|
| | | }
|
| | | if (RecvBufDataLen > 1024) { RecvBufDataLen = 0; }
|
| | | //EscapeCommFunction(hCom1, SETDTR);
|
| | | //*/
|
| | | Sleep(1);
|
| | | }
|
| | | //SysLog(_T("跳出接收循环\r\n"));
|
| | | // hcurDC.SelectObject(def_font);
|
| | |
| | | return 0;
|
| | | }
|
| | |
|
| | | int HvSerialPort::ClearBuf()
|
| | | {
|
| | | PurgeComm(hCom1, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_TXCLEAR);
|
| | | RecvBufDataLen = 0;
|
| | | RecvBufPos = 0;
|
| | | return 0;
|
| | | }
|
| | |
|
| | | int HvSerialPort::Send(void * pBuf, int len1)
|
| | | {
|
| | |
| | |
|
| | | return R_OK;
|
| | | }
|
| | |
|
| | |
|
| | | int HvSerialPort::Recv(void * pBuf, int len1)
|
| | | {
|
| | | return RecvFromCom(pBuf, len1);
|
| | | }
|
| | |
|
| | | int HvSerialPort::RecvFromBuf(void * pBuf, int len1)
|
| | | {
|
| | | CString s1;
|
| | | int i, j;
|
| | | DWORD numreaded = 0;
|
| | | int numtoread = len1;
|
| | | m_strResult.Format(_T("ToRead %d "), len1);
|
| | | for (i = 0; i < 20; i++)
|
| | | {
|
| | | int k = m_CS1.Lock();
|
| | | if (RecvBufDataLen)
|
| | | {
|
| | | if (RecvBufDataLen >= numtoread)
|
| | | {
|
| | | memcpy((char *)pBuf+numreaded, RecvBuf, numtoread);
|
| | | numreaded += numtoread;
|
| | | RecvBufDataLen -= numtoread;
|
| | | numtoread = 0;
|
| | | memmove(RecvBuf, RecvBuf + numtoread, RecvBufDataLen);
|
| | | }
|
| | | else
|
| | | {
|
| | | memcpy((char *)pBuf + numreaded, RecvBuf, RecvBufDataLen);
|
| | | numreaded += RecvBufDataLen;
|
| | | RecvBufDataLen = 0;
|
| | | RecvBufPos = 0;
|
| | | numtoread -= RecvBufDataLen;
|
| | | }
|
| | | }
|
| | | m_CS1.Unlock();
|
| | | if (numtoread == 0) break;
|
| | | Sleep(1);
|
| | | }
|
| | | if (numreaded > 0) {
|
| | | m_strResult.AppendFormat(_T("Read From COM%d OK %dB "), this->m_nPort, numreaded);
|
| | | for (i = 0; (unsigned)i < numreaded; i++)
|
| | | {
|
| | | m_strResult.AppendFormat(_T("%c"), ((char *)pBuf)[i]);
|
| | | }
|
| | | m_strResult.AppendFormat(_T("\r\n"));
|
| | | }
|
| | | else {
|
| | | m_strResult.Format(_T("Read From COM%d failed \r\n"), this->m_nPort);
|
| | | }
|
| | | m_strResult.AppendFormat(_T("ToRun %d, Running %d"), MyThreadProc1ToRun, MyThreadProc1Running);
|
| | | |
| | | return numreaded;
|
| | | }
|
| | | int HvSerialPort::RecvFromCom(void * pBuf, int len1)
|
| | | {
|
| | | // TODO: 在此处添加实现代码.
|
| | | CString s1;
|
| | |
| | | else {
|
| | | if (len2 > 0) {
|
| | | nTryCount2++;
|
| | | if (nTryCount2 >= m_nCountToWait) break;
|
| | | if (nTryCount2 > m_nCountToWait) break;
|
| | | }
|
| | | }
|
| | | if (len2 > 0 && nTryCount2 >= m_nCountToWait) break;
|
| | | if (len2 > 0 && nTryCount2 > m_nCountToWait) break;
|
| | | }
|
| | | numreaded = len2;
|
| | | if (numreaded > 0) {
|