| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: Tmaster.cpp //////
- ////// 作 者: Wangjianfeng //////
- ////// 创建时间: //////
- ////// 说 明: Tmaster约顿空调协议 //////
- ////// //////
- ////// 修改时间:2011-07-23 //////
- ////// 修改说明:数据转换 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommProcess.h"
- #include "Global.h"
- #include ".\tmaster.h"
- CTmaster::CTmaster(
- 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_szE0Msg[i], 0, sizeof(m_szE0Msg[i]));
- memset(m_szSend49Msg[i], 0, sizeof(m_szSend49Msg[i]));
- memset(m_szSend45Msg[i], 0, sizeof(m_szSend45Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CTmaster::~CTmaster()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- int CTmaster::GetULFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szSendMsg[MAX_CMD], //发送Buffer
- char nType[MAX_CMD],
- int &nIndex,
- int &nLen,
- int &startbit,
- int &endbit,
- char szLength[5],
- int &nCDT)
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- GetPrivateProfileString(szCmd, "SendCmd", "", szSendMsg, 10, szFile);
- szSendMsg[strlen(szSendMsg)] = '\0';
- GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
- nType[strlen(nType)] = '\0';
- nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
- startbit = GetPrivateProfileInt(szCmd, "startbit", 0, szFile);
- endbit = GetPrivateProfileInt(szCmd, "endbit", 0, szFile);
- GetPrivateProfileString(szCmd, "LENGTH", "", szLength, 10, szFile);
- szLength[strlen(szLength)] = '\0';
- nCDT = GetPrivateProfileInt(szCmd, "CDT", 0, szFile);
- return 0;
- }
- BOOL CTmaster::AirTmasterOpenComm(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 CTmaster::SendReadRequest(
- char szPath[MAX_PATH],
- char szIniName[MAX_PATH],
- int nCommPort,
- int nAddr,
- char szCmd[MAX_CMD],
- char szMsg[VAR_MSG],
- int nReversed1,
- int nReversed2,
- int nReversed3,
- int nReversed4,
- int nReversed5,
- float fReversed1,
- float fReversed2,
- float fReversed3,
- char szReversed1[MAX_RESERVED1],
- char szReversed2[MAX_RESERVED2],
- char szReversed3[MAX_RESERVED3],
- char szReversed4[MAX_RESERVED4],
- char szReversed5[MAX_RESERVED5]
- )
- {
- int nIndex = 0, nLen = 0;
- int startbit = 0,endbit = 0;
- char szSendMsg[_VAR_MSG] = {0};
- char szType[TYPE_LENGTH] = {0};
- char szLength[5] = {0};
- int nCDT = 0;
- GetULFromIni(szPath, szIniName, szCmd,szSendMsg, szType, nIndex, nLen,startbit,endbit,szLength,nCDT);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if (
- (strlen(m_szE0Msg[nAddr-1]) ==0 &&
- (
- strcmp(szCmd, "cmd-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 || 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-1") == 0
- )
- {
- nRet = GetDeviceParam( nAddr,pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType,startbit,endbit );
- 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 = GetE0VarMsg(szCmd, szMsg, nAddr, nIndex, nLen, szType,startbit,endbit);
- LOG4C((LOG_NOTICE, "szCmd = %s, szMsg = %s ,nIndex = %d , startbit = %d, endbit = %d ,return nRet = %d", szCmd, szMsg,nIndex,startbit,endbit, nRet));
- return nRet;
- }
- int CTmaster::GetDeviceParam(
- int nAddr,
- CCommProcess *pComm, // 串口对象指针
- char szSendMsg[MAX_INFO], // 发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szRecvMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH], // 变量数据类型
- int &startbit,
- int &endbit
- )
- {
- int nRet = -1;
- nRet = RequestReadStatus(nAddr,pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseReadStatus(pComm, szCmd, szRecvMsg, nAddr, nIndex, nLen, szType,startbit,endbit);
- return nRet;
- }
- int CTmaster::ResponseReadStatus(
- CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char szRecvMsg[VAR_MSG],
- int nAddr,
- int &nIndex,
- int &nLen,
- char szType[TYPE_LENGTH],
- int &startbit,
- int &endbit)
- {
- #if ___DEBUG
- REPONSEPARAM structResponse;
- memset( &structResponse, 0, sizeof(REPONSEPARAM) );
- int nReceiveLen = 0;
- nReceiveLen = sizeof(REPONSEPARAM);
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nLen);
- int nReadLen = 0;
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if (!CheckLength(pBuffer))
- {
- SetEvent(m_hSemComm);
- if (pBuffer !=NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_AIRTMASTER_COM_LRC_LOST; //长度校验出错
- }
- if (!ChkSumCheck(pBuffer,nReadLen))
- {
- SetEvent(m_hSemComm);
- if (pBuffer !=NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_AIRTMASTER_COM_LRC_LOST; //校验码校验出错
- }
- int nRet = RtnCheck(pBuffer, nReadLen);
- if (nRet != 0)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return nRet; //RTN校验出错
- }
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_AIRTMASTER_COM_READ_NO_DATA;
- }
- SetE0VarMsg( nAddr,szCmd, pBuffer);
- m_dwOnlineTick[nAddr-1] = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- LOG4C((LOG_NOTICE,"%s",m_szE0Msg[0]));
- #endif
- return 0;
- }
- int CTmaster::RequestReadStatus(int nAddr,CCommProcess *pComm,char chSendMsg[_VAR_MSG])
- {
- #if ___DEBUG
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- REQUESTPARAM RequestPara;
- int iLen = sizeof(RequestPara);
- memset( &RequestPara, 0, iLen );
- //起始位;
- RequestPara.RequestChild.bySoi = 0x7E;
- //通讯协议版本;
- RequestPara.RequestChild.byVer[0] = 0x32;
- RequestPara.RequestChild.byVer[1] = 0x30;
- //设备地址描述;
- RequestPara.RequestChild.byAdr[0] = ByteToAscii((nAddr >> 4) & 0x0f);
- RequestPara.RequestChild.byAdr[1] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- RequestPara.RequestChild.byCid1[0] = 0x36;
- RequestPara.RequestChild.byCid1[1] = 0x30;
- //Cid2
- memcpy(RequestPara.RequestChild.byCid2, chSendMsg, 2);
- RequestPara.RequestChild.byLength[0] = 0x30;
- RequestPara.RequestChild.byLength[1] = 0x30;
- RequestPara.RequestChild.byLength[2] = 0x30;
- RequestPara.RequestChild.byLength[3] = 0x30;
- int nLen = sizeof(RequestPara.byCheckSum) -1;
- //校验码;
- BYTE *pDataBuf = new BYTE[ iLen - nLen];
- memset(pDataBuf, 0, iLen -nLen );
- memcpy(pDataBuf, &RequestPara, iLen - nLen);
- GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.byCheckSum) - 2 );
- delete []pDataBuf;
- pDataBuf = NULL;
- RequestPara.byCheckSum[0] = chChkSum[0];
- RequestPara.byCheckSum[1] = chChkSum[1];
- RequestPara.byCheckSum[2] = chChkSum[2];
- RequestPara.byCheckSum[3] = chChkSum[3];
- //结束符
- RequestPara.byEoi = 0x0D;
- //WaitForSingObject();等待有信号;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- //求得数组字符实际长度;
- int nDataLen = (int)sizeof(RequestPara);
- //ResetEvent()设置无信号状态
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- if( nResult != nDataLen )
- {
- //SetEvent设置有信号;
- SetEvent( m_hSemComm );
- //返回 错误值;
- return EER_CODE_AIRTMASTER_COM_REGNUM;
- }
- }
- else
- {
- //如果m_hSemCom无信号,返回错误;
- return ERR_CODE_AIRTMASTER_COM_BUSY;
- }
- #endif
- return 0;
- }
- int CTmaster::GetE0VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int nAddr, int &nIndex, int &nLen, char szType[TYPE_LENGTH],int &startbit,int &endbit)
- {
- int nRet = 0;
- if( strcmp(szCmd, "cmd-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 || 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szE0Msg[nAddr-1] + nIndex, szRecvMsg, nLen,startbit,endbit );
-
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- // 发送设置设备参数请求
- int CTmaster::SendSetReuest(
- char szPath[MAX_PATH], // 程序所在路径
- char szIniName[MAX_PATH], // 配置文件名称
- int nCommPort, // 串行端口
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 请求命令
- char szSetMsg[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;
- int startbit = 0,endbit = 0;
- char szSendCmd[_VAR_MSG] = {0};
- char szType[TYPE_LENGTH] = {0};
- char szLength[5] = {0};
- int nCDT = 0;
- GetULFromIni(
- szPath,
- szIniName,
- szCmd,
- szSendCmd, //ini SendCmd命令;
- szType, //ini Type键值;
- nIndex,
- nLen,
- startbit,
- endbit,
- szLength, // ini LENGTH;
- nCDT); // int CDT;
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if (
- strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 || //-25空调开机;-26空调关机;
- 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
- )
- {
- nRet = SetDeviceParam( nAddr,pComm, szSendCmd, szCmd, szSetMsg/*输入设备的设置值*/, szLength, nCDT );
- 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;
- // }
- return nRet;
- }
- // 设置传入设备的值;
- int CTmaster::SetDeviceParam(
- int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendCmd[_VAR_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szSetMsg[VAR_MSG], // 接收Buffer
- char szLength[5], // 变量索引,针对接收Buffer而言
- int &nCDT // 变量长度
- )
- {
- LOG4C((LOG_NOTICE,"nAddr = %d,szSendCmd = %s,szCmd = %s,szSetMsg = %s,szLength = %s,nCDT = %d",nAddr,szSendCmd,szCmd,szSetMsg,szLength,nCDT));
- int nRet = -1;
- nRet = RequestWriteStatus(pComm, nAddr, szCmd, szSendCmd, szSetMsg, szLength, nCDT);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseWriteStatus(pComm, nAddr);
- return nRet;
- }
- // 写优先,设置事件为无信号;
- int CTmaster::RequestWriteStatus( CCommProcess *pComm,int nAddr,char szCmd[MAX_CMD],char szSendCmd[VAR_MSG] ,char szSetMsg[VAR_MSG],char szLength[5], int &nCDT)
- {
- char chLength[4] = {0};
- char chChkSum[5] = {0};
- char szSendBuf[100] = {0};
- #if 1
- int nDataLen = 0;
- szSendBuf[0] = 0x7E;
- szSendBuf[1] = 0x32;
- szSendBuf[2] = 0x30;
- szSendBuf[3] = ByteToAscii((nAddr >> 4) & 0x0f);
- szSendBuf[4] = ByteToAscii(nAddr & 0x0f);
- szSendBuf[5] = 0x36;
- szSendBuf[6] = 0x30;
- memcpy(&szSendBuf[7], szSendCmd, 2);
- memcpy(&szSendBuf[9],szLength,4);
- if (strcmp(szCmd,"cmd-25") == 0)
- {
- memcpy(&szSendBuf[13],szSetMsg,2);
- GetCheckSum((char *)szSendBuf + 1, chChkSum, 14 );
- memcpy(&szSendBuf[15],chChkSum,4);
- szSendBuf[19] = 0x0D;
- nDataLen = 20;
- }
- else
- {
- szSendBuf[13] = ByteToAscii((nCDT >> 4) & 0x0f);
- szSendBuf[14] = ByteToAscii(nCDT & 0x0f);
- memcpy(&szSendBuf[15],szSetMsg,4);
- GetCheckSum((char *)szSendBuf + 1, chChkSum, 17 );
- memcpy(&szSendBuf[19],chChkSum,4);
- szSendBuf[23] = 0x0D;
- nDataLen = 24;
- }
- LOG4C_HEX_DUMP((LOG_NOTICE, szSendBuf, nDataLen));
-
- ResetEvent( m_hSemComm );//设置为无信号(写设备优先级高);
- int iResult = pComm->Write((BYTE *)&szSendBuf, nDataLen);
- if (iResult == nDataLen)
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return 0;
- }
- else
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return EER_CODE_AIRTMASTER_COM_WRITE_DATA;
- }
- #endif
- #if 0
- WRITEPARAM RequestPara;
- int iLen = sizeof(RequestPara);
- memset( &RequestPara, 0, iLen );
- //起始位;
- RequestPara.ReposeChild.bySoi = 0x7E;
-
- //通讯协议版本;
- RequestPara.ReposeChild.byVer[0] = 0x32;
- RequestPara.ReposeChild.byVer[1] = 0x30;
-
- //设备地址描述;
- RequestPara.ReposeChild.byAdr[0] = ByteToAscii((nAddr >> 4) & 0x0f);
- RequestPara.ReposeChild.byAdr[1] = ByteToAscii(nAddr & 0x0f);
- //Cid1
- RequestPara.ReposeChild.byCid1[0] = 0x36;
- RequestPara.ReposeChild.byCid1[1] = 0x30;
- //Cid2
- memcpy(RequestPara.ReposeChild.byCid2, szSendCmd, 2);
- //Length;
- memcpy(RequestPara.ReposeChild.byLength,szLength,4);
- if (strcmp(szCmd,"cmd-25") == 0)
- {
- memcpy(RequestPara.ReposeChild.byInfo,szSetMsg,strlen(szSetMsg));
- }
- else
- {
- char szSet[10] ={0};
- szSet[0] = nCDT;
- memcpy(&szSet[1],szSetMsg,strlen(szSetMsg));
- memcpy(RequestPara.ReposeChild.byInfo,szSet,strlen(szSet));
- }
-
- int nLen = sizeof(RequestPara.byCheckSum) -1;
- //校验码;
- BYTE *pDataBuf = new BYTE[ iLen - nLen];
- memset(pDataBuf, 0, iLen -nLen );
- memcpy(pDataBuf, &RequestPara, iLen - nLen);
- GetCheckSum((char *)pDataBuf + 1, chChkSum, iLen - sizeof(RequestPara.byCheckSum) - 2 );
- delete []pDataBuf;
- pDataBuf = NULL;
- RequestPara.byCheckSum[0] = chChkSum[0];
- RequestPara.byCheckSum[1] = chChkSum[1];
- RequestPara.byCheckSum[2] = chChkSum[2];
- RequestPara.byCheckSum[3] = chChkSum[3];
- //结束符
- RequestPara.byEoi = 0x0D;
- ResetEvent( m_hSemComm );//设置为无信号(写设备优先级高);
- int nDataLen = (int)sizeof(RequestPara);
- //WriteMessage写命令入串口,返回实际写入的字节数;
- int iResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- if (iResult == nDataLen)
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return 0;
- }
- else
- {
- SetEvent( m_hSemComm );//设置为有信号;
- return EER_CODE_AIRTMASTER_COM_WRITE_DATA;
- }
- #endif
- return 0;
- }
- int CTmaster::ResponseWriteStatus( CCommProcess *pComm,int nAddr)
- {
- char chBuf[30] = {0};
- //读取请求写入的命令;
- int iReadLen = pComm->Read((BYTE *)chBuf, sizeof(chBuf));
- if( iReadLen <= 0) // 串口没有读到数据
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_AIRTMASTER_COM_READ_NO_DATA;
- }
- SetEvent( m_hSemComm );
- if (chBuf[7] == 0x00 && chBuf[8] == 0x00)//RTN =00H 成功接收;
- {
- return 0;
- }
- else
- return ERR_CODE_AIRTMASTER_COM_INVALIDRES;
- return 0;
- }
- void CTmaster::SetE0VarMsg(int nAddr, char szCmd[MAX_CMD], char *pBuff)
- {
- if( strcmp(szCmd, "cmd-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 ||
- 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szE0Msg[nAddr-1], pBuff, sizeof(m_szE0Msg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- UINT CTmaster::GetCheckSum(char *pBuf, char chDest[5], int len)
- {
- LOG4C((LOG_NOTICE,"pBuf = %s, len = %d",pBuf,len));
- WORD iSum = 0;;
- for(int i=0; i<len; i++) //求和;
- {
- iSum += pBuf[i];
- }
- WORD iCompliment = iSum;
- iCompliment = ~iCompliment; //取反;
- iCompliment++;
- 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);
- }
- WORD CTmaster::RtnCheck(char szRecvdMsg[],int len)
- {
- char ch[2] = {0};
- ch[0] = szRecvdMsg[7];
- ch[1] = szRecvdMsg[8];
- if (atoi(ch) == 0)
- {
- return ERR_CODE_RTN_NATURAL;
- }
- else if (atoi(ch) ==1)
- {
- return ERR_CODE_RTN_VER_ERROR;
- }
- else if (atoi(ch) ==2)
- {
- return ERR_CODE_RTN_CHKSUM_ERROR;
- }
- else if (atoi(ch) ==3)
- {
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- else if (atoi(ch) ==4)
- {
- return ERR_CODE_RTN_CID_ERROR;
- }
- else if (atoi(ch) ==5)
- {
- return ERR_CODE_RTN_COMMAND_FORMAT;
- }
- else if (atoi(ch) ==6)
- {
- return ERR_CODE_RTN_INVALID_DATA;
- }
- return -1;
- }
- void CTmaster::SimulationCommData(void)
- {
- ///
- // 获取系统模拟量量化数据
- m_szE0Msg[0][0]=0x7E;
- m_szE0Msg[0][1]=0x32;
- m_szE0Msg[0][2]=0x30;
- m_szE0Msg[0][3]=0x30;
- m_szE0Msg[0][4]=0x31;
- m_szE0Msg[0][5]=0x36;
- m_szE0Msg[0][6]=0x30;
- m_szE0Msg[0][7]=0x30;
- m_szE0Msg[0][8]=0x30;
- m_szE0Msg[0][9]=0x33;
- m_szE0Msg[0][10]=0x30;
- m_szE0Msg[0][11]=0x33;
- m_szE0Msg[0][12]=0x41;
- m_szE0Msg[0][13]=0x30;
- m_szE0Msg[0][14]=0x31;
- m_szE0Msg[0][15]=0x30;
- m_szE0Msg[0][16]=0x39;
- m_szE0Msg[0][17]=0x30;
- m_szE0Msg[0][18]=0x32;
- m_szE0Msg[0][19]=0x32;
- m_szE0Msg[0][20]=0x36;
- m_szE0Msg[0][21]=0x30;
- m_szE0Msg[0][22]=0x31;
- m_szE0Msg[0][23]=0x32;
- m_szE0Msg[0][24]=0x35;
- m_szE0Msg[0][25]=0x30;
- m_szE0Msg[0][26]=0x38;
- m_szE0Msg[0][27]=0x31;
- m_szE0Msg[0][28]=0x32;
- m_szE0Msg[0][29]=0x30;
- m_szE0Msg[0][30]=0x42;
- m_szE0Msg[0][31]=0x34;
- m_szE0Msg[0][32]=0x42;
- m_szE0Msg[0][33]=0x46;
- m_szE0Msg[0][34]=0x46;
- m_szE0Msg[0][35]=0x30;
- m_szE0Msg[0][36]=0x30;
- m_szE0Msg[0][37]=0x31;
- m_szE0Msg[0][38]=0x42;
- m_szE0Msg[0][39]=0x30;
- m_szE0Msg[0][40]=0x30;
- m_szE0Msg[0][41]=0x31;
- m_szE0Msg[0][42]=0x32;
- m_szE0Msg[0][43]=0x30;
- m_szE0Msg[0][44]=0x30;
- m_szE0Msg[0][45]=0x34;
- m_szE0Msg[0][46]=0x36;
- m_szE0Msg[0][47]=0x30;
- m_szE0Msg[0][48]=0x30;
- m_szE0Msg[0][49]=0x32;
- m_szE0Msg[0][50]=0x33;
- m_szE0Msg[0][51]=0x30;
- m_szE0Msg[0][52]=0x30;
- m_szE0Msg[0][53]=0x33;
- m_szE0Msg[0][54]=0x32;
- m_szE0Msg[0][55]=0x30;
- m_szE0Msg[0][56]=0x30;
- m_szE0Msg[0][57]=0x30;
- m_szE0Msg[0][58]=0x41;
- m_szE0Msg[0][59]=0x30;
- m_szE0Msg[0][60]=0x31;
- m_szE0Msg[0][61]=0x30;
- m_szE0Msg[0][62]=0x34;
- m_szE0Msg[0][63]=0x30;
- m_szE0Msg[0][64]=0x30;
- m_szE0Msg[0][65]=0x31;
- m_szE0Msg[0][66]=0x45;
- m_szE0Msg[0][67]=0x30;
- m_szE0Msg[0][68]=0x30;
- m_szE0Msg[0][69]=0x38;
- m_szE0Msg[0][70]=0x42;
- m_szE0Msg[0][71]=0x46;
- m_szE0Msg[0][72]=0x31;
- m_szE0Msg[0][73]=0x44;
- m_szE0Msg[0][74]=0x35;
- m_szE0Msg[0][75]=0x0D;
- }
|