| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975 |
- // DataMate3000.cpp: implementation of the CDataMate3000 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Daikin.h"
- #include "CommProcess.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDaiKin::CDaiKin( char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- int nRate, // 波特率
- int nDataBit, // 数据位
- int nStopBit, // 停止位
- int nParity, // 校验位
- int nInterval // 时间间隔
- )
- {
- #if IS_USE_READMSG_CS
- InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
- #endif
- MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
- for( int i = 0; i < MAX_ADDR; i++ )
- {
- memset(m_szDaKin_42Msg[i], 0, sizeof(m_szDaKin_42Msg[i]));
- memset(m_szDaKin_43Msg[i], 0, sizeof(m_szDaKin_43Msg[i]));
- memset(m_szDaKin_44Msg[i], 0, sizeof(m_szDaKin_44Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CDaiKin::~CDaiKin()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL CDaiKin::DaiKinOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
- {
- BOOL bResult = FALSE;
- bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
- return bResult;
- }
- // 发送读取设备参数请求
- int CDaiKin::SendReadRequest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szMsg[VAR_MSG], // 响应的值
- int nReversed1, // 预留整形参数1接口
- int nReversed2, // 预留整形参数2接口
- int nReversed3, // 预留整形参数3接口
- int nReversed4, // 预留整形参数4接口
- int nReversed5, // 预留整形参数5接口
- float fReversed1, // 预留float参数1接口
- float fReversed2, // 预留float参数2接口
- float fReversed3, // 预留float参数3接口
- char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
- char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
- char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
- char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
- char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
- )
- {
- int nIndex = 0, nLen = 0;
- char szSendMsg[DAIKIN_SEND_MSG] = {0};
- char szType[DK_TYPE_LENGTH] = {0};
- int startBit = 0;
- int endBit = 0;
- GetFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( (strlen(m_szDaKin_42Msg[nAddr - 1]) == 0 &&
- (strcmp(szCmd, "cmd-1") == 0 ) )|| //获取系统模拟量量化数据(浮点数)
- ( strlen(m_szDaKin_43Msg[nAddr - 1]) == 0 &&
- (strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ) )|| //获取开关输入状态
- ( strlen(m_szDaKin_44Msg[nAddr - 1]) == 0 &&
- (strcmp(szCmd, "cmd-8") == 0 ) ) ||
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-2") ==0 ||
- strcmp(szCmd, "cmd-8") == 0
- ) //获取系统参数(定点数)
- {
- nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = FALSE;
- }
- else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = TRUE;
- }
- if( m_devOnline[nAddr - 1] == FALSE )
- {
- return -1;
- }
- nRet = GetDaiKin_42VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDaiKin_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetDaiKin_44VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szCmd= %s, szMsg = %s", szCmd, szMsg));
- return nRet;
- }
- int CDaiKin::GetDaiKin_42VarMsg( int nAddr, //设备地址
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH], // 变量数据类型
- int startBit,
- int endBit )
- {
- int nRet = 0;
- if( strcmp(szCmd, "cmd-1") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
-
- DataConversion(szType, m_szDaKin_42Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
- //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CDaiKin::GetDaiKin_43VarMsg( int nAddr, //设备地址
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH],// 变量数据类型
- int startBit,
- int endBit )
- {
- int nRet = 0;
- if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szDaKin_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CDaiKin::GetDaiKin_44VarMsg(int nAddr, //设备地址
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH],// 变量数据类型
- int startBit,
- int endBit )
- {
- int nRet = 0;
- if( strcmp(szCmd, "cmd-8") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szDaKin_44Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CDaiKin::GetDeviceParam(int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH]) // 变量数据类型
- {
- int nRet = -1;
- nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CDaiKin::SetDeviceParam(int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[DAIKIN_SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH],
- int &iSendLen) // 变量数据类型
- {
- int nRet = -1;
- nRet = WriteRequestStatus(nAddr, szCmd, pComm, szSendMsg,iSendLen);
- if( nRet != 0 )
- {
- LOG4C((LOG_NOTICE,"Write Error"));
- return nRet; // 串口忙
- }
- nRet = WriteResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- void CDaiKin::SetDaKin_42VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-1") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szDaKin_42Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_42Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDaiKin::SetDaKin_43VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-2") == 0 || strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szDaKin_43Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_43Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CDaiKin::SetDaKin_44VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-8") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szDaKin_44Msg[nAddr - 1], pBuffer, sizeof(m_szDaKin_44Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- // WORD CDaiKin::GetDataLength(const WORD wLENID, char chLength[4])
- // {
- // //char szLenID[3] = {0};
- // ////int nLen = 10;
- // //DigitToBinary(wLENID, szLenID, sizeof(szLenID));
- //
- // //char chCheckSum = GetLCheckSum(szLenID, sizeof(szLenID));
- //
- // //chLength[0] = chCheckSum;
- // //chLength[1] = szLenID[0];
- // //chLength[2] = szLenID[1];
- // //chLength[3] = szLenID[2];
- //
- // return 0;
- // }
- // 发送设置设备参数请求
- int CDaiKin::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szMsg[VAR_MSG], // 响应的值
- int nReversed1, // 预留整形参数1接口
- int nReversed2, // 预留整形参数2接口
- int nReversed3, // 预留整形参数3接口
- int nReversed4, // 预留整形参数4接口
- int nReversed5, // 预留整形参数5接口
- float fReversed1, // 预留float参数1接口
- float fReversed2, // 预留float参数2接口
- float fReversed3, // 预留float参数3接口
- char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
- char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
- char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
- char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
- char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
- )
- {
- LOG4C((LOG_NOTICE,"CtrlMsg = %s",szMsg));
- int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0;
- char szSendMsg[DAIKIN_SEND_MSG];
- char szType[TYPE_LENGTH] = {0};
- char szCid2[CID2_LENGTH] = {0};
- memset(szSendMsg, 0, DAIKIN_SEND_MSG);
- GetFromIni(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit);
- int iSendLen = 0;
- int iLen = sizeof(REQUESTSET_STRCUT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- BYTE byArySend[120] = {0};
- byArySend[0] = 0x7E;
- byArySend[1] = 0x32;
- byArySend[2] = 0x30;
- byArySend[3] = ByteToAscii((nAddr>>4) & 0x0f);
- byArySend[4] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- byArySend[5] = 0x36;
- byArySend[6] = 0x30;
- memcpy(byArySend + 7, szCid2, 2);
- if ( (strcmp(szCmd, "cmd-9") == 0)) // 开关机;
- {
- // lengchck;
- byArySend[9] = 0x45;
- byArySend[10] = 0x30;
- byArySend[11] = 0x30;
- byArySend[12] = 0x32;
- int iCtrl = atoi(szMsg);
- memset(szMsg,0,strlen(szMsg));
- itoa(iCtrl,szMsg,16);
- for (int i(0); i < 2 ; i++)
- lowcase2uppcase((BYTE &)szMsg[i]);
- memcpy(byArySend + 13, szMsg, 2);
- GetCheckSum((char *)byArySend + 1, chChkSum, 14 );
- memcpy(byArySend + 15, chChkSum, 4);
- byArySend[19] = 0x0D;
- iSendLen = 20;
- LOG4C((LOG_NOTICE,"cmd-9=空调开关控制:%s",szMsg));
- }
- else if ( (strcmp(szCmd, "cmd-10") == 0)) // 设置风量;
- {
- int iRealLen = strlen(szMsg);
- if ( iRealLen != 4)
- {
- LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg));
- return -1;
- }
- // lengchck;
- byArySend[9] = 0x38;
- byArySend[10] = 0x30;
- byArySend[11] = 0x30;
- byArySend[12] = 0x38;
- // STX;
- byArySend[13] = 0x36;
- byArySend[14] = 0x34;
- byArySend[15] = 0x30;
- // 接口卡LINEID;
- byArySend[16] = nAddr -1 + 48;
- // 设置值;
- memcpy(byArySend + 17, szMsg, 4);
- GetCheckSum((char *)byArySend + 1, chChkSum, 20 );
- memcpy(byArySend + 21, chChkSum, 4);
- // EOL;
- byArySend[25] = 0x0D;
- iSendLen = 26;
- LOG4C((LOG_NOTICE,"cmd-10=空调风量设置:%s",szMsg));
- }
- else if ( (strcmp(szCmd, "cmd-11") == 0)) // 设置温度;
- {
- byArySend[9] = 0x36;
- byArySend[10] = 0x30;
- byArySend[11] = 0x30;
- byArySend[12] = 0x41;
- byArySend[13] = 0x36;
- byArySend[14] = 0x33;
- byArySend[15] = 0x30;
- byArySend[16] = nAddr -1 + 48;
- char szTemp[10] = {0};
- int iRealLen = strlen(szMsg);
- memcpy(szTemp,szMsg,iRealLen);
- if (iRealLen > 6)
- {
- LOG4C((LOG_NOTICE,"非法指令长度:%s",szMsg));
- return -1;
- }
- if (iRealLen == 5)
- {
- memcpy(szMsg + 1, szTemp,iRealLen);
- szMsg[0] = 48;
- }else if (iRealLen == 4)
- {
- memcpy(szMsg + 2, szTemp,iRealLen);
- szMsg[0] = 48;
- szMsg[1] = 48;
- }
- else if ( iRealLen == 3)
- {
- memcpy(szMsg + 3, szTemp,iRealLen);
- szMsg[0] = 48;
- szMsg[1] = 48;
- szMsg[2] = 48;
- }
- else if ( iRealLen == 2)
- {
- memcpy(szMsg + 4, szTemp,iRealLen);
- szMsg[0] = 48;
- szMsg[1] = 48;
- szMsg[2] = 48;
- szMsg[3] = 48;
- }
- else if ( iRealLen == 1)
- {
- memcpy(szMsg + 5, szTemp,iRealLen);
- szMsg[0] = 48;
- szMsg[1] = 48;
- szMsg[2] = 48;
- szMsg[3] = 48;
- szMsg[4] = 48;
- }
- else if ( iRealLen == 0)
- memcpy(szMsg , "000000", 6);
-
- memcpy(byArySend + 17, szMsg, 6);
- GetCheckSum((char *)byArySend + 1, chChkSum, 22 );
- memcpy(byArySend + 23, chChkSum, 4);
- byArySend[27] = 0x0D;
- iSendLen = 28;
- LOG4C((LOG_NOTICE,"cmd-11=空调温度设置:%s",szMsg));
- }else if ( strcmp(szCmd, "cmd-12") == 0) // 设置运转模式;
- {
- // lengchck;
- byArySend[9] = 0x42;
- byArySend[10] = 0x30;
- byArySend[11] = 0x30;
- byArySend[12] = 0x35;
- // STX;
- byArySend[13] = 0x36;
- byArySend[14] = 0x32;
- byArySend[15] = 0x30;
- // 接口卡LINEID; 范围:0~2
- byArySend[16] = nAddr -1 + 48;
- // 设置值; 范围:0~7
- memcpy(byArySend + 17, szMsg, 1);
- GetCheckSum((char *)byArySend + 1, chChkSum, 17 );
- memcpy(byArySend + 18, chChkSum, 4);
- // EOL;
- byArySend[22] = 0x0D;
- iSendLen = 23;
- LOG4C((LOG_NOTICE,"cmd-12=空调运转模式设置:%s",szMsg));
- }
- memcpy(szSendMsg, byArySend, sizeof(byArySend));
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- if( nAddr < 1 ) return -1;
- SetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType, iSendLen );
- return 0;
- }
- //读取配置文件
- int CDaiKin::GetFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szCid2[CID2_LENGTH], //发送Buffer
- char szType[TYPE_LENGTH],
- int &nIndex,
- int &nLen,
- int &nStaBit,
- int &nEndBit
- )
- {
- char szFile[MAX_PATH + 1] = "";
- memset(szType, 0, TYPE_LENGTH);
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);//只读config下面的ini
- GetPrivateProfileString(szCmd, "SendCmd", "", szCid2, CID2_LENGTH, szFile);//读取配置文件中的一段字符串
- GetPrivateProfileString(szCmd, "Type", "", szType, TYPE_LENGTH, szFile);
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);//从配置文件中取值
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- nStaBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
- nEndBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
- return 0;
- }
- int CDaiKin::RequestStatus( int nAddr,
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[DAIKIN_SEND_MSG] //发送Buffer
- )
- {
- int nRet = -1;
- #if DEBUG_DAIKIN
- int iLen = sizeof(REQUEST_STRUCT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- REQUEST_STRUCT RequestPara;
- memset( &RequestPara, 0, iLen );
- //起始位
- RequestPara.headMes.Start = 0x7E;
- //通讯协议版本
- RequestPara.headMes.Version[0] = 0x32;
- RequestPara.headMes.Version[1] = 0x30;
- //设备地址描述
- RequestPara.headMes.Address[0] = ByteToAscii((nAddr>>4) & 0x0f);
- RequestPara.headMes.Address[1] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- RequestPara.headMes.Cid1[0] = 0x36;
- RequestPara.headMes.Cid1[1] = 0x30;
- //Cid2
- RequestPara.headMes.Cid2[0] = ByteToAscii(chSendMsg[0] & 0x0f);
- RequestPara.headMes.Cid2[1] = ByteToAscii(chSendMsg[1] & 0x0f);
- RequestPara.headMes.Lenth[0] = 0x30;
- RequestPara.headMes.Lenth[1] = 0x30;
- RequestPara.headMes.Lenth[2] = 0x30;
- RequestPara.headMes.Lenth[3] = 0x30;
- //校验码
- BYTE *pDataBuf = new BYTE[ iLen - sizeof(RequestPara.chkSum) - 1 ];
- memset(pDataBuf, 0, iLen - sizeof(RequestPara.chkSum) - 1 );
- memcpy(pDataBuf, &RequestPara, iLen - sizeof(RequestPara.chkSum) - 1 );
- GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.chkSum) - 2 );
- RequestPara.chkSum[0] = chChkSum[0];
- RequestPara.chkSum[1] = chChkSum[1];
- RequestPara.chkSum[2] = chChkSum[2];
- RequestPara.chkSum[3] = chChkSum[3];
- //结束符
- RequestPara.End = 0x0D;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- int nDataLen = (int)sizeof(RequestPara);
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- if( nResult == iLen )
- {
- }
- else
- {
- delete []pDataBuf;
- SetEvent( m_hSemComm );
- return EER_CODE_DAIKIN_COM_WRITE_DATA;
- }
- }
- else
- {
- delete []pDataBuf;
- return ERR_CODE_DAIKIN_COM_BUSY;
- }
- delete[]pDataBuf;
-
- #endif
- return 0;
- }
- int CDaiKin::ResponseStatus(
- int nAddr, // 设备地址
- CCommProcess *pComm, //串口对象指针
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- #if DEBUG_DAIKIN
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT structResponse;
- memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) );
- nReceiveLen = sizeof(RESPONSE_STRUCT);
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
-
- nReadLen = pComm->Read((BYTE *)pBuffer,nReceiveLen);
- //LOG4C((LOG_NOTICE, " nLen= %d, nReadLen = %d", nLen, nReadLen));
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- //TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE, "串口没有读到数据"));
- return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
- }
- if (!ChkSumCheck(pBuffer, nReadLen))
- {
- TRACE("校验码校验出错");
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE, "校验码校验出错"));
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck(pBuffer) != 0)
- {
- SetEvent(m_hSemComm);
- TRACE("RTN校验出错");
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE, "RTN校验出错"));
- return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
- }
- SetDaKin_42VarMsg(nAddr, szCmd, pBuffer);
- SetDaKin_43VarMsg(nAddr, szCmd, pBuffer);
- SetDaKin_44VarMsg(nAddr, szCmd, pBuffer);
- //SetDaKin_45VarMsg(nAddr, szCmd, pBuffer);
- //SetDaKin_47VarMsg(nAddr, szCmd, pBuffer);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- int CDaiKin::WriteRequestStatus( int nAddr,
- char *szCmd, // 命令
- CCommProcess *pComm, //串口对象指针
- char *chSendMsg , //发送Buffer
- int &iSendLen
- )
- {
- #if DEBUG_DAIKIN
- ResetEvent( m_hSemComm );
- // 读取上次未读的数据,确保空调缓存已无数据;
- BYTE byRecv[250] = {0};
- pComm->Read(byRecv, 250);
- Sleep(1000);
- int nResult = pComm->Write((unsigned char *)chSendMsg, iSendLen);
- LOG4C((LOG_NOTICE,"%s %d",chSendMsg,iSendLen));
- if( nResult == iSendLen )
- {
- }
- else
- {
- SetEvent( m_hSemComm );
- return EER_CODE_DAIKIN_COM_WRITE_DATA;
- }
- Sleep(300);
- #endif
- return 0;
- }
- int CDaiKin::WriteResponseStatus( int nAddr, // 设备地址
- CCommProcess *pComm, //串口对象指针
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- #if DEBUG_DAIKIN
- int nReceiveLen = 250;
- int nProcessLen = 0;
- int nReadLen = 0;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- Sleep(200);
-
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- LOG4C((LOG_NOTICE,"写 nReadLen = %d, nReceiveLen = %d",nReadLen, nReceiveLen));
- if( nReadLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE,"写 串口没有读到数据"));
- return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
- }
- #if 0
- //int nTempLen = strlen(pBuffer);
- //CString str;
- //str.Format("%d", nTempLen);
- //AfxMessageBox(str);
- if (!ChkSumCheck(pBuffer, strlen(pBuffer)))
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE,"写 校检错误"));
- return ERR_CODE_DAIKIN_COM_CHKSUM_LOST;
- }
- if (!CheckLength(pBuffer))
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE,"写 数据长度校检错误"));
- return ERR_CODE_DAIKIN_COM_VARLEN;
- }
- int nRet = RtnCheck(pBuffer);
- if(0 != nRet)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- LOG4C((LOG_NOTICE,"写 返回码出错"));
- return nRet;
- }
- #endif
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- void CDaiKin::SimulationCommData(int nAddr)
- {
- }
- WORD CDaiKin::RtnCheck(char Msg[VAR_MSG])
- {
- int len ,index;
- len = 2;
- index = 7;
- char ch[2];
- ch[0] = Msg[index];
- ch[1] = Msg[index+1];
- if (atoi(ch)==ERR_CID_RTN_VAR)
- {
- return ERR_CID_RTN_VAR;
- }
- else if (atoi(ch)==ERR_CID_RTN_CHKSUM)
- {
- return ERR_CID_RTN_CHKSUM;
- }
- else if (atoi(ch)==ERR_CID_RTN_LCHKSUM)
- {
- return ERR_CID_RTN_LCHKSUM;
- }
- else if (atoi(ch)==ERR_CID_RTN_CID2)
- {
- return ERR_CID_RTN_CID2;
- }
- else if (atoi(ch)==ERR_CID_RTN_FORMAT)
- {
- return ERR_CID_RTN_FORMAT;
- }
- else if (atoi(ch)==ERR_CID_RTN_INVALI_DATA)
- {
- return ERR_CID_RTN_INVALI_DATA;
- }
- else /*if (atoi(ch)==ERR_CID_RTN_NORMAL)*/
- {
- return ERR_CID_RTN_NORMAL;
- }
- }
- void CDaiKin::lowcase2uppcase(BYTE &btSrc)
- {
- if( btSrc >= 'a' && btSrc <= 'z' )
- {
- btSrc = btSrc - 'a' + 'A';
- }
- }
- void CDaiKin::GetCheckSum(char *szData, char *szCheck ,int nlen /* = 12 */)
- {
- DWORD dwSum(0);
- nlen = strlen(szData);
- for (int i = 0; i < nlen; i++)
- {
- dwSum += szData[i] ;
- }
- WORD iCompliment = dwSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- itoa(iCompliment, szCheck, 16);
- for (int i(0); i < 5 ; i++)
- {
- lowcase2uppcase((BYTE &)szCheck[i]);
- }
- }
- #if 0
- UINT CDaiKin::GetCheckSum(char *pBuf, char chDest[5], int len)
- {
- WORD iSum = 0;
- //unsigned char chCompliment[2] = {0};
- for(int i=0; i<len; i++)//求和
- {
- //TRACE("%x\r\n", pBuf[i]);
- iSum += pBuf[i];
- }
- WORD iCompliment = iSum;
- iCompliment = ~iCompliment;//取反
- iCompliment++;
- //DigitToBinary(iCompliment, chDest, sizeof(chDest));
- itoa(iCompliment, chDest, 16);
- chDest[0] = lowercase2uppercase(chDest[0]);
- chDest[1] = lowercase2uppercase(chDest[1]);
- chDest[2] = lowercase2uppercase(chDest[2]);
- chDest[3] = lowercase2uppercase(chDest[3]);
- return atoi(chDest);
- }
- #endif
|