////////////////////////////////////////////////////////////////////////////// ////// ////// ////// 文 件: Global.cpp ////// ////// 作 者: jesse ////// ////// 创建时间: 2010-04-15 ////// ////// 说 明: 定义全局变量,全局函数 ////// ////// ////// ////// 修改时间: ////// ////// 修改说明: ////// ////// ////// ////////////////////////////////////////////////////////////////////////////// #pragma once #include "stdafx.h" #include "Global.h" #include #include INT g_nPrecision = 4; /*字符大小写互换*/ void UpperLowerSwap(char *szMsg) { for (int i=0; i< (int)strlen(szMsg); i++) { if ((szMsg[i]>='A') && (szMsg[i]<='Z')) { szMsg[i] = szMsg[i] + 32; } else if ((szMsg[i]>='a') && (szMsg[i]<='z')) { szMsg[i] = szMsg[i] - 32; } else { szMsg[i] = szMsg[i]; } } } /*字符全转为大写*/ void ByteToUpper(char *szMsg) { for (int i=0; i < static_cast(strlen(szMsg)); i++) { if ((szMsg[i]>='a') && (szMsg[i]<='z')) { szMsg[i] = szMsg[i] - 32; } else { szMsg[i] = szMsg[i]; } } } /*字符全转为小写*/ void ByteToLower(char *szMsg) { for (int i=0; i < static_cast(strlen(szMsg)); i++) { if ((szMsg[i]>='A') && (szMsg[i]<='Z')) { szMsg[i] = szMsg[i] + 32; } else { szMsg[i] = szMsg[i]; } } } /*一串字符的每个字节转换为两个字节*/ //szMsg, 要转换的消息 //szConvMsg 转换后的消息 void ByteToTwoByte( char *szMsg,char *szConvMsg) { char ch[3]; for (int i=0; i < static_cast(strlen(szMsg)); i++) { memset(ch, 0, 3); //itoa(int(szMsg[i]), ch,16); itoa(static_cast(szMsg[i]), ch, 16); szConvMsg[i*2] = ch[0]; szConvMsg[i*2 + 1] = ch[1]; } } /*一串字符的每两个字节转换为一个字节*/ void TwoByteToByte( char *szMsg, /*要转换的消息*/ char *szConvMsg /*转换后的消息*/ ) { for (int i=0; i< static_cast(strlen(szMsg)/2); i++) { szConvMsg[i] = szMsg[i*2] << 4 | szMsg[i*2 + 1]; } } /*十六进制Ascii字符转整数*/ INT AsciiToBYTE(char szMsg) { int iConvMsg = static_cast(szMsg); //iConvMsg = (int)szMsg; if ((szMsg >= 'A')&&(szMsg <= 'F')) { iConvMsg = iConvMsg - 'A' + 10; } else if ((szMsg >= 'a')&&(szMsg <= 'f')) { iConvMsg = iConvMsg - 'a' + 10; } else if ((szMsg >= '0')&&(szMsg <= '9')) { iConvMsg -= '0'; } return iConvMsg; } /*获得chksum*/ void GetChkSum( char *szMsg, /*要转换的消息*/ char *szConvMsg, /*转换后的消息*/ int len ) { unsigned short iMsg; iMsg = 0; for (int i=1;i<=(len-6);i++) { iMsg += szMsg[i]; //ASSIC码值相加 } iMsg = iMsg%65536; iMsg = ~iMsg + 1; itoa(iMsg,szConvMsg,16); ByteToUpper(szConvMsg); } /*电总协议chksum校验*/ bool ChkSumCheck( char *szMsg, /*要转换的消息*/ int len ) { char szConvMsg[5]; /*转换后的消息*/ char ChkSum[5]; int j = 0; memset(szConvMsg, 0, 5); memset(ChkSum, 0, 5); for (int i=(len-5); i<(len-1); i++) { ChkSum[j] = szMsg[i]; j++; } GetChkSum(szMsg, szConvMsg, len); ByteToUpper(szConvMsg); ByteToUpper(ChkSum); if (memcmp(szConvMsg, ChkSum, 4) == 0) { return true; } else { return false; } } /*电总协议Length校验*/ bool CheckLength( char *szMsg ) { //char *szConvMsg; WORD len ,index , lenID; len = 4; index = 9; char ch[4]; for (int i=0;i(btSrc % 10 + '0'); chDest = lowercase2uppercase(chDest); return chDest; } else { chDest = ByteToAscii( btSrc / 10 ) + static_cast( btSrc % 10 + '0' ); chDest = lowercase2uppercase(chDest); return chDest; } } char lowercase2uppercase(BYTE btSrc) { if( btSrc >= 'a' && btSrc <= 'z' ) { return btSrc - 'a' + 'A'; } return btSrc; } INT DataConversion(char *szType, char *szMsg, char *szConvMsg, int len, int StaBit, int EndBit) { if ( strcmp(szType, "FLOAT") == 0) { TypeIsFLOAT(szMsg, szConvMsg); } else if( strcmp(szType, "WORD") == 0) { TypeIsWORD(szMsg, szConvMsg); } else if( strcmp(szType, "SHORT") == 0) { TypeIsSHORT(szMsg, szConvMsg); } else if ( strcmp(szType, "BYTE") == 0 ) { TypeIsBYTE(reinterpret_cast(szMsg), reinterpret_cast(szConvMsg), len); } else if ( strcmp(szType, "CHAR") == 0 ) { TypeIsCHAR(szMsg, szConvMsg, len); } else if( strcmp(szType, "BIT") == 0) { TypeIsBIT(szMsg, szConvMsg, len, StaBit, EndBit); } else if (strcmp(szType, "UINT") == 0) { TypeIsUINT(szMsg, szConvMsg, len); } else if (strcmp(szType, "ONEBYTE") == 0) { TypeIsONEBYTE(szMsg, szConvMsg, len); } else if ( strcmp(szType,"DL") == 0) { _BYTE_1_Decimal(szConvMsg,(BYTE*)szMsg); } else if ( strcmp(szType,"WBIT") == 0) { int nSour = szMsg[0]; //ASCII_to_Byte(szMsg[0]) *16 + ASCII_to_Byte(szMsg[1]); GetWORDBit(szConvMsg,nSour,StaBit,EndBit); } return 0; } //将32 33 37 2E 30 转换为237.0 INT TypeIsUINT(char *szMsg, char *szConvMsg, int len) { char strBuf[10] = {0}; //memcpy(szConvMsg, szMsg, len); sprintf(szConvMsg,"%lf",atof(szMsg)); return 0; } //一个字节 0x31转换为1 INT TypeIsONEBYTE(char *szMsg, char *szConvMsg, int len) { char strBuf[5] = {0}; szConvMsg[0] = szMsg[0]; return 0; } INT TypeIsFLOAT(char *szMsg, char *szConvMsg) { union { float fNum ; BYTE ch[4]; }uBtoF; BYTE ch[8] = {0}; memcpy(ch, szMsg, 8); //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg)); uBtoF.ch[0] = (AsciiToBYTE(szMsg[0])<<4) | AsciiToBYTE(szMsg[1]); uBtoF.ch[1] = (AsciiToBYTE(szMsg[2])<<4) | AsciiToBYTE(szMsg[3]); uBtoF.ch[2] = (AsciiToBYTE(szMsg[4])<<4) | AsciiToBYTE(szMsg[5]); uBtoF.ch[3] = (AsciiToBYTE(szMsg[6])<<4) | AsciiToBYTE(szMsg[7]); sprintf(szConvMsg,"%f",uBtoF.fNum); //LOG4C((LOG_NOTICE,"ch = %s ; szConvMsg = %s", ch, szConvMsg)); return 0; } INT TypeIsSHORT(char *szMsg, char *szConvMsg) { WORD hW; BYTE a[2] = {0}; BYTE ch[4] = {0}; memcpy(ch, szMsg, 4); a[0] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]); a[1] = AsciiToBYTE(ch[2]) << 4 | AsciiToBYTE(ch[3]); hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF ); //hW = atoi(a); sprintf(szConvMsg,"%d",hW); return 0; } INT TypeIsWORD(char *szMsg, char *szConvMsg) { WORD hW; BYTE a[2] = {0}; int i; BYTE ch[4] = {0}; for (i=0;i<4;i++) { ch[i] = szMsg[i]; } a[0] = AsciiToBYTE(ch[0])*16 + AsciiToBYTE(ch[1]); a[1] = AsciiToBYTE(ch[2])*16 + AsciiToBYTE(ch[3]); hW = (a[0] & 0xFFFF) << 8 | ( a[1] & 0xFFFF ); //hW = atoi(a); sprintf(szConvMsg,"%d",hW); //LOG4C((LOG_NOTICE,"szMsg = %s ; szConvMsg = %s", szMsg, szConvMsg)) return 0; } // 市委未用到,回去再改 INT TypeIsDWORD(char *szMsg, char *szConvMsg) { char nbuf[VAR_MSG] = {0}; char nchar; char nchar1; char nchar2; char nchar3; for (int i = 0; i<(int)strlen(szMsg); i++) { nbuf[i] = static_cast(AsciiToBYTE(szMsg[i])); } nchar = nbuf[0]<<4 | nbuf[1]; nchar1 = nbuf[2]<<4 | nbuf[3]; nchar2 = nbuf[4]<<4 | nbuf[5]; nchar3 = nbuf[6]<<4 | nbuf[7]; if ((nchar == 0) && (nchar1 == 0) && (nchar2 == 0) && (nchar3 == 0)) { sprintf(szConvMsg, "0"); } else { sprintf(szConvMsg, "%c%c%c%c",nchar, nchar1, nchar2, nchar3); } return 0; } INT TypeIsBYTE(BYTE *szMsg, BYTE *szConvMsg, int len) { //LOG4C((LOG_NOTICE, "TypeIsBYTE")); //LOG4C_HEX_DUMP((LOG_INFO, (char *)szMsg, 5)); BYTE btChar; char ch[3] = {0}; for (int i=0; i(szConvMsg),"%d",btChar); //szConvMsg[i] = AsciiToBYTE(ch[0]) << 4 | AsciiToBYTE(ch[1]); return 0; } INT TypeIsCHAR(char *szMsg, char *szConvMsg, int len) { BYTE ch[3]; for (int i=0; i(strlen(pTmpBuf)) - 1; char chValue[16] = {0}; strcpy(chValue, pTmpBuf); for (int i =0; i<=iLen; i++) { pTmpBuf[i] = chValue[iLen - i]; } for (int k = 0; k= 'A' && str[i] <= 'Z' ) { str[i] += 32; } else if(str[i] >= 'a' && str[i] <= 'z') { str[i] -= 32; } } } } //由数据精度获得浮点数的字符串值 CString GetDoubleText(double d) { CString strRtn; CString strFmt; if(g_nPrecision == 0) strFmt = "%g"; else { long lV = static_cast(fabs(d)); if(lV >= 1) { int nIntBit = 0; while(lV >= 1) { lV = lV/10; nIntBit++; } if(nIntBit < g_nPrecision) strFmt.Format("%c%d.%df", '%', nIntBit, (g_nPrecision-nIntBit)); else strFmt.Format("%c%d.0f", '%', nIntBit); } else { double dTemp = fabs(d); if(dTemp > 0.00000000001) { int nDot0Bit = 0; while(dTemp < 0.1) { dTemp = dTemp*10; nDot0Bit++; } strFmt.Format("%c1.%df", '%', nDot0Bit + g_nPrecision); } else { strFmt = "%g"; } } } strRtn.Format(strFmt, d); return strRtn; } char Hex16(char WillChangeNum[]) //该函数把四位二进制转换成十六进制数 { int i; i = (WillChangeNum[3]) + (WillChangeNum[2] * 10) + (WillChangeNum[1] * 100) + (WillChangeNum[0] * 1000); switch(i) { case 0: return 0; case 1: return 1; case 10: return 2; case 11: return 3; case 100: return 4; case 101: return 5; case 110: return 6; case 111: return 7; case 1000: return 8; case 1001: return 9; case 1010: return 0x0A; case 1011: return 0x0B; case 1100: return 0x0C; case 1101: return 0x0D; case 1110: return 0x0E; case 1111: return 0x0F; } return -1; } #define CPH 0 #define CPL 1 static unsigned char tbcrch[] = { 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 1,192,128,65,0,193,129,64,0,193,129,64,1,192,128,65, 0,193,129,64,1,192,128,65,1,192,128,65,0,193,129,64,}; static unsigned char tbcrcl[] = { 0,192,193,1,195,3,2,194,198,6,7,199,5,197,196,4, 204,12,13,205,15,207,206,14,10,202,203,11,201,9,8,200, 216,24,25,217,27,219,218,26,30,222,223,31,221,29,28,220, 20,212,213,21,215,23,22,214,210,18,19,211,17,209,208,16, 240,48,49,241,51,243,242,50,54,246,247,55,245,53,52,244, 60,252,253,61,255,63,62,254,250,58,59,251,57,249,248,56, 40,232,233,41,235,43,42,234,238,46,47,239,45,237,236,44, 228,36,37,229,39,231,230,38,34,226,227,35,225,33,32,224, 160,96,97,161,99,163,162,98,102,166,167,103,165,101,100,164, 108,172,173,109,175,111,110,174,170,106,107,171,105,169,168,104, 120,184,185,121,187,123,122,186,190,126,127,191,125,189,188,124, 180,116,117,181,119,183,182,118,114,178,179,115,177,113,112,176, 80,144,145,81,147,83,82,146,150,86,87,151,85,149,148,84, 156,92,93,157,95,159,158,94,90,154,155,91,153,89,88,152, 136,72,73,137,75,139,138,74,78,142,143,79,141,77,76,140, 68,132,133,69,135,71,70,134,130,66,67,131,65,129,128,64,}; /******************************************************************** * NAME : TwoHexCharToChar * FUNCTION : * PROCESS : * INPUT : * OUTPUT : * UPDATE : * RETURN : * : * PROGRAMMED : * DATE(ORG) : * CALL : * SYSTEM : ********************************************************************/ char TwoHexCharToChar(char ch1,char ch2) { char Numb1; char Numb2; if (ch1 >= 'A') Numb1 = (toupper(ch1)-'0'-7)*16; else Numb1 = (ch1 - '0')*16; if (ch2 >= 'A') Numb2 = (toupper(ch2) - '0' - 7); else Numb2 = (ch2 - '0'); return (Numb1 + Numb2); } /******************************************************************** * NAME : Str2HexStr * FUNCTION : * PROCESS : * INPUT : * OUTPUT : * UPDATE : * RETURN : * : * PROGRAMMED : * DATE(ORG) : * CALL : * SYSTEM : ********************************************************************/ void Str2HexStr(char *szHexString,char *szString,int *iHexStringLen) { int iLoop; int iCount; iLoop = 0; iCount = 0; szHexString[0] = '\0'; while(1) { if (szString[iLoop] == '\0' || szString[iLoop + 1] == '\0') { break; } szHexString[iCount] = static_cast(TwoHexCharToChar(szString[iLoop],szString[iLoop + 1])); iLoop ++; iLoop ++; iCount ++; } szHexString[iCount] = '\0'; *iHexStringLen = iCount; } /* Convert a binary data buffer to a hex string str: output (a string like “20ef9a“) bin: input (a data buffer) binlen: input (the length of the data buffer) */ void bin2str(char *str, const unsigned char *bin, int binlen) { int i; for(i=0; i> 4); str[2*i] = (str[2*i] > 9) ? (str[2*i] + 'A' - 10) : (str[2*i] + '0'); str[2*i+1] = (bin[i] & 0x0F); str[2*i+1] = (str[2*i+1] > 9) ? (str[2*i+1] + 'A' - 10) : (str[2*i+1] + '0'); } str[2*i] = '\0'; } // 将10进制转换为2进制字符串; char *dectobin(int dec,int len) { int i=0; static char buf[256]; char szbuf1[4] = {0}; char szbuf2[4] = {0}; char szbuf3[4] = {0}; #if 0 for (i = 0; i< 4;i++) { szbuf1[i] = dec%2+48; dec = dec/2; } for (i = 0; i< 4;i++) { szbuf2[i] = dec%2+48; dec = dec/2; } for (i = 0; i< 4;i++) { szbuf3[i] = dec%2+48; dec = dec/2; } #endif #if 1 memset(buf,0,256); memset(buf,0,len); while(dec != 0) { buf[i++]=dec%2+48; dec=dec/2; } #endif return strrev(buf); } // 算法:calc length by lenid; void ReadDZLegthbyLenid(BYTE LenID[2],BYTE Length[4]) { int ndec = atoi((char*)LenID); Length = reinterpret_cast(dectobin(ndec,12)); return; } // 将16进制字符串按顺序转为10进制字符串; // 高在前,低在后; int HexStr2Dec(char *Source) { int iLen,temp = 0,n; iLen = (int)strlen(Source);//十六进制是按字符串传进来的,所以要获得他的长度; for( int i = 0; i < iLen; i++) { if(Source[i] >= 'A' && Source[i] <= 'F') //十六进制还要判断他是不是在A-F或者a-f之间a=10; n = Source[i] - 'A' + 10; else if(Source[i] >= 'a' && Source[i] <= 'f') n = Source[i] - 'a' + 10; else n = Source[i] - '0'; temp = temp*16 + n; } return temp; } // 取字节的位; void ONEBYTE_BIT(char *szValue, const int &iSour, const int &iSBit, const int &iEBit) { if ( iSBit > iEBit) return; bitset<8> bitSor(iSour); char szBinary[9] = {0}; for ( int i = 0; i < 8; i++) szBinary[i] = bitSor[i]; if ( iSBit == iEBit) sprintf(szValue,"%d",(int)bitSor[iSBit]); else { // biset是从位0开始的; for (int i = 0; i < 8; i++) { if ( i < iSBit || i > iEBit) bitSor.reset(i); } itoa(bitSor.to_ulong(),szValue,10); } } void GetBYTEBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit) { if ( iSBit > iEBit) return; bitset<8> bitSor(iSour); char szBinary[9] = {0}; for ( int i = 0; i < 8; i++) szBinary[i] = bitSor[i]; if ( iSBit == iEBit) sprintf(szValue,"%d",(int)bitSor[iSBit]); else { // 以下无效; for (int i = 0; i < 8; i++) { if ( i < iSBit || i > iEBit) bitSor.reset(i); } itoa(bitSor.to_ulong(),szValue,10); } } // 取字节的位; void GetWORDBit(char *szValue, const int &iSour, const int &iSBit, const int &iEBit) { if ( iSBit > iEBit) return; bitset<16> bitSor(iSour); char szBinary[17] = {0}; for ( int i = 0; i < 16; i++) szBinary[i] = bitSor[i]; if ( iSBit == iEBit) sprintf(szValue,"%d",(int)bitSor[iSBit]); } // 取字节的位; void ONEBYTE_BIT2(char *szValue, const int &iSour, const int &iSBit, const int &iEBit) { if ( iSBit > iEBit) return; //int iSour = szCopy[0]; // 位顺序: 高->低 (7~0); bitset<8> bitSor(iSour); char szBinary[9] = {0}; for ( int i = 0,j = 7; i < 8; i++,j--) szBinary[j] = bitSor[i] + 48; char szCut[3] = {0}; for ( int i = iSBit,j = 0; i <= iEBit; i++,j++) szCut[j] = szBinary[i]; if ( strcmp(szCut,"00") == 0) szValue[0] = '0'; else if ( strcmp(szCut,"01") == 0) szValue[0] = '1'; else if ( strcmp(szCut,"10") == 0) szValue[0] = '2'; else if ( strcmp(szCut,"11") == 0) szValue[0] = '3'; } int GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit) { CHAR szFile[MAX_PATH + 1] = ""; wsprintf(szFile, "%s\\config\\%s", szPath, szIniName); IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的; GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile); szDataType[strlen(szDataType)] = '\0'; nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile); nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile); iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值 iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile); return 0; } UINT GetModbus16CRC(BYTE *pBuf, int len) { unsigned int Genpoly=0xA001; unsigned int CRC=0xFFFF; unsigned int index; while(len--) { CRC=CRC^(unsigned int)*pBuf++; for(index=0;index<8;index++) { if((CRC & 0x0001)==1) CRC=(CRC>>1)^Genpoly; else (CRC=CRC>>1); } } return(CRC); } INT ASCII_to_Byte(char c) { int iConvt = static_cast(c); if ((c >= 'A')&&(c <= 'F')) iConvt = iConvt - 'A' + 10; else if ((c >= 'a')&&(c <= 'f')) iConvt = iConvt - 'a' + 10; else if ((c >= '0')&&(c <= '9')) iConvt -= '0'; return iConvt; } int digit_to_hex(BYTE by) { if ( by >= 0 && by <= 9 ) return by; char szRet[3] = {0}; itoa(by,szRet,10); return TwoHexCharToChar(szRet[0],szRet[1]); } WORD ReturnASCIIWORD(BYTE *bySource) { WORD wValue = 0; wValue = bySource[0] << 8 | bySource[1]; return wValue; } WORD ReturnNASCIIWORD(BYTE *bySource) { WORD wValue = 0; wValue = bySource[1] << 8 | bySource[0]; return wValue; } void _BYTE_2_Decimal(char *szValue, BYTE *szSource) { // 高在前,低在后; WORD wValue = ReturnASCIIWORD(szSource); sprintf(szValue,"%d",wValue); } void _BYTE_1_Decimal(char *szValue, BYTE *szSource) { // 高在前,低在后; sprintf(szValue,"%d",szSource[0]); }