| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- // ProtocolPMC916.cpp: implementation of the CProtocolModbus class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "ProtocolModbus.h"
- #include "winsock2.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CProtocolModbus::CProtocolModbus() : CProtocol()
- {
- InitializeCriticalSection( &m_csReadFinished );
- InitializeCriticalSection( &m_csWrFinished );
- MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) );
- }
- CProtocolModbus::~CProtocolModbus()
- {
- DeleteCriticalSection( &m_csReadFinished );
- DeleteCriticalSection( &m_csWrFinished );
- MTVERIFY( CloseHandle( m_hSemComm ) );
- }
- int CProtocolModbus::WorkMain( int nRequestLen, //请求数据总长度
- int nResponseLen, //响应数据总长度
- int nCmd, //命令
- int nCmdPos, //变量索引
- int nCmdLen, //变量长度
- int nParam, //参数
- char chMsg[80] ) //读到变量值
- {
- if(!m_pComm)
- return ERR_CODE_UPS_POWERWARE_COM_FAULT; // 串口通信故障
- int nRet;
- #if 1
- nRet = RequestStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam, chMsg);
- #else
- if( nCmd == 0x33 || nCmd == 0x34 )
- {
- nRet = ResponseStatus(nRequestLen, nResponseLen, nCmd, nCmdPos, nCmdLen, nParam, chMsg);
- }
- else
- {
- nRet = -1;
- }
- #endif
- return nRet;
- }
- int CProtocolModbus:: WriteCommand(SETBASEPARAM SetBasePara, double dbData, int nDataLen)
- {
- int iResult = 0;
- // if(!m_pComm)
- // return ERR_CODE_MODBUS_RTU_COM_FAULT;
- // iResult = RequestWrStatus(SetBasePara, dbData, nDataLen);
- // if (iResult == 0)
- // return ResponseWrStatus(SetBasePara.nAddr, SetBasePara.nRegNum, nDataLen);
- // else
- return iResult;
- }
- int CProtocolModbus::ResponseStatus( int nRequestLen, //请求数据总长度
- int nResponseLen, //响应数据总长度
- int nCmd, //命令
- int nCmdPos, //变量索引
- int nCmdLen, //变量长度
- int nParam, //参数
- char chMsg[80] ) //读到变量值
- {
- RESPONSE_STRUCT tagResponse;
-
- memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
- int nReadLen = 0, nHeadLen = 0, nIndex = 4;
- int nLen;
- char *pTotolBuffer = NULL, *pBuffer = NULL;
- if( nCmd == 0x33 )
- {
- pTotolBuffer = new char[ 24 ];
- pBuffer = new char[ 24 ];
- memset(pTotolBuffer, 0, 24);
- memset(pBuffer, 0, 24);
- nLen = 24;
- }
- else if( nCmd == 0x34 )
- {
- pTotolBuffer = new char[ 147 ];
- pBuffer = new char[147];
- memset(pTotolBuffer, 0, 147);
- memset(pBuffer, 0, 147);
- nLen = 147;
- }
- int nTotalLen = 0;
- #if 1
- nReadLen = ReadMessage( (BYTE *)pTotolBuffer, nLen );
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_UPS_POWERWARE_COM_READ_NO_DATA;
- }
- else if( nReadLen < nLen )
- {
- SetEvent( m_hSemComm );
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_UPS_POWERWARE_COM_READ_NO_DATA;
- }
- else if( nReadLen > nLen )
- {
- SetEvent( m_hSemComm );
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_UPS_POWERWARE_COM_READ_LEN_OVER;
- }
- #else
- //AB 03 13 81 F0 1B 00 10 05 01 00 00 00 00 00 01
- //00 00 00 00 00 00 00 9C
- if( nCmd == 0x33 )
- {
- pTotolBuffer[0] = 0xAB;
- pTotolBuffer[1] = 0x03;
- pTotolBuffer[2] = 0x13;
- pTotolBuffer[3] = 0x81;
- pTotolBuffer[4] = 0xF0;
- pTotolBuffer[5] = 0x1B;
- pTotolBuffer[6] = 0x00;
- pTotolBuffer[7] = 0x10;
- pTotolBuffer[8] = 0x05;
- pTotolBuffer[9] = 0x01;
- pTotolBuffer[10] = 0x00;
- pTotolBuffer[11] = 0x00;
- pTotolBuffer[12] = 0x00;
- pTotolBuffer[13] = 0x00;
- pTotolBuffer[14] = 0x00;
- pTotolBuffer[15] = 0x01;
- pTotolBuffer[16] = 0x00;
- pTotolBuffer[17] = 0x00;
- pTotolBuffer[18] = 0x00;
- pTotolBuffer[19] = 0x00;
- pTotolBuffer[20] = 0x00;
- pTotolBuffer[21] = 0x00;
- pTotolBuffer[22] = 0x00;
- pTotolBuffer[23] = 0x9C;
- }
- else if( nCmd == 0x34 )
- {
- //AB 04 40 01 00 00 D0 40 00 00 D0 40 00 00 D0 40 ?.@...?@..?@..?@
- //00 00 00 00 00 80 3B 45 00 00 00 00 00 60 86 45 .....?;E.....`?E
- //00 00 00 00 00 A3 30 3F 00 00 48 42 00 00 48 42 .....?0?..HB..HB
- //AB 04 40 01 C0 CC 04 41 C0 CC 04 41 C0 CC 04 41
- //00 00 00 00 00 E0 92 45 00 00 00 00 00 00 AF 45
- //00 00 00 00 00 7A 54 3F 00 00 48 42 00 00 48 42
- pTotolBuffer[0] = 0xAB;
- pTotolBuffer[1] = 0x04;
- pTotolBuffer[2] = 0x40;
- pTotolBuffer[3] = 0x01;
- pTotolBuffer[4] = 0xC0;
- pTotolBuffer[5] = 0xCC;
- pTotolBuffer[6] = 0x04;
- pTotolBuffer[7] = 0x41;
- pTotolBuffer[8] = 0xC0;
- pTotolBuffer[9] = 0xCC;
- pTotolBuffer[10] = 0x04;
- pTotolBuffer[11] = 0x41;
- pTotolBuffer[12] = 0xC0;
- pTotolBuffer[13] = 0xCC;
- pTotolBuffer[14] = 0x04;
- pTotolBuffer[15] = 0x41;
- pTotolBuffer[16] = 0x00;
- pTotolBuffer[17] = 0x00;
- pTotolBuffer[18] = 0x00;
- pTotolBuffer[19] = 0x00;
- pTotolBuffer[20] = 0x00;
- pTotolBuffer[21] = 0xE0;
- pTotolBuffer[22] = 0x92;
- pTotolBuffer[23] = 0x45;
- pTotolBuffer[24] = 0x00;
- pTotolBuffer[25] = 0x00;
- pTotolBuffer[26] = 0x00;
- pTotolBuffer[27] = 0x00;
- pTotolBuffer[28] = 0x00;
- pTotolBuffer[29] = 0x00;
- pTotolBuffer[30] = 0xAF;
- pTotolBuffer[31] = 0x45;
- pTotolBuffer[32] = 0x00;
- pTotolBuffer[33] = 0x00;
- pTotolBuffer[34] = 0x00;
- pTotolBuffer[35] = 0x00;
- pTotolBuffer[36] = 0x00;
- pTotolBuffer[37] = 0x7A;
- pTotolBuffer[38] = 0x54;
- pTotolBuffer[39] = 0x3F;
- pTotolBuffer[40] = 0x00;
- pTotolBuffer[41] = 0x00;
- pTotolBuffer[42] = 0x48;
- pTotolBuffer[43] = 0x42;
- pTotolBuffer[44] = 0x00;
- pTotolBuffer[45] = 0x00;
- pTotolBuffer[46] = 0x48;
- pTotolBuffer[47] = 0x42;
- //98 99 47 42 00 66 66 BF 66 86 0A 44 00 00 C6 42 ??GB.ff?f?.D..?B
- //F7 70 00 00 A1 AB 04 40 02 33 B3 60 43 00 00 60 ÷p..??.@.3?`C..`
- //43 33 33 61 43 CC 4C 60 43 CC 4C 60 43 66 66 61 C33aC?L`C?L`Cffa
- //43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4 C..............?
- //42 00 00 E4 42 00 00 E4 42 22 01 10 A0 48 36 43 B..?B..?B"..?H6C
- //15 00 00 63 43 99 19 62 43 DB AB 04 04 83 66 66 ...cC?.bC??..?ff
- //61 43 5A
- //00 00 48 42 00 99 19 3F 33 F3 05 44 00 00 C6 42
- //F7 70 00 00 78 AB 04 40 02 99 19 65 43 33 B3 64
- pTotolBuffer[48] = 0x00;
- pTotolBuffer[49] = 0x00;
- pTotolBuffer[50] = 0x48;
- pTotolBuffer[51] = 0x42;
- pTotolBuffer[52] = 0x00;
- pTotolBuffer[53] = 0x99;
- pTotolBuffer[54] = 0x19;
- pTotolBuffer[55] = 0x3F;
- pTotolBuffer[56] = 0x33;
- pTotolBuffer[57] = 0xF3;
- pTotolBuffer[58] = 0x05;
- pTotolBuffer[59] = 0x44;
- pTotolBuffer[60] = 0x00;
- pTotolBuffer[61] = 0x00;
- pTotolBuffer[62] = 0xC6;
- pTotolBuffer[63] = 0x42;
- pTotolBuffer[64] = 0xF0;
- pTotolBuffer[65] = 0x70;
- pTotolBuffer[66] = 0x00;
- pTotolBuffer[67] = 0x00;
- pTotolBuffer[68] = 0x78;
- pTotolBuffer[69] = 0xAB;
- pTotolBuffer[70] = 0x04;
- pTotolBuffer[71] = 0x40;
- pTotolBuffer[72] = 0x02;
- pTotolBuffer[73] = 0x99;
- pTotolBuffer[74] = 0x19;
- pTotolBuffer[75] = 0x65;
- pTotolBuffer[76] = 0x43;
- pTotolBuffer[77] = 0x33;
- pTotolBuffer[78] = 0xB3;
- pTotolBuffer[79] = 0x64;
- //43 66 66 65 43 66 E6 64 43 99 19 65 43 33 B3 64
- //43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E4
- pTotolBuffer[80] = 0x43;
- pTotolBuffer[81] = 0x66;
- pTotolBuffer[82] = 0x66;
- pTotolBuffer[83] = 0x65;
- pTotolBuffer[84] = 0x43;
- pTotolBuffer[85] = 0x66;
- pTotolBuffer[86] = 0xE6;
- pTotolBuffer[87] = 0x64;
- pTotolBuffer[88] = 0x43;
- pTotolBuffer[89] = 0x99;
- pTotolBuffer[90] = 0x19;
- pTotolBuffer[91] = 0x65;
- pTotolBuffer[92] = 0x43;
- pTotolBuffer[93] = 0x33;
- pTotolBuffer[94] = 0xB3;
- pTotolBuffer[95] = 0x64;
- pTotolBuffer[96] = 0x43;
- pTotolBuffer[97] = 0x00;
- pTotolBuffer[98] = 0x00;
- pTotolBuffer[99] = 0x00;
- pTotolBuffer[100] = 0x00;
- pTotolBuffer[101] = 0x00;
- pTotolBuffer[102] = 0x00;
- pTotolBuffer[103] = 0x00;
- pTotolBuffer[104] = 0x00;
- pTotolBuffer[105] = 0x00;
- pTotolBuffer[106] = 0x00;
- pTotolBuffer[107] = 0x00;
- pTotolBuffer[108] = 0x00;
- pTotolBuffer[109] = 0x00;
- pTotolBuffer[110] = 0x00;
- pTotolBuffer[111] = 0xE4;
- //42 00 00 E4 42 00 00 E4 42 06 02 10 A0 31 48 59
- //18 99 19 62 43 00 00 61 43 CB AB 04 04 83 00 00
- //60 43 27
- pTotolBuffer[112] = 0x42;
- pTotolBuffer[113] = 0x00;
- pTotolBuffer[114] = 0x00;
- pTotolBuffer[115] = 0xE4;
- pTotolBuffer[116] = 0x42;
- pTotolBuffer[117] = 0x00;
- pTotolBuffer[118] = 0x00;
- pTotolBuffer[119] = 0xE4;
- pTotolBuffer[120] = 0x42;
- pTotolBuffer[121] = 0x06;
- pTotolBuffer[122] = 0x02;
- pTotolBuffer[123] = 0x10;
- pTotolBuffer[124] = 0xA0;
- pTotolBuffer[125] = 0x31;
- pTotolBuffer[126] = 0x48;
- pTotolBuffer[127] = 0x59;
- pTotolBuffer[128] = 0x18;
- pTotolBuffer[129] = 0x99;
- pTotolBuffer[130] = 0x19;
- pTotolBuffer[131] = 0x62;
- pTotolBuffer[132] = 0x43;
- pTotolBuffer[133] = 0x00;
- pTotolBuffer[134] = 0x00;
- pTotolBuffer[135] = 0x61;
- pTotolBuffer[136] = 0x43;
-
- pTotolBuffer[137] = 0xCB;
- pTotolBuffer[138] = 0xAB;
- pTotolBuffer[139] = 0x04;
- pTotolBuffer[140] = 0x04;
- pTotolBuffer[141] = 0x83;
- pTotolBuffer[142] = 0x00;
- pTotolBuffer[143] = 0x00;
- pTotolBuffer[144] = 0x60;
- pTotolBuffer[145] = 0x43;
- pTotolBuffer[146] = 0x27;
- }
- #endif
- try
- {
- memcpy( &tagResponse, pTotolBuffer, sizeof(RESPONSE_STRUCT) );
- int nTempLen = tagResponse.chLen & 0x000000FF;
- if( nCmd == 0x33 )
- {
- // 判断校验
- char chCheckSum;
- GetCheckSum(pTotolBuffer, chCheckSum, nLen);
- if ( !(chCheckSum = pTotolBuffer[nLen -1]) )
- {
- //TRACE("校验失败!\r\n");
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- if( nTempLen == 0x13 )
- {
- memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen );
- }
- }
- else if( nCmd == 0x34 )
- {
- if( nTempLen == 0x40 )
- {
- // 判断校验
- char chCheckSum;
- GetCheckSum(pTotolBuffer, chCheckSum, nTempLen + nIndex + 1);
- if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex]) )
- {
- //TRACE("校验失败!\r\n");
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- if( nTotalLen + nTempLen >= nLen || nIndex + nTempLen >= nLen )
- {
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
- nTotalLen += nTempLen;
- nIndex += nTempLen;
- nIndex += 1; //去掉尾
- #if 0
- // 判断校验
- GetCheckSum(pTotolBuffer + nIndex, chCheckSum, nTempLen + nIndex + 4 + 1);
- if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex + 4]) )
- {
- //TRACE("校验失败!\r\n");
- AfxMessageBox("校验失败!\r\n");
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- #endif
-
- memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
- if( nIndex + sizeof(RESPONSE_STRUCT) >= nLen )
- {
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- memcpy( &tagResponse, pTotolBuffer + nIndex, sizeof(RESPONSE_STRUCT) );
- nTempLen = tagResponse.chLen & 0x000000FF;
- nIndex += sizeof(RESPONSE_STRUCT); //去掉头
- if( nTempLen == 0x40 )
- {
- if( nTotalLen + nTempLen >= nLen || nIndex + nTempLen >= nLen )
- {
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
- nTotalLen += nTempLen;
- nIndex += nTempLen;
- nIndex += 1; //去掉尾
- #if 0
- // 判断校验
- GetCheckSum(pTotolBuffer + nIndex, chCheckSum, nTempLen + nIndex + 4 + 1);
- if ( !(chCheckSum = pTotolBuffer[nTempLen + nIndex + 4]) )
- {
- //TRACE("校验失败!\r\n");
- AfxMessageBox("校验失败!\r\n");
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- SetEvent( m_hSemComm );
- return ERR_CODE_UPS_POWERWARE_COM_LRC_LOST;
- }
- #endif
- memset( &tagResponse, 0, sizeof(RESPONSE_STRUCT) );
- if( nIndex + sizeof(RESPONSE_STRUCT) < nLen )
- {
- memcpy( &tagResponse, pTotolBuffer + nIndex, sizeof(RESPONSE_STRUCT) );
- }
- nTempLen = tagResponse.chLen & 0x000000FF;
- nIndex += sizeof(RESPONSE_STRUCT); //去掉头
- if( nTempLen == 0x04 )
- {
- if( nTotalLen + nTempLen < nLen && nIndex + nTempLen < nLen )
- {
- memcpy(pBuffer + nTotalLen, pTotolBuffer + nIndex, nTempLen);
- }
- }
- }
- }
- }
- //取变量数据
- EnterCriticalSection( &m_csWrFinished );
- for( int i = 0; i < nCmdLen; i++ )
- {
- if( nCmdPos + i >= nLen ) break;
- chMsg[i] = pBuffer[nCmdPos + i];
- }
- LeaveCriticalSection(&m_csWrFinished);
- }
- catch(...)
- {
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- return -1;
- }
- if( pTotolBuffer != NULL)
- {
- delete[] pTotolBuffer;
- pTotolBuffer = NULL;
- }
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- return 0;
- }
- int CProtocolModbus::RequestStatus( int nRequestLen, //请求数据总长度
- int nResponseLen, //响应数据总长度
- int nCmd, //命令
- int nCmdPos, //变量索引
- int nCmdLen, //变量长度
- int nParam ) //参数
- {
- int nLen = sizeof(REQUESTPARAM);
- REQUESTPARAM RequestPara;
- memset( &RequestPara, 0, nLen );
- //起始位
- RequestPara.chSoi = 0xAB;
-
- //请求长度
- RequestPara.chLen = (BYTE)nRequestLen;
- //命令
- RequestPara.chCmd = (BYTE)nCmd;
- char chCheckSum;
- GetCheckSum((char *)&RequestPara, chCheckSum, nLen );
- RequestPara.chCheckSum = chCheckSum;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- ResetEvent( m_hSemComm );
- int nResult = WriteMessage( (BYTE *)&RequestPara, nLen );
-
- if( nResult == nLen )
- {
- }
- else
- {
- SetEvent( m_hSemComm );
- return EER_CODE_UPS_POWERWARE_COM_WRITE_DATA;
- }
- }
- else
- {
- return ERR_CODE_UPS_POWERWARE_COM_BUSY;
- }
- return 0;
- }
- void CProtocolModbus::GetCheckSum(char *pBuf, char &chDest, int len)
- {
- WORD iSum = 0;
- for(int i=0; i<len; i++)//求和
- {
- iSum += pBuf[i];
- }
- WORD iCompliment = iSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- chDest = (BYTE)iCompliment;
- }
- BOOL CProtocolModbus::InitParam(PPORTPARAM pPortParam, CCommAsyn *pComm)
- {
- int addr=pPortParam->StartAddr;
- m_pComm=pComm;
- return TRUE;
- }
- int DigitToBinary(WORD wdSource, char* pDes, int iBit)
- {
- char pTmpBuf[16] = {0};
- char chBuffer[16] = {0};
- //wdSource =htonl(wdSource);
- itoa(wdSource, pTmpBuf, 2);
- int iLen = (int)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<iBit; k++)
- {
-
- if ( 0x00 == pTmpBuf[iBit - k - 1])
- chBuffer[k] = 0x30;
- else
- chBuffer[k] = pTmpBuf[iBit - k - 1];
- }
- memcpy(pDes, chBuffer, iBit);
- return 0;
- }
- void strReverse( char *str )
- {
- int l = (int)strlen(str);
- for( int i = 0; i < l; i++ )
- {
- for(int i = 0; i < l; i++)
- {
- if( str[i] >= 'A' && str[i] <= 'Z' )
- {
- str[i] += 32;
- }
- else if(str[i] >= 'a' && str[i] <= 'z')
- {
- str[i] -= 32;
- }
- }
- }
- }
- char lowercase2uppercase(BYTE btSrc)
- {
- if( btSrc >= 'a' && btSrc <= 'z' )
- {
- return btSrc - 'a' + 'A';
- }
- return btSrc;
- }
- char ByteToAscii(BYTE btSrc)
- {
- char chDest;
- if( btSrc < 10 )
- {
- chDest = (char)(btSrc % 10 + '0');
- chDest = lowercase2uppercase(chDest);
- return chDest;
- }
- else
- {
- chDest = ByteToAscii( btSrc / 10 ) + (char)( btSrc % 10 + '0' );
- chDest = lowercase2uppercase(chDest);
- return chDest;
- }
- }
- WORD AsciiToBYTE(BYTE btSrc)
- {
- WORD chDest = (WORD)btSrc;
- if ((btSrc >= 'A')&&(btSrc <= 'F'))
- {
- chDest = chDest - 'A' + 10;
- }
- else if ((btSrc >= 'a')&&(btSrc <= 'f'))
- {
- chDest = chDest - 'a' + 10;
- }
- else if ((btSrc >= '0')&&(btSrc <= '9'))
- {
- chDest -= '0';
- }
- return chDest;
- }
- /*
- 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<binlen; i++)
- {
- str[2*i] = ((bin[i] & 0xF0) >> 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';
- }
|