| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- // 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);
- 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 nRet = -1;
- nRet = WriteRequestStatus(nAddr, szCmd, pComm, szSendMsg);
- if( nRet != 0 )
- {
- 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接口
- )
- {
- int nIndex = 0, nLen = 0, StaBit = 0, EndBit = 0;
- char szSendMsg[DAIKIN_SEND_MSG];
- char szType[TYPE_LENGTH] = {0};
- char szCid2[CID2_LENGTH] = {0};
- char szAddr[2] = {0};
- char szLength[4] = {0};
- REQUESTSET_STRCUT szSend = {0};
- memset(szSendMsg, 0, DAIKIN_SEND_MSG);
- GetFromIni(szPath, szIniName, szCmd, szCid2, szType, nIndex, nLen, StaBit, EndBit);
- int iLen = sizeof(REQUESTSET_STRCUT);
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- REQUESTSET_STRCUT RequestPara;
- memset( &RequestPara, 0, iLen );
- //起始位
- RequestPara.Sol = 0x7E;
- //通讯协议版本
- RequestPara.Ver[0] = 0x32;
- RequestPara.Ver[1] = 0x30;
- //设备地址描述
- RequestPara.Adr[0] = ByteToAscii((nAddr>>4) & 0x0f);
- RequestPara.Adr[1] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- RequestPara.Cid1[0] = 0x36;
- RequestPara.Cid1[1] = 0x30;
- //Cid2
- RequestPara.Cid2[0] = ByteToAscii(szCid2[0] & 0x0f);
- RequestPara.Cid2[1] = ByteToAscii(szCid2[1] & 0x0f);
- RequestPara.Length[0] = 0x45;
- RequestPara.Length[1] = 0x30;
- RequestPara.Length[2] = 0x30;
- RequestPara.Length[3] = 0x30;
- memcpy(RequestPara.szInfo, szMsg, sizeof(RequestPara.szInfo));
- memset(RequestPara.Chksum, 0, sizeof(RequestPara.Chksum));
- //校验码
- 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.Eol = 0x0D;
- memcpy(szSendMsg, &RequestPara, sizeof(REQUESTSET_STRCUT));
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( nAddr < 1 ) return -1;
- //LOG4C((LOG_NOTICE, "CDaiKin::SendSetReuest szSendMsg= %s, cmd = %s", szSendMsg, szCmd));
- if( (strcmp(szCmd, "cmd-9") == 0))
- {
- SetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
- }
- 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, nLen);
- nReadLen = pComm->Read((BYTE *)pBuffer,nLen);
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_DAIKIN_COM_READ_NO_DATA;
- }
- else if( nReadLen < nLen )
- {
- TRACE("长度没有收够,断续接收,止到收完为止!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer )
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_DAIKIN_COM_FAULT;
- }
- else if( nReadLen > nLen )
- {
- // 完全代码,不一定能执行到
- TRACE("接收的长度超时所需的长度,认为是非法包,扔掉\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_DAIKIN_COM_READ_LEN_OVER;
- }
- if (!ChkSumCheck(pBuffer, nReadLen))
- {
- TRACE("校验码校验出错");
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
- }
- if (RtnCheck(pBuffer) != 0)
- {
- SetEvent(m_hSemComm);
- TRACE("RTN校验出错");
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- 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[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[DAIKIN_SEND_MSG] //发送Buffer
- )
- {
- #if DEBUG_DAIKIN
-
- int nDataLen = (int)strlen(chSendMsg);
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((unsigned char *)chSendMsg, nDataLen);
- if( nResult == nDataLen )
- {
- }
- else
- {
- SetEvent( m_hSemComm );
- return EER_CODE_DAIKIN_COM_WRITE_DATA;
- }
- #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 = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- RESPONSE_STRUCT structResponse;
- memset( &structResponse, 0, sizeof(RESPONSE_STRUCT) );
- nReceiveLen = sizeof(RESPONSE_STRUCT) - sizeof(structResponse);
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- //TRACE("串口没有读到数据!\r\n");
- //AfxMessageBox("串口没有读到数据!");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- 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)))
- {
- // 校检错误
- //AfxMessageBox("校检错误");
- //TRACE("校检错误!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_DAIKIN_COM_CHKSUM_LOST;
- }
- if (!CheckLength(pBuffer))
- {
- // 数据长度校检错误
- //TRACE("数据长度校检错误!\r\n");
- //AfxMessageBox("数据长度校检错误!");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_DAIKIN_COM_VARLEN;
- }
- int nRet = RtnCheck(pBuffer);
- if(0 != nRet)
- {
- //AfxMessageBox("校验出错");
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return nRet;
- }
- #endif
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData(nAddr);
- #endif
- return 0;
- }
- void CDaiKin::SimulationCommData(int nAddr)
- {
- m_szDaKin_42Msg[nAddr - 1][0] = 0x7E;
- m_szDaKin_42Msg[nAddr - 1][1] = 0x32;
- m_szDaKin_42Msg[nAddr - 1][2] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][3] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][4] = 0x31;
- m_szDaKin_42Msg[nAddr - 1][5] = 0x36;
- m_szDaKin_42Msg[nAddr - 1][6] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][7] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][8] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][9] = 0x39;
- m_szDaKin_42Msg[nAddr - 1][10] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][11] = 0x33;
- m_szDaKin_42Msg[nAddr - 1][12] = 0x34;
- m_szDaKin_42Msg[nAddr - 1][13] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][14] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][15] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][16] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][17] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][18] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][19] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][20] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][21] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][22] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][23] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][24] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][25] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][26] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][27] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][28] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][29] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][30] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][31] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][32] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][33] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][34] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][35] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][36] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][37] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][38] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][39] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][40] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][41] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][42] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][43] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][44] = 0x38;
- m_szDaKin_42Msg[nAddr - 1][45] = 0x44;
- m_szDaKin_42Msg[nAddr - 1][46] = 0x31;
- m_szDaKin_42Msg[nAddr - 1][47] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][48] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][49] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][50] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][51] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][52] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][53] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][54] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][55] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][56] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][57] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][58] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][59] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][60] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][61] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][62] = 0x20;
- m_szDaKin_42Msg[nAddr - 1][63] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][64] = 0x30;
- m_szDaKin_42Msg[nAddr - 1][65] = 0x46;
- m_szDaKin_42Msg[nAddr - 1][66] = 0x36;
- m_szDaKin_42Msg[nAddr - 1][67] = 0x38;
- m_szDaKin_42Msg[nAddr - 1][68] = 0x41;
- m_szDaKin_42Msg[nAddr - 1][69] = 0x0D;
- m_szDaKin_43Msg[nAddr - 1][0] = 0x7E;
- m_szDaKin_43Msg[nAddr - 1][1] = 0x32;
- m_szDaKin_43Msg[nAddr - 1][2] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][3] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][4] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][5] = 0x36;
- m_szDaKin_43Msg[nAddr - 1][6] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][7] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][8] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][9] = 0x37;
- m_szDaKin_43Msg[nAddr - 1][10] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][11] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][12] = 0x38;
- m_szDaKin_43Msg[nAddr - 1][13] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][14] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][15] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][16] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][17] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][18] = 0x39;
- m_szDaKin_43Msg[nAddr - 1][19] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][20] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][21] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][22] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][23] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][24] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][25] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][26] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][27] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][28] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][29] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][30] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][31] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][32] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][33] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][34] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][35] = 0x30;
- m_szDaKin_43Msg[nAddr - 1][36] = 0x32;
- m_szDaKin_43Msg[nAddr - 1][37] = 0x46;
- m_szDaKin_43Msg[nAddr - 1][38] = 0x39;
- m_szDaKin_43Msg[nAddr - 1][39] = 0x31;
- m_szDaKin_43Msg[nAddr - 1][40] = 0x38;
- m_szDaKin_43Msg[nAddr - 1][41] = 0x0D;
- m_szDaKin_44Msg[nAddr - 1][0] = 0x7E;
- m_szDaKin_44Msg[nAddr - 1][1] = 0x32;
- m_szDaKin_44Msg[nAddr - 1][2] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][3] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][4] = 0x31;
- m_szDaKin_44Msg[nAddr - 1][5] = 0x36;
- m_szDaKin_44Msg[nAddr - 1][6] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][7] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][8] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][9] = 0x33;
- m_szDaKin_44Msg[nAddr - 1][10] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][11] = 0x31;
- m_szDaKin_44Msg[nAddr - 1][12] = 0x43;
- m_szDaKin_44Msg[nAddr - 1][13] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][14] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][15] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][16] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][17] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][18] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][19] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][20] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][21] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][22] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][23] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][24] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][25] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][26] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][27] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][28] = 0x32;
- m_szDaKin_44Msg[nAddr - 1][29] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][30] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][31] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][32] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][33] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][34] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][35] = 0x20;
- m_szDaKin_44Msg[nAddr - 1][36] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][37] = 0x31;
- m_szDaKin_44Msg[nAddr - 1][38] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][39] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][40] = 0x30;
- m_szDaKin_44Msg[nAddr - 1][41] = 0x46;
- m_szDaKin_44Msg[nAddr - 1][42] = 0x39;
- m_szDaKin_44Msg[nAddr - 1][43] = 0x39;
- m_szDaKin_44Msg[nAddr - 1][44] = 0x44;
- m_szDaKin_44Msg[nAddr - 1][45] = 0x0D;
- }
- 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;
- }
- }
- 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);
- }
|