| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163 |
- // 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]));
- memset(m_szDaKin_400Msg[i], 0, sizeof(m_szDaKin_400Msg[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::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;
- int iCmd = atoi(szCmd+4);
- if( (strlen(m_szDaKin_400Msg[nAddr - 1]) == 0) && (iCmd >=13 && iCmd <= 23) ) //获取系统参数(定点数)
- {
- 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_400VarMsg(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::GetDaiKin_400VarMsg( int nAddr,char *szCmd,char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szDaKin_400Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- return 0;
- }
- 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, CCommProcess *pComm,char *chSendMsg )
- //{
- // 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::RequestStatus( int nAddr, char *szCmd, CCommProcess *pComm,char *chSendMsg )
- {
- int nRet = -1;
- #if DEBUG_DAIKIN
- char chChkSum[5] = {0};
- BYTE byArySend[60] = {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;
- // Cid2;
- byArySend[7] = 0x45;
- byArySend[8] = 0x30;
- // LENGTH;
- byArySend[9] = 0x41;
- byArySend[10] = 0x30;
- byArySend[11] = 0x30;
- byArySend[12] = 0x36;
- // STX;
- byArySend[13] = 0x34;
- byArySend[14] = 0x30;
- byArySend[15] = 0x30;
- // LINE No; 范围:0~2
- byArySend[16] = nAddr -1 + 48;
- // Unit No;
- byArySend[17] = 0x30;
- byArySend[18] = 0x30;
- // check;
- GetCheckSum((char*)byArySend + 1, chChkSum, 18);
- memcpy(byArySend + 19, chChkSum, 4);
- // End;
- byArySend[23] = 0x0D;
- int iSendLen = 24;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((BYTE *)&byArySend, iSendLen);
- if( nResult == iSendLen )
- {
- SetEvent( m_hSemComm );
- LOG4C((LOG_NOTICE,"实际写长度不等"));
- return EER_CODE_DAIKIN_COM_WRITE_DATA;
- }
- }
- else
- {
- LOG4C((LOG_NOTICE,"无信号写"));
- return ERR_CODE_DAIKIN_COM_BUSY;
- }
- #endif
- return 0;
- }
- int CDaiKin::ResponseStatus(int nAddr,CCommProcess *pComm,char *szCmd, char *szMsg, int &nIndex,int &nLen,char *szType)
- {
- #if DEBUG_DAIKIN
- int iSendLen = 90;
- int iRecvLen = 0;
- BYTE *byAryRecv = new BYTE[iSendLen];
- ZeroMemory(byAryRecv,iSendLen);
- iRecvLen = pComm->Read(byAryRecv,iSendLen);
- if( iRecvLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete[]byAryRecv;
- byAryRecv = NULL;
- }
- LOG4C((LOG_NOTICE, "串口没有读到数据"));
- return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
- }
- if (!ChkSumCheck( (char*)byAryRecv, iRecvLen))
- {
- SetEvent(m_hSemComm);
- if (byAryRecv != NULL)
- {
- delete []byAryRecv;
- byAryRecv = NULL;
- }
- LOG4C((LOG_NOTICE, "校验码校验出错"));
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck( (char*)byAryRecv) != 0)
- {
- SetEvent(m_hSemComm);
- if (byAryRecv != NULL)
- {
- delete []byAryRecv;
- byAryRecv = NULL;
- }
- LOG4C((LOG_NOTICE, "RTN校验出错"));
- return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
- }
- memcpy(m_szDaKin_400Msg, byAryRecv, iRecvLen);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- SetEvent( m_hSemComm );
- if( byAryRecv != NULL)
- {
- delete []byAryRecv;
- byAryRecv = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- //int CDaiKin::ResponseStatus(int nAddr,CCommProcess *pComm,char *szCmd, char *szMsg, int &nIndex,int &nLen,char *szType)
- //{
- //#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
|