Files
TAO Cheng 5a8e27e70d Merge
2013-07-22 14:34:50 +08:00

436 lines
12 KiB
C++

//=============================================================================
#include "stdafx.h"
#include "SsiStatus.h"
#include "logger.h"
#include "math.h"
#include "Proto_Util.h"
#define MY_CONFIG 1
#define MAX_DEVPATH_LENGTH 256
#define ENDPOINT_TIMEOUT 500
#define MAX_BUFF_SIZE 1024
//===========================================================================
double CProto_Util::ScaleToMM(long lCount, double dResolution)
{
double dMM = 0.0;
dMM = lCount * dResolution;
return dMM;
}
//===========================================================================
long CProto_Util::MMtoScale(double lDistanceMM, double dResolution)
{
long lCounts = 0;
if (dResolution)
lCounts = (long) (lDistanceMM / dResolution);
else
ASSERT(0);
return lCounts;
}
//=====================================================================================
long CProto_Util::_4char2long(unsigned char *cBuff)
{
union
{
long l_value;
char c_array[5];
};
memset (c_array, 0, 5);
c_array[0] = cBuff[3];
c_array[1] = cBuff[2];
c_array[2] = cBuff[1];
c_array[3] = cBuff[0];
return(l_value);
}
//========================================================================
void CProto_Util::_reverse_dword(DWORD *dWord)
{
BYTE cBuff[4];
BYTE *dwBuff = (BYTE *)dWord;
for ( int ii = 0 ; ii < 4 ; ++ii )
cBuff[ii]= dwBuff[ii];
dwBuff[0] = cBuff[3];
dwBuff[1] = cBuff[2];
dwBuff[2] = cBuff[1];
dwBuff[3] = cBuff[0];
}
//******************************************************************************
void CProto_Util::_scale2inch(unsigned long scale, double &inch)
{
UNREFERENCED_PARAMETER(scale);
UNREFERENCED_PARAMETER(inch);
}
//******************************************************************************
void CProto_Util::_inch2scale(unsigned long &scale, double inch)
{
UNREFERENCED_PARAMETER(scale);
UNREFERENCED_PARAMETER(inch);
}
//******************************************************************************
// convert a string of characters into its binary form
void CProto_Util::_char2bin(unsigned char *cBuff, BYTE *cBytes, int iLen)
{
memset(cBytes, 0, MAX_BUFF_SIZE);
for (int i=0;i<iLen;i++)
{
sscanf_s((const char *)(cBuff+i*2), "%2x", (cBytes+i));
};
return;
}
//******************************************************************************
void CProto_Util::_swap_byte(unsigned short &Val)
{
unsigned short MSB = Val<<8;
BYTE LSB = Val>>8;
Val = MSB|LSB;
}
//******************************************************************************
double CProto_Util::TimeInSecs(void)
{
double Secs;
LARGE_INTEGER HPCounterTicksPerSecond;
BOOL HasHPCounter = QueryPerformanceFrequency(&HPCounterTicksPerSecond);
if (HasHPCounter == TRUE)
{
// Use high resolution clock.
double HPCounterTicksPersec = (DWORD)((double) HPCounterTicksPerSecond.QuadPart);
LARGE_INTEGER HPTicks;
QueryPerformanceCounter(&HPTicks);
Secs = ((double)HPTicks.QuadPart / HPCounterTicksPersec);
}
else
{
// Use clock with less resolution.
Secs = GetTickCount();
Secs /= 1000.0;
}
return Secs;
}
//******************************************************************************
CProto_Util::CProto_Util()
{
g_pLogger = new CLogger(_T("\\UtilityDebug.Log"));
};
//******************************************************************************
CProto_Util::~CProto_Util()
{
delete g_pLogger;
g_pLogger = NULL;
}
#pragma warning(disable:4996)
//******************************************************************************
// Send is direct and async.
// The receive thread will receive data and interpret it.
//******************************************************************************
SSI_STATUS CProto_Util::Initialize()
{
SSI_STATUS Status=SSI_STATUS_NORMAL;
UNREFERENCED_PARAMETER(Status);
if (g_pLogger->m_lLogMask & LOGACTIONS)
g_pLogger->SendAndFlushPerMode(_T("Exit Initialize Usb\n"));
return SSI_STATUS_NORMAL;
}
void CProto_Util::SetOutFile(FILE* pOutFile)
{
SSI_STATUS Status=SSI_STATUS_NORMAL;
UNREFERENCED_PARAMETER(Status);
m_pOutFile = pOutFile;
};
//******************************************************************************
SSI_STATUS CProto_Util::Terminate()
{
SSI_STATUS Status=SSI_STATUS_NORMAL;
if (g_pLogger->m_lLogMask & LOGACTIONS)
g_pLogger->SendAndFlushPerMode(_T("Exit Exit_MvUsb\n"));
return Status;
}
//******************************************************************************
SSI_STATUS CProto_Util::ExtractAppPath(CString &Path)
{
CString tmpPath = Path;
tmpPath.TrimRight();
tmpPath.TrimLeft();
int nLastSlash = tmpPath.ReverseFind('\\');
if (nLastSlash > -1)
{ // complete path
tmpPath = Path.Left(nLastSlash);
Path = tmpPath;
}
else
{ // not a complete path
Path="";
};
return SSI_STATUS_NORMAL;
};
//******************************************************************************
SSI_STATUS CProto_Util::GetAppPath(CString &Path)
{
Path=_T(""); // Speed optimization - noticed slow in GlowCode
if (Path.IsEmpty())
{
CString tmpPath;
GetModuleFileName(NULL,tmpPath.GetBuffer(255),255);
tmpPath.ReleaseBuffer();
tmpPath.TrimRight();
int nLastSlash = tmpPath.ReverseFind('\\');
if (nLastSlash >= 0)
tmpPath = tmpPath.Left(nLastSlash);
else
tmpPath.Empty();
Path=tmpPath;
}
return SSI_STATUS_NORMAL;
};
void CProto_Util::_ascii2bin(char *cBuff)
{
char x[3];
char t[MAX_BUFF_SIZE];
int iSize = strlen(cBuff)/2;
memset(t, 0, MAX_BUFF_SIZE);
for (int j=0;j<iSize;j++)
{
memset(x, 0, 3);
memcpy(x, cBuff+(j*2), 2);
sscanf_s(x, "%2x", &(t[j]));
};
memset(cBuff, 0, MAX_BUFF_SIZE);
memcpy(cBuff, t, iSize);
};
void CProto_Util::_clear_blanks(char *cBuff)
{
char pBuff[MAX_BUFF_SIZE];
int j = strlen(cBuff);
int k = 0;
for (int i=0; i<j; i++)
{
if ((cBuff[i]!=' ') && (cBuff[i]!= 0x0d) && (cBuff[i]!= 0x0a) ) pBuff[k++] = cBuff[i];
};
pBuff[k] = 0;
strcpy(cBuff, pBuff);
return;
};
//=====================================================================================
void CProto_Util::Dump_Array_Ascii(char *cBuff, char iEP, char *cResult)
{
int iLen = strlen(cBuff);
char cBytes[MAX_BUFF_SIZE];
memset(cBytes, 0, MAX_BUFF_SIZE);
if (iEP == '2') iLen = (iLen-6)/2;
for (int i=0;i<iLen;i++)
{
sscanf_s((const char *)(cBuff+i*2), "%2x", (cBytes+i));
};
fprintf(m_pOutFile, cBytes);
strcpy(cResult, cBytes);
// Build_Mitutoyo_Data_Matrix(cBytes, iEP);
return;
}
//=====================================================================================
//
//=====================================================================================
void CProto_Util::Dump_Array_LONG_MM(char *cBuff, int iLen)
{
char cLong_array[64][9];
long lLong_array[64];
double mm;
for (int i=0;i<iLen;i++)
{
memcpy(&(cLong_array[i][0]), cBuff+8*i, 8);
sscanf_s(&(cLong_array[i][0]), "%8x", &(lLong_array[i]));
mm = lLong_array[i]/(25.4*1000);
fprintf(m_pOutFile, "%6.3f ", mm );
};
return;
}
//=====================================================================================
//
//=====================================================================================
void CProto_Util::Dump_BYTES(char *cBuff, int iLen)
{
char x[MAX_BUFF_SIZE];
memset(x, 0, MAX_BUFF_SIZE);
memcpy(x, cBuff, iLen);
fprintf(m_pOutFile, "%s ", x );
return;
}
//=====================================================================================
// Dump an array of short INT
//=====================================================================================
void CProto_Util::Dump_SHORT_INT(char *cBuff)
{
char x[5];
int iShort;
memset(x, 0, 5);
x[0] = *(cBuff+2);
x[1] = *(cBuff+3);
x[2] = *(cBuff+0);
x[3] = *(cBuff+1);
iShort = HexToInt(x, 4);
fprintf(m_pOutFile, "%8i ", iShort );
return;
}
//=====================================================================================
//
//=====================================================================================
void CProto_Util::Dump_Array_BYTE(char *cBuff)
{
char x[3];
memset(x, 0, sizeof(x));
memcpy(x, cBuff, 2);
fprintf(m_pOutFile, "%s ", x );
return;
}
//=====================================================================================
void CProto_Util::Dump_Array_UINT_TO_INCH(char *cBuff, int iLen)
{
char cLong_array[64][7];
long lLong_array[64];
for (int i=0;i<iLen;i++)
{
memcpy(&(cLong_array[i][0]), cBuff+4*i, 4);
sscanf_s(&(cLong_array[i][0]), "%4x", &(lLong_array[i]));
fprintf(m_pOutFile, "%06.6f ", lLong_array[i]/(25.4 * 1000.0) );
};
return;
};
//=====================================================================================
//
//=====================================================================================
void CProto_Util::Dump_Array_UINT(char *cBuff, int iLen)
{
char cLong_array[64][7];
long lLong_array[64];
for (int i=0;i<iLen;i++)
{
memcpy(&(cLong_array[i][0]), cBuff+4*i, 4);
sscanf_s(&(cLong_array[i][0]), "%4x", &(lLong_array[i]));
fprintf(m_pOutFile, "%06d ", lLong_array[i] );
};
return;
}
long CProto_Util::_ascii2long_rev(char *cBuff)
{
char cLong[9];
long lLong;
cLong[7] = *cBuff;
cLong[6] = *(cBuff+1);
cLong[5] = *(cBuff+2);
cLong[4] = *(cBuff+3);
cLong[3] = *(cBuff+4);
cLong[2] = *(cBuff+5);
cLong[1] = *(cBuff+6);
cLong[0] = *(cBuff+7);
sscanf_s(cLong, "%8x", &lLong);
return lLong;
};
//=====================================================================================
//
void CProto_Util::Dump_Array_LONG_REV(char *cBuff, int iLen)
{
char cLong[9];
long lLong;
int _iLen = (iLen>64) ? 64 : iLen; // only process 64 max
for (int i=0;i<_iLen;i++)
{
cLong[6] = *(cBuff+8*i+0);
cLong[7] = *(cBuff+8*i+1);
cLong[4] = *(cBuff+8*i+2);
cLong[5] = *(cBuff+8*i+3);
cLong[2] = *(cBuff+8*i+4);
cLong[3] = *(cBuff+8*i+5);
cLong[0] = *(cBuff+8*i+6);
cLong[1] = *(cBuff+8*i+7);
sscanf_s(cLong, "%8x", &lLong);
fprintf(m_pOutFile, "%08d ", lLong);
};
return;
};
//=====================================================================================
//
//=====================================================================================
void CProto_Util::Dump_Array_LONG(char *cBuff, int iLen, int iSize)
{
char cLong_array[64][9];
long lLong_array[64];
for (int i=0;i<iLen;i++)
{
/*
if(bSo7)
memcpy(&(cLong_array[i][0]), cBuff+6*i, 6);
else
memcpy(&(cLong_array[i][0]), cBuff+8*i, 8);
*/
memcpy(&(cLong_array[i][0]), cBuff+iSize*i, iSize);
sscanf_s(&(cLong_array[i][0]), "%8x", &(lLong_array[i]));
fprintf(m_pOutFile, "%08d ", lLong_array[i] );
};
return;
}
//===================================================================
int CProto_Util::HexToInt(char *Data, int Bytes)
{
int Byte;
int HexChar, Value;
Value = 0;
for( Byte = 0; Byte < Bytes; Byte++ )
{
Value <<= 4;
HexChar = *Data++ -= '0';
if( HexChar > 32 )
HexChar -= 39;
else if( HexChar > 9 )
HexChar -= 7;
Value += HexChar;
}
return( Value );
}
//===================================================================
const char *CProto_Util::byte_to_binary ( int x )
{
static char b[9];
b[0] = '\0';
int z;
for (z = 256; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}