📄 comportcmd.cpp
字号:
//---------------------------------------------------------------------------
// Pixelworks Inc. Company Confidential Strictly Private
//
// $Archive: /SwTools/FlashUpgrader/ComPortCmd.cpp $
// $Revision: 1.1.1.1 $
// $Author: KevinM $
// $Date: 2003/09/29 18:19:04 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 1997 (c) Pixelworks Inc.
//
// Pixelworks owns the sole copyright to this software. Under international
// copyright laws you (1) may not make a copy of this software except for
// the purposes of maintaining a single archive copy, (2) may not derive
// works herefrom, (3) may not distribute this work to others. These rights
// are provided for information clarification, other restrictions of rights
// may apply as well.
//
// This is an unpublished work.
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>> WARRANTEE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Pixelworks Inc. MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE USE OF
// THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE.
// --------------------------------------------------------------------------
#include "stdafx.h"
#include "ComPortCmd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef enum
{
weDATA = 0,
weCANCEL
} eWAITEVENT;
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
CComPortCmd::CComPortCmd()
{
m_pParent = NULL;
m_hDataEvent = INVALID_HANDLE_VALUE;
#ifdef BUILD_USB_CONFIG
m_pUsb = NULL;
#endif
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
CComPortCmd::~CComPortCmd()
{
#ifdef BUILD_USB_CONFIG
if (m_pUsb) delete m_pUsb;
#endif
}
BEGIN_MESSAGE_MAP(CComPortCmd, CComPortBase)
//{{AFX_MSG_MAP(CComPortCmd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CComPortCmd::Create(CWnd *pParent)
{
m_pParent = pParent;
ASSERT(m_pParent);
m_hDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (INVALID_HANDLE_VALUE == m_hDataEvent)
{
TRACE("CreateEvent failed\n");
return FALSE;
}
return CComPortBase::Create((CWnd*)pParent); // invoke base class
}
//----------------------------------------------------------------------------
// Pure virtual method declared in base class being implemented here.
//----------------------------------------------------------------------------
void CComPortCmd::ReceivedData(CByteArray& ref_byteArray)
{
{
CMakeSafe ms(&m_Safe); // start of thread-safe block
m_ReceivedDataArray.Append(ref_byteArray);
}
SetEvent(m_hDataEvent);
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CComPortCmd::FlushBuffer()
{
BOOL bStatus = CCommunication::FlushBuffer(); // invoke base class
CMakeSafe ms(&m_Safe); // start of thread-safe block
m_ReceivedDataArray.RemoveAll();
ResetEvent(m_hDataEvent);
return bStatus;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CComPortCmd::SendData(LPCSTR lpszData)
{
BOOL bResult = FALSE;
bResult = SetOutput((BYTE*)lpszData, strlen(lpszData));
return bResult;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CComPortCmd::SendData(BYTE cData)
{
BOOL bResult = FALSE;
bResult = SetOutput(&cData, 1);
return bResult;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CComPortCmd::SendData(BYTE* pData, int nSize)
{
BOOL bResult = FALSE;
bResult = SetOutput(pData, nSize);
return bResult;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
eSENDCMDRETVAL CComPortCmd::SendCommand(LPCSTR lpszCmd, CString& rstrRet,
HANDLE hCancelEvent)
{
rstrRet = "Error";
CString strCmd(lpszCmd);
strCmd += "\r";
ResetEvent(m_hDataEvent);
m_ReceivedDataArray.RemoveAll();
if (FALSE == SetOutput((BYTE*)(LPCSTR)strCmd, strCmd.GetLength()))
{
return scERROR;
}
return GetData(rstrRet, hCancelEvent, TRUE);
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
static BYTE UsbData[1024];
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
eSENDCMDRETVAL CComPortCmd::GetData(CString& rstrRet,
HANDLE hCancelEvent,
int nTimeoutSec /* = DEFAULT_TIMEOUT */,
int nDelayMs /* = 0 */,
BOOL bStopOnEOL /* = FALSE */)
{
rstrRet.Empty();
CByteArray byteArray;
eSENDCMDRETVAL eRet = GetData(byteArray, hCancelEvent, 0, nTimeoutSec, nDelayMs, bStopOnEOL);
if (scOK == eRet)
{
DWORD dwSize = byteArray.GetSize();
if (dwSize)
{
LPSTR pString = rstrRet.GetBuffer(dwSize);
BYTE *pData = byteArray.GetData();
CopyMemory(pString, pData, dwSize);
//----------------------------------------------------------------
// Since the string is not NULL terminated, we need to tell the
// CString object the size of the string when we release the
// buffer.
//----------------------------------------------------------------
rstrRet.ReleaseBuffer(dwSize);
}
TrimString(rstrRet);
}
return eRet;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
eSENDCMDRETVAL CComPortCmd::GetData(CByteArray& rbyteArray,
HANDLE hCancelEvent,
int nSize /* = 0 */,
int nTimeoutSec /* = DEFAULT_TIMEOUT */,
int nDelayMs /* = 0 */,
BOOL bStopOnEOL /* = FALSE */)
{
BOOL bWait = TRUE;
static HANDLE hArray[2];
hArray[0] = m_hDataEvent;
hArray[1] = hCancelEvent;
rbyteArray.RemoveAll();
#ifdef BUILD_USB_CONFIG
if (m_eComm == ccUSB)
{
int nRead = 0;
if (nSize == 0 && bStopOnEOL) nSize = 1024;
BYTE cMarker = (bStopOnEOL?0x0d:0x0);
while (nRead == 0)
{
if (WaitForSingleObject(hCancelEvent, 0) != WAIT_TIMEOUT)
{
return scCANCEL;
//int i = 5;
}
nRead = m_pUsb->Read(&UsbData[0], nSize, cMarker);
}
m_ReceivedDataArray.RemoveAll();
m_ReceivedDataArray.SetSize(nSize);
PBYTE pData = m_ReceivedDataArray.GetData();
CopyMemory(pData, &UsbData[0], nRead);
}
else
#endif
{
CTime startTime = CTime::GetCurrentTime();
while (bWait)
{
{
CMakeSafe ms(&m_Safe); // start of thread-safe block
if (bStopOnEOL)
{
if (-1 != FindEOLChar())
{
break;
}
}
else if (0 == nSize || (nSize <= m_ReceivedDataArray.GetSize()))
{
break;
}
}
long lRemainingTime = INFINITE;
if (INFINITE != nTimeoutSec)
{
CTime endTime = CTime::GetCurrentTime();
CTimeSpan elapsedTime = endTime - startTime;
long lSeconds = elapsedTime.GetTotalSeconds();
lRemainingTime = nTimeoutSec - lSeconds;
if (lRemainingTime < 0)
{
TRACE("Timeout\n");
return scERROR; // timeout has expired
}
lRemainingTime *= 1000; // convert to mS
}
{
DWORD dwState = MsgWaitForMultipleObjects(2,
hArray,
FALSE,
lRemainingTime,
QS_ALLINPUT);
switch(dwState)
{
// Received text from com port.
case WAIT_OBJECT_0 + weDATA:
{
CMakeSafe ms(&m_Safe); // start of thread-safe block
if (bStopOnEOL)
{
if (-1 != FindEOLChar())
{
bWait = FALSE;
}
}
else if (0 == nSize || (nSize <= m_ReceivedDataArray.GetSize()))
{
bWait = FALSE;
}
if (nDelayMs)
{
::Sleep(nDelayMs);
}
}
break;
case WAIT_OBJECT_0 + weCANCEL:
TRACE("Canceled\n");
return scCANCEL;
break;
// A message is in queue.
case WAIT_OBJECT_0 + 2:
TRACE("Message in queue\n");
break;
case WAIT_TIMEOUT:
TRACE("Timeout\n");
return scERROR;
break;
default:
TRACE("Unknown value from MsgWaitForMultipleObjects\n");
return scERROR;
break;
}
ProcessMessages();
}
}
}
{
CMakeSafe ms(&m_Safe); // start of thread-safe block
if (bStopOnEOL)
{
int nIndex = FindEOLChar();
if (-1 != nIndex)
{
nSize = nIndex + 1; // Index is zero based
}
}
if (0 == nSize)
{
rbyteArray.Copy(m_ReceivedDataArray);
m_ReceivedDataArray.RemoveAll();
}
else
{
DWORD dwSize = m_ReceivedDataArray.GetSize();
BYTE *pOrgData = m_ReceivedDataArray.GetData();
rbyteArray.SetSize(nSize);
BYTE *pDest = rbyteArray.GetData();
CopyMemory(pDest, pOrgData, nSize);
CopyMemory(pOrgData, pOrgData + nSize, dwSize - nSize);
m_ReceivedDataArray.SetSize(dwSize - nSize);
}
}
return scOK;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
int CComPortCmd::FindEOLChar()
{
CMakeSafe ms(&m_Safe); // start of thread-safe block
DWORD dwSize = m_ReceivedDataArray.GetSize();
BYTE *pData = m_ReceivedDataArray.GetData();
BYTE cChar;
for (int i = 0; i < (int)dwSize; i++)
{
cChar = *pData++;
if (0x0d == cChar || 0 == cChar)
{
return i;
}
}
return -1;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
void CComPortCmd::ProcessMessages()
{
MSG msg;
// Process existing messages in the application's message queue.
while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
{
if (!AfxGetThread()->PumpMessage())
break;
}
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
void CComPortCmd::TrimString(CString& ref_strText)
{
int nCharIndex = 0;
if (ref_strText.GetLength() > 32768)
{
ref_strText.Left(32768);
}
// Eliminate line feeds.
while ((nCharIndex = ref_strText.Find(TCHAR('\n'))) != -1)
{
ref_strText = ref_strText.Left(nCharIndex) + ref_strText.Mid(nCharIndex + 1);
}
// Eliminate extra carriage returns.
for (int i = 0; i < ref_strText.GetLength(); i++)
{
if ('\r' == ref_strText.GetAt(i))
{
int nNext = i;
while ((nNext < ref_strText.GetLength() - 1) &&
'\r' == ref_strText.GetAt(nNext + 1))
{
nNext++;
}
if (i != nNext)
{
ref_strText = ref_strText.Left(i + 1) + ref_strText.Mid(nNext + 1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -