| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- // DataMate3000.cpp: implementation of the CDataMate3000 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "M84c5z.h"
- #include "CommProcess.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CM84c5z::CM84c5z( 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 ) ); //创建或打开一个命名的或无名的事件对象
- for( int i = 0; i < MAX_ADDR; i++ )
- {
- memset(m_szMz_41Msg[i], 0, sizeof(m_szMz_41Msg[i]));
- memset(m_szMz_43Msg[i], 0, sizeof(m_szMz_43Msg[i]));
- memset(m_szMz_44Msg[i], 0, sizeof(m_szMz_44Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CM84c5z::~CM84c5z()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL CM84c5z::MzOpenComm(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 CM84c5z::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接口
- )
- {
- //LOG4C((LOG_NOTICE," CM84c5z::SendReadRequest"));
- int nIndex = 0, nLen = 0;
- char szSendMsg[MZ_SEND_MSG] = {0};
- char szType[DK_TYPE_LENGTH] = {0};
- char szScreen[MAX_CMD] = {0};
- int startBit = 0;
- int endBit = 0;
- GetMzFromIni(szPath, szIniName, szCmd, szSendMsg, szType, szScreen, nIndex, nLen, startBit, endBit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( (strlen(m_szMz_41Msg[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-6") == 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 ) )|| //获取系统模拟量量化数据(浮点数)
- ( strlen(m_szMz_43Msg[nAddr - 1]) == 0 &&
- (strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 ) )|| //获取开关输入状态
- ( strlen(m_szMz_44Msg[nAddr - 1]) == 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-1") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 ||
- strcmp(szCmd, "cmd-14") == 0 ||
- strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-17") == 0 ) //获取系统参数(定点数)
- {
- nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType, szScreen );
- 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 = GetMz_41VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetMz_43VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- nRet = GetMz_44VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
- //LOG4C((LOG_NOTICE, "%s = %s, return %d", szCmd, szMsg, nRet));
- return nRet;
- }
- int CM84c5z::GetMz_41VarMsg( 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 || strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-6") == 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)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szMz_41Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
- //LOG4C((LOG_NOTICE, "GetMz_41VarMsg m_szMz_41Msg = %s,szMsg=%s, cmd = %s, type =%s",m_szMz_41Msg, szMsg, szCmd, szType));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CM84c5z::GetMz_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-14") == 0 ||
- strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szMz_43Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CM84c5z::GetMz_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-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)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- DataConversion(szType, m_szMz_44Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CM84c5z::GetDeviceParam(int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[MZ_SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH],
- char szScreen[CID2_LENGTH]) // 变量数据类型
- {
- int nRet = -1;
- nRet = RequestStatus(nAddr, szCmd, pComm, szSendMsg, szScreen);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CM84c5z::SetDeviceParam(int nAddr,
- CCommProcess *pComm, //串口对象指针
- char szSendMsg[MZ_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 CM84c5z::SetMz_41VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
- strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
- strcmp(szCmd, "cmd-6") == 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 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szMz_41Msg[nAddr - 1], pBuffer, sizeof(m_szMz_41Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CM84c5z::SetMz_43VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-14") == 0 ||
- strcmp(szCmd, "cmd-15") == 0 ||
- strcmp(szCmd, "cmd-16") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szMz_43Msg[nAddr - 1], pBuffer, sizeof(m_szMz_43Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CM84c5z::SetMz_44VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( 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)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szMz_44Msg[nAddr - 1], pBuffer, sizeof(m_szMz_44Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- // 发送设置设备参数请求
- int CM84c5z::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接口
- )
- {
- return 0;
- }
- //读取配置文件
- int CM84c5z::GetMzFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szCid2[CID2_LENGTH], //发送Buffer
- char szType[TYPE_LENGTH],
- char szScreen[CID2_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);
- GetPrivateProfileString("CMD", "Screen-count", "", szScreen, CID2_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 CM84c5z::RequestStatus( int nAddr,
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[MZ_SEND_MSG], //发送Buffer
- char szScreen[CID2_LENGTH] //交流屏个数
- )
- {
- int nRet = -1;
- #if DEBUG_MZ
- 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] = 0x34;
- 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[1] = 0x30;
- RequestPara.headMes.Lenth[2] = 0x30;
- RequestPara.headMes.Lenth[3] = 0x32;
- RequestPara.headMes.Lenth[0] = ByteToAscii(GetLengthSum(RequestPara.headMes.Lenth));
- RequestPara.headMes.GroupInfo[0] = 0x46;
- RequestPara.headMes.GroupInfo[1] = 0x46;
- //校验码
- 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;
- //LOG4C_HEX_DUMP((LOG_NOTICE, (char *)&RequestPara, sizeof(RequestPara)));
- 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_MZ_COM_WRITE_DATA;
- }
- }
- else
- {
- LOG4C((LOG_NOTICE, "m_hSemComm no single"));
- delete []pDataBuf;
- return ERR_CODE_MZ_COM_BUSY;
- }
- delete[]pDataBuf;
- #endif
- return 0;
- }
- int CM84c5z::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_MZ
- 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," nReadLen = %d ,pBuffer = %s",nReadLen,pBuffer));
- if( nReadLen <= 0)
- {
- // 串口没有读到数据
- //TRACE("串口没有读到数据!\r\n");
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_MZ_COM_READ_NO_DATA;
- }
- //else if( nReadLen < nReceiveLen )
- //{
- // SetEvent( m_hSemComm );
- // if( pBuffer )
- // {
- // delete[] pBuffer;
- // pBuffer = NULL;
- // }
- // return ERR_CODE_MZ_COM_FAULT;
- //}
- //else if( nReadLen > nLen )
- //{
- // // 完全代码,不一定能执行到
- // SetEvent( m_hSemComm );
- // if( pBuffer != NULL)
- // {
- // delete[] pBuffer;
- // pBuffer = NULL;
- // }
- // return ERR_CODE_MZ_COM_READ_LEN_OVER;
- //}
- if (LengthCheck(pBuffer) != 0)
- {
- SetEvent(m_hSemComm);
- TRACE("长度校验出错");
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- 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)
- {
- TRACE("RTN校验出错");
- SetEvent(m_hSemComm);
- if (pBuffer != NULL)
- {
- delete [] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
- }
- SetMz_41VarMsg(nAddr, szCmd, pBuffer);
- SetMz_43VarMsg(nAddr, szCmd, pBuffer);
- SetMz_44VarMsg(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 CM84c5z::WriteRequestStatus( int nAddr,
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[MZ_SEND_MSG] //发送Buffer
- )
- {
- #if DEBUG_MZ
- 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_MZ_COM_WRITE_DATA;
- }
- #endif
- return 0;
- }
- int CM84c5z::WriteResponseStatus( int nAddr, // 设备地址
- CCommProcess *pComm, //串口对象指针
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- SetEvent( m_hSemComm );
- return 0;
- }
- void CM84c5z::SimulationCommData(int nAddr)
- {
- m_szMz_41Msg[nAddr - 1][0] = 0x7E;
- m_szMz_41Msg[nAddr - 1][1] = 0x32;
- m_szMz_41Msg[nAddr - 1][2] = 0x30;
- m_szMz_41Msg[nAddr - 1][3] = 0x30;
- m_szMz_41Msg[nAddr - 1][4] = 0x31;
- m_szMz_41Msg[nAddr - 1][5] = 0x34;
- m_szMz_41Msg[nAddr - 1][6] = 0x30;
- m_szMz_41Msg[nAddr - 1][7] = 0x30;
- m_szMz_41Msg[nAddr - 1][8] = 0x30;
- m_szMz_41Msg[nAddr - 1][9] = 0x38;
- m_szMz_41Msg[nAddr - 1][10] = 0x30;
- m_szMz_41Msg[nAddr - 1][11] = 0x36;
- m_szMz_41Msg[nAddr - 1][12] = 0x32;
- m_szMz_41Msg[nAddr - 1][13] = 0x30;
- m_szMz_41Msg[nAddr - 1][14] = 0x30;
- m_szMz_41Msg[nAddr - 1][15] = 0x30;
- m_szMz_41Msg[nAddr - 1][16] = 0x31;
- m_szMz_41Msg[nAddr - 1][17] = 0x30;
- m_szMz_41Msg[nAddr - 1][18] = 0x32;
- m_szMz_41Msg[nAddr - 1][19] = 0x43;
- m_szMz_41Msg[nAddr - 1][20] = 0x34;
- m_szMz_41Msg[nAddr - 1][21] = 0x43;
- m_szMz_41Msg[nAddr - 1][22] = 0x45;
- m_szMz_41Msg[nAddr - 1][23] = 0x37;
- m_szMz_41Msg[nAddr - 1][24] = 0x31;
- m_szMz_41Msg[nAddr - 1][25] = 0x34;
- m_szMz_41Msg[nAddr - 1][26] = 0x33;
- m_szMz_41Msg[nAddr - 1][27] = 0x42;
- m_szMz_41Msg[nAddr - 1][28] = 0x46;
- m_szMz_41Msg[nAddr - 1][29] = 0x37;
- m_szMz_41Msg[nAddr - 1][30] = 0x36;
- m_szMz_41Msg[nAddr - 1][31] = 0x37;
- m_szMz_41Msg[nAddr - 1][32] = 0x32;
- m_szMz_41Msg[nAddr - 1][33] = 0x34;
- m_szMz_41Msg[nAddr - 1][34] = 0x33;
- m_szMz_41Msg[nAddr - 1][35] = 0x43;
- m_szMz_41Msg[nAddr - 1][36] = 0x38;
- m_szMz_41Msg[nAddr - 1][37] = 0x32;
- m_szMz_41Msg[nAddr - 1][38] = 0x36;
- m_szMz_41Msg[nAddr - 1][39] = 0x37;
- m_szMz_41Msg[nAddr - 1][40] = 0x31;
- m_szMz_41Msg[nAddr - 1][41] = 0x34;
- m_szMz_41Msg[nAddr - 1][42] = 0x33;
- m_szMz_41Msg[nAddr - 1][43] = 0x20;
- m_szMz_41Msg[nAddr - 1][44] = 0x20;
- m_szMz_41Msg[nAddr - 1][45] = 0x20;
- m_szMz_41Msg[nAddr - 1][46] = 0x20;
- m_szMz_41Msg[nAddr - 1][47] = 0x20;
- m_szMz_41Msg[nAddr - 1][48] = 0x20;
- m_szMz_41Msg[nAddr - 1][49] = 0x20;
- m_szMz_41Msg[nAddr - 1][50] = 0x20;
- m_szMz_41Msg[nAddr - 1][51] = 0x30;
- m_szMz_41Msg[nAddr - 1][52] = 0x30;
- m_szMz_41Msg[nAddr - 1][53] = 0x43;
- m_szMz_41Msg[nAddr - 1][54] = 0x31;
- m_szMz_41Msg[nAddr - 1][55] = 0x41;
- m_szMz_41Msg[nAddr - 1][56] = 0x30;
- m_szMz_41Msg[nAddr - 1][57] = 0x37;
- m_szMz_41Msg[nAddr - 1][58] = 0x32;
- m_szMz_41Msg[nAddr - 1][59] = 0x34;
- m_szMz_41Msg[nAddr - 1][60] = 0x33;
- m_szMz_41Msg[nAddr - 1][61] = 0x43;
- m_szMz_41Msg[nAddr - 1][62] = 0x34;
- m_szMz_41Msg[nAddr - 1][63] = 0x43;
- m_szMz_41Msg[nAddr - 1][64] = 0x45;
- m_szMz_41Msg[nAddr - 1][65] = 0x37;
- m_szMz_41Msg[nAddr - 1][66] = 0x31;
- m_szMz_41Msg[nAddr - 1][67] = 0x34;
- m_szMz_41Msg[nAddr - 1][68] = 0x33;
- m_szMz_41Msg[nAddr - 1][69] = 0x46;
- m_szMz_41Msg[nAddr - 1][70] = 0x37;
- m_szMz_41Msg[nAddr - 1][71] = 0x46;
- m_szMz_41Msg[nAddr - 1][72] = 0x41;
- m_szMz_41Msg[nAddr - 1][73] = 0x36;
- m_szMz_41Msg[nAddr - 1][74] = 0x46;
- m_szMz_41Msg[nAddr - 1][75] = 0x34;
- m_szMz_41Msg[nAddr - 1][76] = 0x33;
- m_szMz_43Msg[nAddr - 1][0] = 0x7E;
- m_szMz_43Msg[nAddr - 1][1] = 0x32;
- m_szMz_43Msg[nAddr - 1][2] = 0x30;
- m_szMz_43Msg[nAddr - 1][3] = 0x30;
- m_szMz_43Msg[nAddr - 1][4] = 0x31;
- m_szMz_43Msg[nAddr - 1][5] = 0x34;
- m_szMz_43Msg[nAddr - 1][6] = 0x30;
- m_szMz_43Msg[nAddr - 1][7] = 0x30;
- m_szMz_43Msg[nAddr - 1][8] = 0x30;
- m_szMz_43Msg[nAddr - 1][9] = 0x34;
- m_szMz_43Msg[nAddr - 1][10] = 0x30;
- m_szMz_43Msg[nAddr - 1][11] = 0x30;
- m_szMz_43Msg[nAddr - 1][12] = 0x43;
- m_szMz_43Msg[nAddr - 1][13] = 0x30;
- m_szMz_43Msg[nAddr - 1][14] = 0x30;
- m_szMz_43Msg[nAddr - 1][15] = 0x30;
- m_szMz_43Msg[nAddr - 1][16] = 0x31;
- m_szMz_43Msg[nAddr - 1][17] = 0x30;
- m_szMz_43Msg[nAddr - 1][18] = 0x30;
- m_szMz_43Msg[nAddr - 1][19] = 0x30;
- m_szMz_43Msg[nAddr - 1][20] = 0x32;
- m_szMz_43Msg[nAddr - 1][21] = 0x45;
- m_szMz_43Msg[nAddr - 1][22] = 0x30;
- m_szMz_43Msg[nAddr - 1][23] = 0x45;
- m_szMz_43Msg[nAddr - 1][24] = 0x34;
- m_szMz_43Msg[nAddr - 1][25] = 0x46;
- m_szMz_43Msg[nAddr - 1][26] = 0x42;
- m_szMz_43Msg[nAddr - 1][27] = 0x33;
- m_szMz_43Msg[nAddr - 1][28] = 0x31;
- m_szMz_43Msg[nAddr - 1][29] = 0x0D;
- m_szMz_44Msg[nAddr - 1][0] = 0x7E;
- m_szMz_44Msg[nAddr - 1][1] = 0x32;
- m_szMz_44Msg[nAddr - 1][2] = 0x30;
- m_szMz_44Msg[nAddr - 1][3] = 0x30;
- m_szMz_44Msg[nAddr - 1][4] = 0x31;
- m_szMz_44Msg[nAddr - 1][5] = 0x34;
- m_szMz_44Msg[nAddr - 1][6] = 0x30;
- m_szMz_44Msg[nAddr - 1][7] = 0x30;
- m_szMz_44Msg[nAddr - 1][8] = 0x30;
- m_szMz_44Msg[nAddr - 1][9] = 0x32;
- m_szMz_44Msg[nAddr - 1][10] = 0x30;
- m_szMz_44Msg[nAddr - 1][11] = 0x32;
- m_szMz_44Msg[nAddr - 1][12] = 0x43;
- m_szMz_44Msg[nAddr - 1][13] = 0x30;
- m_szMz_44Msg[nAddr - 1][14] = 0x30;
- m_szMz_44Msg[nAddr - 1][15] = 0x30;
- m_szMz_44Msg[nAddr - 1][16] = 0x31;
- m_szMz_44Msg[nAddr - 1][17] = 0x30;
- m_szMz_44Msg[nAddr - 1][18] = 0x32;
- m_szMz_44Msg[nAddr - 1][19] = 0x30;
- m_szMz_44Msg[nAddr - 1][20] = 0x31;
- m_szMz_44Msg[nAddr - 1][21] = 0x30;
- m_szMz_44Msg[nAddr - 1][22] = 0x31;
- m_szMz_44Msg[nAddr - 1][23] = 0x30;
- m_szMz_44Msg[nAddr - 1][24] = 0x31;
- m_szMz_44Msg[nAddr - 1][25] = 0x20;
- m_szMz_44Msg[nAddr - 1][26] = 0x20;
- m_szMz_44Msg[nAddr - 1][27] = 0x30;
- m_szMz_44Msg[nAddr - 1][28] = 0x30;
- m_szMz_44Msg[nAddr - 1][29] = 0x30;
- m_szMz_44Msg[nAddr - 1][30] = 0x34;
- m_szMz_44Msg[nAddr - 1][31] = 0x30;
- m_szMz_44Msg[nAddr - 1][32] = 0x35;
- m_szMz_44Msg[nAddr - 1][33] = 0x20;
- m_szMz_44Msg[nAddr - 1][34] = 0x20;
- m_szMz_44Msg[nAddr - 1][35] = 0x30;
- m_szMz_44Msg[nAddr - 1][36] = 0x35;
- m_szMz_44Msg[nAddr - 1][37] = 0x30;
- m_szMz_44Msg[nAddr - 1][38] = 0x35;
- m_szMz_44Msg[nAddr - 1][39] = 0x30;
- m_szMz_44Msg[nAddr - 1][40] = 0x31;
- m_szMz_44Msg[nAddr - 1][41] = 0x30;
- m_szMz_44Msg[nAddr - 1][42] = 0x31;
- m_szMz_44Msg[nAddr - 1][43] = 0x30;
- m_szMz_44Msg[nAddr - 1][44] = 0x31;
- m_szMz_44Msg[nAddr - 1][45] = 0x20;
- m_szMz_44Msg[nAddr - 1][46] = 0x20;
- m_szMz_44Msg[nAddr - 1][47] = 0x30;
- m_szMz_44Msg[nAddr - 1][48] = 0x31;
- m_szMz_44Msg[nAddr - 1][49] = 0x30;
- m_szMz_44Msg[nAddr - 1][50] = 0x31;
-
- }
- WORD CM84c5z::RtnCheck(char Msg[VAR_MSG])
- {
- char buffer[3] = {0};
- int j = 0;
- for (int i = 7; i < 9; i++)
- {
- buffer[j++] = Msg[i];
- }
- buffer[j] = '\0';
- if (strcmp(buffer,"00") == 0)
- {
- return 0;
- }
- else if (strcmp(buffer,"01") == 0)
- {
- return ERR_CODE_RTN_VER_ERROR;
- }
- else if (strcmp(buffer,"02") == 0)
- {
- return ERR_CODE_RTN_CHKSUM_ERROR;
- }
- else if (strcmp(buffer,"03") == 0)
- {
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- else if (strcmp(buffer,"04") == 0)
- {
- return ERR_CODE_RTN_CID_ERROR;
- }
- else if (strcmp(buffer,"05") == 0)
- {
- return ERR_CODE_RTN_COMMAND_FORMAT;
- }
- else if (strcmp(buffer,"06") == 0)
- {
- return ERR_CODE_RTN_INVALID_LIMIT;
- }
- else if (strcmp(buffer,"E0") == 0)
- {
- return ERR_CODE_RTN_OPERATE_FAIL;
- }
- else if (strcmp(buffer,"E1") == 0)
- {
- return ERR_CODE_RTN_EQUIT_FAIL;
- }
- else if (strcmp(buffer,"E3")==0 ||
- strcmp(buffer,"E4")==0
- )
- {
- return ERR_CODE_RTN_KEEP;
- }
- else if (strcmp(buffer, "E5")==0 ||
- strcmp(buffer, "E6") == 0 ||
- strcmp(buffer, "E7") == 0 ||
- strcmp(buffer, "E8") == 0 ||
- strcmp(buffer, "E9") == 0
- )
- {
- return ERR_CODE_RTN_WATCH;
- }
- else if (strcmp(buffer,"EA") == 0 ||
- strcmp(buffer, "EB") == 0 ||
- strcmp(buffer, "EC") == 0 ||
- strcmp(buffer, "ED") == 0 ||
- strcmp(buffer, "EE") == 0 ||
- strcmp(buffer, "EF") == 0
- )
- {
- return ERR_CODE_RTN_ELECTICAL_SOURCE;
- }
- return 0;
- }
- UINT CM84c5z::GetCheckSum(char *pBuf, char chDest[5], int 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);
- }
- BYTE CM84c5z::GetLengthSum(BYTE szChar[])
- {
- unsigned int nData = 0;
- unsigned int nData1 =0;
- unsigned short nDataLength = 0;
- unsigned short nChar1;
- nChar1 =AsciiToBYTE(szChar[1]) +
- AsciiToBYTE(szChar[2]) +
- AsciiToBYTE(szChar[3]);
- nChar1 = (~nChar1) + 1;
- nChar1 %=16;
- return (BYTE)nChar1;
- }
- int CM84c5z::LengthCheck(char szSendMsg[])
- {
- int len = (int)strlen(szSendMsg);
- char buffer[5] = {0};
- int j = 0;
- for (int i = 9; i< 13; i++)
- {
- buffer[j++] = szSendMsg[i]; //取出检验位
- }
- buffer[j] ='0';
- if(!ValiateLength(buffer)) // 相加模十六取反加一
- {
- return ERR_CODE_RTN_LCHKSUM_ERROR;
- }
- /*转换为十六进制*/
- return 0;
- }
- BOOL CM84c5z::ValiateLength(char szChar[])
- {
- unsigned int nData = 0;
- unsigned int nData1 =0;
- unsigned short nDataLength = 0;
- unsigned short nChar1;
- nChar1 =AsciiToBYTE(szChar[1]) +
- AsciiToBYTE(szChar[2]) +
- AsciiToBYTE(szChar[3]);
- nChar1 = (~nChar1) + 1;
- nChar1 %=16;
- nDataLength = AsciiToBYTE(szChar[0]);
- if (nDataLength != nChar1)
- {
- return FALSE;
- }
- return TRUE;
- }
|