| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879 |
- // Emerson_CM.cpp: implementation of the Emerson_CM class.
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: Emerson_CM.cpp //////
- ////// 作 者: Wangjianfeng //////
- ////// 创建时间: //////
- ////// 说 明: EmersonCM+协议 //////
- ////// //////
- ////// 修改时间:2010-04-08 //////
- ////// 修改说明:数据转换 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include "Global.h"
- #include "Emerson_CM.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- Emerson_CM::Emerson_CM(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_emerson_ControlMsg[i], 0, sizeof(m_emerson_ControlMsg[i]));
- memset(m_emerson_QeuryMsg[i], 0, sizeof(m_emerson_QeuryMsg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- Emerson_CM::~Emerson_CM()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL Emerson_CM::AirEmersonOpenComm(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 Emerson_CM::SendReadRequest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szRecvMsg[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 LBCount = 0;
- int SendCmd = 0;
- int Function = 0;
- int Index = 0;
- int nLen = 0;
- char Dtype[10] = {0};
- int startbit =0;
- int endbit = 0;
- GetFromIni(szPath, szIniName, szCmd, SendCmd,Function, LBCount,Index,nLen,Dtype,startbit,endbit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- if( nAddr < 1 ) return -1;
- int nRet = -1;
- /*if( strlen((char *)m_emerson_QeuryMsg) == 0 &&
- (
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0
- )
- ||*/
- if((strlen((char *)m_emerson_QeuryMsg[nAddr-1]) == 0 &&
- (
- strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 || strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 || strcmp(szCmd, "cmd-21") == 0 ||
- strcmp(szCmd, "cmd-22") == 0 || strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-24") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 || strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 || strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 || strcmp(szCmd, "cmd-33") == 0 ||
- strcmp(szCmd, "cmd-34") == 0 || strcmp(szCmd, "cmd-35") == 0 ||
- strcmp(szCmd, "cmd-36") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0
- ))
- ||(strcmp(szCmd, "cmd-4"))||strcmp(szCmd, "cmd-33")||strcmp(szCmd, "cmd-41")
- )
-
- {
- int nRet = GetDeviceParam( pComm,nAddr,szCmd,SendCmd,Function,LBCount,Index,nLen, Dtype,startbit,endbit,szRecvMsg);
- if( nRet != 0 ) return nRet;
- }
- if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = FALSE;
- //LOG4C((LOG_NOTICE , "m_devOnline[%d] = FALSE",nAddr-1));
- }
- else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 * 1000 && m_dwOnlineTick[nAddr - 1] > 0 )
- {
- m_devOnline[nAddr - 1] = TRUE;
- //LOG4C((LOG_NOTICE , "m_devOnline[%d] = TRUE",nAddr-1));
- }
- if( m_devOnline[nAddr - 1] == FALSE )
- {
- return -1;
- }
- nRet = GetEmersonEnquiryVarMsg(nAddr,szCmd, szRecvMsg, Index, nLen, Dtype,startbit,endbit);
- //LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d , startbit = %d, endbit = %d ,return nRet = %d", szCmd, szRecvMsg,Index,startbit,endbit, nRet));
- return nRet;
- }
- // 发送设置设备参数请求
- int Emerson_CM::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szRecvMsg[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 LBCount = 0;
- int SendCmd = 0;
- int Function = 0;
- int Index = 0;
- int nLen = 0;
- char Dtype[10] = {0};
- int startbit =0;
- int endbit = 0;
- GetFromIni(szPath, szIniName, szCmd, SendCmd,Function, LBCount,Index,nLen,Dtype,startbit,endbit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- if( strlen((char *)m_emerson_ControlMsg) == 0 ||
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0
- )
- {
- GetDeviceParam( pComm,nAddr,szCmd,SendCmd,Function,LBCount,Index,nLen, Dtype,startbit,endbit,szRecvMsg);
- }
- return 0;
- }
- int Emerson_CM::RequestReadStatus(CCommProcess *pComm,int SendCmd,int Function,int LBCount,int addr)
- {
- #if DEBUG_EMERSON
- int iLen = sizeof(REQUESTPARAM);
- REQUESTPARAM RequestPara;
- memset( &RequestPara, 0, iLen );
- RequestPara.UnitId = (addr - 1) ^ 0xf0;//
- RequestPara.ControlId = Function;
- RequestPara.Len = LBCount;
- RequestPara.Request = SendCmd;
- RequestPara.Chksum = (RequestPara.UnitId + RequestPara.ControlId + RequestPara.Len + RequestPara.Request) & 0xff;
- //WaitForSingObject();等待有信号;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- //ResetEvent()设置无信号状态
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((unsigned char *)&RequestPara, iLen);
- if( nResult != iLen )
- {
- //SetEvent设置有信号;
- SetEvent( m_hSemComm );
- //返回 错误值;
- return EER_CODE_AIREMERSONCM_COM_REGNUM;
- }
- }
- else
- {
- //如果m_hSemCom无信号,返回错误;
- return ERR_CODE_AIREMERSONCM_COM_BUSY;
- }
- #endif
- return 0;
- }
- int Emerson_CM::ResponseReadStatus( int nAddr,
- CCommProcess *pComm, // 串口对象
- char szCmd[MAX_CMD], // 命令
- int &SendCmd, // 控制码
- int &Function, // 功能码
- int &LBCount // 变量类型长度
- )
- {
- #if DEBUG_EMERSON
- int nReceiveLen = sizeof(RESPONSE_STRUCT);
- char *pBuffer = new char[nReceiveLen];
- memset(pBuffer, 0, nReceiveLen);
- int nReadLen = 0;
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);//pBuffer整串协议 数据
- if( nReadLen <= 0)
- {
- LOG4C((LOG_NOTICE, "EmersonCM串口没有读到数据!"));
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_AIREMERSONCM_COM_READ_NO_DATA;
- }
- #if 1
- if (!ChkSumCheck(pBuffer,SendCmd))
- {
- LOG4C((LOG_NOTICE, "EmersonCM ChkSumCheck Lost SendCmd = %d!", SendCmd));
- SetEvent(m_hSemComm);
- if (pBuffer !=NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_AIREMERSONCM_COM_LRC_LOST; //校验码校验出错
- }
- #endif
- //SetEmersonContrlVarMsg(szCmd, pBuffer);
- SetEmersonEnquiryVarMsg(nAddr,szCmd, pBuffer);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- int Emerson_CM::RequestWriteStatus( CCommProcess *pComm,int Function ,int SendCmd,int LBCount,int nAddr )
- {
- REQUESTPARAM requsetparam;
- int nDataLen = sizeof(REQUESTPARAM);
- memset(&requsetparam,0,nDataLen);
- requsetparam.UnitId = (nAddr-1) ^ 0xf0; //机组-in
- requsetparam.ControlId = SendCmd; //0x11//控制
- requsetparam.Len = LBCount; //后续数
- requsetparam.Request = Function; //szcmd//功能码-in
- requsetparam.Chksum = (requsetparam.UnitId + requsetparam.ControlId + requsetparam.Len + requsetparam.Request) & 0xff; //校验
- ResetEvent( m_hSemComm );//设置为无信号
- //WriteMessage写命令入串口,返回实际写入的字节数;
- int iResult = pComm->Write((BYTE *)&requsetparam, nDataLen);
- if (iResult == nDataLen)
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return 0;
- }
- else
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return EER_CODE_AIREMERSONCM_COM_WRITE_DATA;
- }
- }
- int Emerson_CM::ResponseWriteStatus(int nAddr,CCommProcess *pComm)
- {
- char chBuf[30] = {0};
- //读取请求写入的命令;
- int iReadLen = pComm->Read((BYTE *)chBuf, sizeof(chBuf));
- if( iReadLen <= 0) // 串口没有读到数据
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_AIREMERSONCM_COM_READ_NO_DATA;
- }
- SetEvent( m_hSemComm );
- if (chBuf[1] == 0x06)//ack字符 成功接收;
- {
- LOG4C((LOG_NOTICE,"设置成功"));
- return 0;
- }
- else
- return ERR_CODE_AIREMERSONCM_COM_INVALIDRES;
- }
- int Emerson_CM::GetFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //功能码
- int &SendCmd,
- int &Function,
- int &LBCount,
- int &Index,
- int &nLen,
- char Dtype[10],
- int &startbit,
- int &endbit
- )
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- TRACE("szFile =%s\n",szFile);
- SendCmd = GetPrivateProfileInt(szCmd,"SendCmd",0,szFile);
- Function = GetPrivateProfileInt(szCmd,"Function",0,szFile);
-
- LBCount = GetPrivateProfileInt(szCmd,"LBCount",0,szFile);
- Index = GetPrivateProfileInt(szCmd,"Index",0,szFile);
- nLen = GetPrivateProfileInt(szCmd,"nLen",0,szFile);
- DWORD ks = GetPrivateProfileString(szCmd, "Type" , "" , Dtype ,10, szFile);
- Dtype[strlen(Dtype)] = '\0';
- startbit = GetPrivateProfileInt(szCmd,"startbit",0,szFile);
- endbit = GetPrivateProfileInt(szCmd,"endbit",0,szFile);
- return 0;
- }
- int Emerson_CM::GetDeviceParam( CCommProcess *pComm, //串口对象指针
- int nAddr, //设备地址
- char szCmd[MAX_CMD], //功能码
- int &SendCmd, //
- int &Function, //
- int &LBCount, //
- int &Index, //
- int &nLen, //
- char Dtype[10], //
- int &startbit, //
- int &endbit, //
- char szRecvMsg[VAR_MSG] // 接收Buffer
- )
- {
- int nRet = -1;
- if(SendCmd == 0x05)
- {
- nRet = RequestReadStatus(pComm, SendCmd, Function, LBCount, nAddr);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseReadStatus( nAddr,pComm, szCmd,SendCmd,Function, LBCount);
- }
- else if (SendCmd = 0x11)
- {
- nRet = RequestWriteStatus(pComm, SendCmd, Function, LBCount, nAddr);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseWriteStatus(nAddr,pComm);
- }
-
- return nRet;
- }
- int Emerson_CM::GetEmersonEnquiryVarMsg(int nAddr, //address
- char szCmd[MAX_CMD], // 命令
- char szRecvMsg[VAR_MSG], // 接收Buffer
- int &Index, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szSendCmd[TYPE_LENGTH] ,
- int &startbit,
- int &endbit) // 变量数据类型
- {
- int nRet = 0;
- if(
- //strcmp(szCmd, "cmd-4") == 0 ||//无bit;
- //strcmp(szCmd, "cmd-5") == 0 || //无bit;
- strcmp(szCmd, "cmd-6") == 0 ||
- //strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 ||
- strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 ||
- strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 ||
- strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 ||
- //strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 ||
- strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 ||
- strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 ||
- //strcmp(szCmd, "cmd-21") == 0 || //冷水,4位;
- //strcmp(szCmd, "cmd-22") == 0 ||//无bit;
- //strcmp(szCmd, "cmd-23") == 0 ||//无bit;
- strcmp(szCmd, "cmd-24") == 0 ||//无bit;
- strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 ||
- strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 ||
- //strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 ||
- strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 ||
- strcmp(szCmd, "cmd-33") == 0 /*||*/
- //strcmp(szCmd, "cmd-34") == 0 ||
- //strcmp(szCmd, "cmd-35") == 0 ||
- //strcmp(szCmd, "cmd-36") == 0 ||
- //strcmp(szCmd, "cmd-37") == 0 ||
- //strcmp(szCmd, "cmd-38") == 0 ||
- //strcmp(szCmd, "cmd-39") == 0 //双bit;
- //strcmp(szCmd, "cmd-40") == 0 ||//无bit;
- //strcmp(szCmd, "cmd-41") == 0 ||//无bit;
- //strcmp(szCmd, "cmd-42") == 0//无bit;
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
- //LOG4C((LOG_NOTICE , "szRevcMsg = %s",szRecvMsg));
- GetBitStatu(szRecvMsg,startbit,endbit);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- if (
- strcmp(szCmd, "cmd-4") == 0 //温度设定
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
- int Temp = 17 + (szRecvMsg[0] & 0xf0)/16;
- sprintf(szRecvMsg,"%d",Temp);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- if (
- strcmp(szCmd, "cmd-5") == 0 //湿度设定
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
- int Hum = 42 + (szRecvMsg[0] & 0x0f) * 2;
- sprintf(szRecvMsg,"%d",Hum);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- if(
- strcmp(szCmd, "cmd-22") == 0 ||//BCD温度 无bit;
- strcmp(szCmd, "cmd-23") == 0 //BCD湿度 无bit;
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_emerson_QeuryMsg[nAddr-1] + Index,nLen);//命令对应的需要的数据szRecvMsg;
- BCDtofloat(szRecvMsg);
- //LOG4C((LOG_NOTICE, "BCDtofloat = %",szRecvMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- // if (
- // strcmp(szCmd, "cmd-40") == 0 ||//not bit SendCmd
- // strcmp(szCmd, "cmd-41") == 0 ||
- // strcmp(szCmd, "cmd-42") == 0
- // )
- // {
- //#if IS_USE_READMSG_CS
- // EnterCriticalSection( &m_csReadMsg );
- //#endif
- // memcpy(szRecvMsg, m_emerson_QeuryMsg + Index,nLen);//命令对应的需要的数据szRecvMsg;
- //#if IS_USE_READMSG_CS
- // LeaveCriticalSection(&m_csReadMsg);
- //#endif
- // }
- return nRet;
- }
- void Emerson_CM::SetEmersonEnquiryVarMsg( int nAddr,char szCmd[MAX_CMD], char *pBuffer)
- {
- if (strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-5") == 0 ||
- strcmp(szCmd, "cmd-6") == 0 ||
- //strcmp(szCmd, "cmd-7") == 0 ||
- strcmp(szCmd, "cmd-8") == 0 ||
- strcmp(szCmd, "cmd-9") == 0 ||
- strcmp(szCmd, "cmd-10") == 0 ||
- strcmp(szCmd, "cmd-11") == 0 ||
- strcmp(szCmd, "cmd-12") == 0 ||
- strcmp(szCmd, "cmd-13") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 ||
- //strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 ||
- strcmp(szCmd, "cmd-17") == 0 ||
- strcmp(szCmd, "cmd-18") == 0 ||
- strcmp(szCmd, "cmd-19") == 0 ||
- strcmp(szCmd, "cmd-20") == 0 ||
- //strcmp(szCmd, "cmd-21") == 0 ||
- strcmp(szCmd, "cmd-22") == 0 ||
- strcmp(szCmd, "cmd-23") == 0 ||
- strcmp(szCmd, "cmd-24") == 0 ||
- strcmp(szCmd, "cmd-25") == 0 ||
- strcmp(szCmd, "cmd-26") == 0 ||
- strcmp(szCmd, "cmd-27") == 0 ||
- strcmp(szCmd, "cmd-28") == 0 ||
- //strcmp(szCmd, "cmd-29") == 0 ||
- strcmp(szCmd, "cmd-30") == 0 ||
- strcmp(szCmd, "cmd-31") == 0 ||
- strcmp(szCmd, "cmd-32") == 0 ||
- strcmp(szCmd, "cmd-33") == 0 /*||
- strcmp(szCmd, "cmd-34") == 0 ||
- strcmp(szCmd, "cmd-35") == 0 ||
- strcmp(szCmd, "cmd-36") == 0 ||
- strcmp(szCmd, "cmd-37") == 0 ||
- strcmp(szCmd, "cmd-38") == 0 ||
- strcmp(szCmd, "cmd-39") == 0 ||
- strcmp(szCmd, "cmd-40") == 0 ||
- strcmp(szCmd, "cmd-41") == 0 */
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_emerson_QeuryMsg[nAddr-1], pBuffer, sizeof(m_emerson_QeuryMsg[nAddr-1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- BOOL Emerson_CM::ChkSumCheck(char szSendMsg[],int SendCmd/*,int nReadLen*/)
- {
- //int len = (int)strlen(szSendMsg);
- unsigned int nData = 0;
- int i = 0;
- unsigned int nSumCrc = (unsigned int)szSendMsg[28];
- switch (SendCmd)
- {
- case 0x05:
- for (i = 0;i < 28;i++)
- {
- nData += szSendMsg[i];
- }
- nData = nData & 0x00ff;
- if ( (char)nData != szSendMsg[i] )
- {
- //LOG4C((LOG_NOTICE, "nData=%d, szSendMsg[28]=%d, sumcrc = %d", nData, (unsigned int)szSendMsg[28], nSumCrc));
- LOG4C_HEX_DUMP((LOG_NOTICE, szSendMsg, 29));
- return FALSE;
- }
- break;
- case 0x11:
- for(i = 0;i < 4;i++)
- {
- nData +=szSendMsg[i];
- }
- nData = nData & 0x00ff;
- if ((unsigned int)nData != (unsigned int)szSendMsg[i])
- {
- return FALSE;
- }
- break;
- default:
- break;
- }
- return TRUE;
- }
- void Emerson_CM:: GetBitStatu( char *bytValue,int startbit,int endbit)
- {
- int ByteBit = 0;
- char tempbyte[9] = {0};
- //int Len = sizeof(bytValue);
- if (startbit == endbit)
- {
- switch(startbit)
- {
- /*
- case 7:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 0:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }*/
- case 0:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }
- _snprintf(bytValue,sizeof(bytValue),"%d",ByteBit);
- //itoa(ByteBit,&bytValue,10);
- }
- else
- {
- int tempbit = startbit;
- int TiLen = endbit - startbit;
- for (int i = 0; i < TiLen;i++)
- {
- tempbit = tempbit + i;
- switch(tempbit)
- {
- /*case 7:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 0:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }*/
- case 0:
- ByteBit = bytValue[0] & 0x01;
- break;
- case 1:
- ByteBit = (bytValue[0] & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue[0] & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue[0] & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue[0] & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue[0] & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue[0] & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue[0] & 0x80) / 0x80;
- break;
- }
- _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit);
- //itoa(ByteBit,&bytValue,10);
- }
- bytValue = tempbyte;
- }
- }
- void Emerson_CM::BCDtofloat(char szRecvbuffer[VAR_MSG])
- {
- int Lvalue;
- char BufferLd[20] = {0};
- char BufferHd[20] = {0};
- //int Len = sizeof(szRecvbuffer)-1;
- BYTE TempChar[2] = {0};
- TempChar[0] = szRecvbuffer[0];
- TempChar[1] = szRecvbuffer[1];
- CString Hstr,Lstr,Sumstr;
- Hstr.Format("%0x",TempChar[0]);
- if (TempChar[1] < 10)
- {
- Lstr.Format("0%0x",TempChar[1]);
- }
- else
- Lstr.Format("%0x",TempChar[1]);
- Sumstr = Hstr + Lstr;
- _snprintf(szRecvbuffer, sizeof(szRecvbuffer), "%s",Sumstr);
- }
- void Emerson_CM::SimulationCommData()
- {
- //
- //LOG4C((LOG_NOTICE, "模拟数据---------"));
- m_emerson_QeuryMsg[0][0] =0xF0;
- m_emerson_QeuryMsg[0][1] =0x06;
- m_emerson_QeuryMsg[0][2] =0x1A;
- m_emerson_QeuryMsg[0][3] =0x00;
- m_emerson_QeuryMsg[0][4] =0x02;
- m_emerson_QeuryMsg[0][5] =0x09;
- m_emerson_QeuryMsg[0][6] =0x04;
- m_emerson_QeuryMsg[0][7] =0x49;
- m_emerson_QeuryMsg[0][8] =0xFF;
- m_emerson_QeuryMsg[0][9] =0xF3;
- m_emerson_QeuryMsg[0][10]=0xFF;
- m_emerson_QeuryMsg[0][11]=0x44;
- m_emerson_QeuryMsg[0][12]=0x00;
- m_emerson_QeuryMsg[0][13]=0x08;
- m_emerson_QeuryMsg[0][14]=0x00;
- m_emerson_QeuryMsg[0][15]=0x00;
- m_emerson_QeuryMsg[0][16]=0x63;
- m_emerson_QeuryMsg[0][17]=0x08;
- m_emerson_QeuryMsg[0][18]=0xf0;
- m_emerson_QeuryMsg[0][19]=0x14;
- m_emerson_QeuryMsg[0][20]=0x01;
- m_emerson_QeuryMsg[0][21]=0x01;
- m_emerson_QeuryMsg[0][22]=0x08;
- m_emerson_QeuryMsg[0][23]=0x430;
- m_emerson_QeuryMsg[0][24]=0x0A;
- m_emerson_QeuryMsg[0][25]=0x4B;
- m_emerson_QeuryMsg[0][26]=0x10;
- m_emerson_QeuryMsg[0][27]=0x05;
- m_emerson_QeuryMsg[0][28]=0xDB;
- }
|