⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comread.cpp

📁 DVD
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

//---------------------------------------------------------------------------

TComRead::TComRead(void)
{
    // Make THRead suspended
    LogProcess = new TComLog(true);
//    TComLog->SetHandle(hComm);
}
//---------------------------------------------------------------------------

TComRead::~TComRead(void)
{
    // Shold be deleted automatically with Read
    if(LogProcess != NULL)
    {
        LogProcess->ReadLength = 0;
        LogProcess->Suspended = false;
        
        // Terminate the Thread
        LogProcess->Terminate();
        Sleep(250);
        
        //delete LogProcess;
        //LogProcess = NULL;
    }
}

//---------------------------------------------------------------------------
// COM READ FUNCTIONS
//---------------------------------------------------------------------------

void TComRead::SetMode(uchar bReadMode)
{
    switch(bReadMode)
    {
        case MODE_END :

            // For some reason LogProcess will hang whole program
            // If it is suspended

            // Start log thread
            LogProcess->Suspended = false; 
            LogProcess->ReadLength = 0;
            bMode = bReadMode;
            break;

        case MODE_LOG :
            // Start log thread
            LogProcess->Suspended = false; 
            LogProcess->ReadLength = 256;
            bMode = bReadMode;
            break;

        case MODE_BACKUP :
        case MODE_FLASH :
        case MODE_MEMORY :
            // Stop log thread
            LogProcess->Suspended = true; 
            LogProcess->ReadLength = 0;
            bMode = bReadMode;
            break;

        default : 
            // Stop log thread
            LogProcess->Suspended = true; 
            LogProcess->ReadLength = 0;
            bMode = MODE_NONE;
            break;
    }
}
//---------------------------------------------------------------------------

bool TComRead::Byte(uchar &bData)
{
	bool Ret;
	uchar cIncoming[2];
	int iRetry;
	ulong ulBytesRead;

	try
	{
		iRetry = COM_RETRY;
		*cIncoming = 0x00;

		while(1)
		{
			// Try read bytes
			Ret = ReadFile(ComPort->hComm, cIncoming, 1, &ulBytesRead, NULL);

			// No data readed
			if(ulBytesRead < 1)
				Ret = false;

			// Break if no more retry or Ret
			if((Ret == true) || (iRetry-- <= 0))
				break;

			// Wait some time before new attempt
			Sleep(COM_DELAY * 100);
		}

		// Get readed data
		if(Ret == true)
		{
			bData = *cIncoming;
		  #ifdef _DEBUG
            sprintf(ComPort->Msg, "Read : %02X\n", cIncoming[0]);
			Print->Debug(ComPort->Msg);
		  #endif
		}
		else
		{
			bData = 0;
		  #ifdef _DEBUG
			Print->Debug("Read : Error!");
		  #endif
			throw COM_ID_READ_ERROR;
		}
	}

	catch (int E)
	{
		//ComPort->Error = E;
		ComPort->PortMessage(E);
		//PrintError(sText.c_str());
		return false;
	}

	catch (...)
	{
		//ComPort->Error = COM_ID_ERROR;
		ComPort->PortMessage(COM_ID_ERROR);
		//PrintError("ERROR : Unknow COM Read Byte error !");
		return false;
	}

	return true;
}
//---------------------------------------------------------------------------
// Read usLength bytes from COM to cData buffer
//
// Options :
// - usLength : Input bytes to Read / Output bytes Readed
//              (Max length is  65535 bytes (sixe of short))
// - *cData   : Pointer to output data buffer
//              (!! Be sure that output buffer is long enough when calling
//              this routine !!)
//---------------------------------------------------------------------------

bool TComRead::Read(uword &usLength, uchar *cData)
{
	ulong ulBytesRead;
	ulong ulReaded;
	ulong ulLength;
	ulong ulError;
	int iRetry;
    uchar bDelay;
	bool Ret;
	uchar cIncoming[2];
  #ifdef _DEBUG
    int i;
  #endif

	try
	{
		ulLength = (ulong) usLength;
		ulReaded = 0;

        if(usLength == 0)
        {
            throw COM_ID_ZERO_LENGTH;
        }

	  #ifdef _DEBUG
		strcpy(ComPort->Msg, "Read : ");
	  #endif

	  #if 0
		// Try read bytes
		while(ulReaded < ulLength)
		{
			iRetry = COM_RETRY;
			*cIncoming = 0x00;

			while (1)
			{
				// Try read bytes
				Ret = ReadFile(ComPort->hComm, cIncoming, 1, &ulBytesRead, NULL);

				// No data readed
				if(ulBytesRead < 1)
                {
					Ret = false;
                }
              #ifdef _DEBUG
                else
                {
                    sprintf(ComPort->Tmp, "%02X ", *cIncoming);
                    strcat(ComPort->Msg, ComPort->Tmp);
                    if(strlen(ComPort->Msg) > 80)
                    {
                        Print->Debug(ComPort->Msg);
                        strcpy(ComPort->Msg, "");
                    }
                }
              #endif

                // Break if no more retry or Ret
                if((Ret == true) || (iRetry-- <= 0))
                    break;

                // Wait some time before new attempt
                Sleep(bDelay * 100);
            }

            if(Ret == false)
                break;

            *(cData + ulReaded) = *cIncoming;
            ulReaded++;
        }
      #else
        iRetry = (int) ComPort->bRetry; //COM_RETRY
        bDelay = ComPort->bDelay;

        while(1)
        {
            // Try read bytes
            ReadFile(ComPort->hComm, cData + ulReaded, ulLength - ulReaded, &ulBytesRead, NULL);

            // No bytes readed
            if(ulBytesRead < 1)
            {
                ulBytesRead = 0;
                Ret = false;

                // Break if no more retry
                if (iRetry-- <= 0)
                {
                    break;
                }

              #ifdef _DEBUG
                if(strlen(ComPort->Msg) > 0)
                {
                    Print->Debug(ComPort->Msg);
                }
                sprintf(ComPort->Msg, "Retry : %i", iRetry);
                Print->Debug(ComPort->Msg);
                strcpy(ComPort->Msg, "Read : ");
              #endif

                //PurgeComm(ComPort->hComm, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
                // Wait some time
                Sleep(bDelay * 10);
            }
            else
            {
              #ifdef _DEBUG
                for(i=0; i<(int)ulBytesRead; i++)
                {
                    sprintf(ComPort->Tmp, "%02X ", *(cData + ulReaded + i));
                    strcat(ComPort->Msg, ComPort->Tmp);
                    if(strlen(ComPort->Msg) > 80)
                    {
                        Print->Debug(ComPort->Msg);
                        strcpy(ComPort->Msg, "Read : ");
                    }
                }
              #endif

                ulReaded += ulBytesRead;

                // Break if all bytes ar readed
                if(ulReaded >= ulLength)
                {
                    ulReaded = ulLength;
                    Ret = true;
                    break;
                }
            }
		}
	  #endif

	  #ifdef _DEBUG
		if(Ret == false)
		{
			strcat(ComPort->Msg, "Error!");
		}
		Print->Debug(ComPort->Msg);
	  #endif

        usLength = (unsigned short) ulReaded;

        if(Ret == false)
        {
            throw COM_ID_READ_ERROR;
        }
    }

	catch (int E)
	{
		//ComPort->Error = E;
		ComPort->PortMessage(E);
		//PrintError(sText.c_str());
		return false;
	}

	catch (...)
	{
		//ComPort->Error = COM_ID_ERROR;
		ComPort->PortMessage(COM_ID_ERROR);
		//PrintError("ERROR : Unknow COM Read error !");
		return false;
	}

	return true;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -