| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904 |
- #include "stdafx.h"
- #include "JB1501A64P.h"
- #include "CommProcess.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- CJB1501A64P::CJB1501A64P(
- 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_szJB1501A64P_1Msg[i], 0, sizeof(m_szJB1501A64P_1Msg[i]));
- memset(m_szJB1501A64P_3Msg[i], 0, sizeof(m_szJB1501A64P_3Msg[i]));
- memset(m_szJB1501A64P_7Msg[i], 0, sizeof(m_szJB1501A64P_7Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CJB1501A64P::~CJB1501A64P()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL CJB1501A64P::JB1501A64POpenComm(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 CJB1501A64P::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, StaBit = 0, EndBit = 0;
- char szSendMsg[JB1501A64P_SEND_MSG];
- char szType[TYPE_LENGTH] = {0};
- char szCid2[CID2_LENGTH] = {0};
- int type = 0;
- int eol = 255;
- memset(szSendMsg, 0, JB1501A64P_SEND_MSG);
- memset(szMsg, 0, VAR_MSG);
- GetFromIni(szPath, szIniName, szCmd, szCid2, type, nIndex, nLen, StaBit, EndBit);
- szSendMsg[0] = nAddr;
- szSendMsg[1] = type;
- szSendMsg[2] = eol;
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( nAddr < 1 ) return -1;
- if(
- ( strlen(m_szJB1501A64P_1Msg[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-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-42") == 0 ||
- strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
- strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
- strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
- strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
- strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
- strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
- strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
- strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
- strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
- strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
- strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
- strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
- strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 ||
- strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 ||
- strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 ||
- strcmp(szCmd, "cmd-73") == 0
- ))
- ||
- ( strlen(m_szJB1501A64P_7Msg[nAddr-1]) == 0 &&
- ( strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ) )
- ||strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-4") == 0 || strcmp(szCmd, "cmd-11") == 0 ||strcmp(szCmd, "cmd-14") == 0
- )
- {
- nRet = GetDeviceParam( pComm, nAddr, 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 = GetJB1501A64P_1VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType);
- nRet = GetJB1501A64P_7VarMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType);
- //LOG4C((LOG_NOTICE,"szCmd = %s ,返回值 = %s ",szCmd, szMsg));
- return nRet;
- }
- int CJB1501A64P::GetJB1501A64P_1VarMsg(
- int nAddr,
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- 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-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-42") == 0 ||
- strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
- strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
- strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
- strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
- strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
- strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
- strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
- strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
- strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
- strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
- strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
- strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
- strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 ||
- strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 ||
- strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 ||
- strcmp(szCmd, "cmd-73") == 0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szMsg, m_szJB1501A64P_1Msg[nAddr - 1] + nIndex, nLen);
- //DataConversion(szType, m_szJB1501A64P_1Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CJB1501A64P::GetJB1501A64P_3VarMsg(
- int nAddr,
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- int nRet = 0;
- if(strcmp(szCmd, "cmd-10") == 0 )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- //memcpy(szMsg, m_szJB1501A64P_3Msg + nIndex, nLen);
- DataConversion(szType, m_szJB1501A64P_3Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CJB1501A64P::GetJB1501A64P_7VarMsg(
- int nAddr,
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- int nRet = 0;
- if(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(szMsg, m_szJB1501A64P_7Msg + nIndex, nLen);
- DataConversion(szType, m_szJB1501A64P_7Msg[nAddr - 1] + nIndex, szMsg, nLen, 0, 0);
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CJB1501A64P::GetDeviceParam(
- CCommProcess *pComm, //串口对象指针
- int nAddr,
- char szSendMsg[JB1501A64P_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(szCmd, pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = ResponseStatus(pComm, nAddr,szSendMsg ,szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- int CJB1501A64P::SetDeviceParam(
- CCommProcess *pComm, //串口对象指针
- int nAddr,
- char szSendMsg[JB1501A64P_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(szCmd, pComm, szSendMsg);
- if( nRet != 0 )
- {
- return nRet; // 串口忙
- }
- nRet = WriteResponseStatus(pComm, nAddr, szCmd, szMsg, nIndex, nLen, szType);
- return nRet;
- }
- void CJB1501A64P::SetJB1501A64P_1VarMsg( 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-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-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-42") == 0 ||
- strcmp(szCmd, "cmd-43") == 0 || strcmp(szCmd, "cmd-44") == 0 ||
- strcmp(szCmd, "cmd-45") == 0 || strcmp(szCmd, "cmd-46") == 0 ||
- strcmp(szCmd, "cmd-47") == 0 || strcmp(szCmd, "cmd-48") == 0 ||
- strcmp(szCmd, "cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 ||
- strcmp(szCmd, "cmd-51") == 0 || strcmp(szCmd, "cmd-52") == 0 ||
- strcmp(szCmd, "cmd-53") == 0 || strcmp(szCmd, "cmd-54") == 0 ||
- strcmp(szCmd, "cmd-55") == 0 || strcmp(szCmd, "cmd-56") == 0 ||
- strcmp(szCmd, "cmd-57") == 0 || strcmp(szCmd, "cmd-58") == 0 ||
- strcmp(szCmd, "cmd-59") == 0 || strcmp(szCmd, "cmd-60") == 0 ||
- strcmp(szCmd, "cmd-61") == 0 || strcmp(szCmd, "cmd-62") == 0 ||
- strcmp(szCmd, "cmd-63") == 0 || strcmp(szCmd, "cmd-64") == 0 ||
- strcmp(szCmd, "cmd-65") == 0 || strcmp(szCmd, "cmd-66") == 0 ||
- strcmp(szCmd, "cmd-67") == 0 || strcmp(szCmd, "cmd-68") == 0 ||
- strcmp(szCmd, "cmd-69") == 0 || strcmp(szCmd, "cmd-70") == 0 ||
- strcmp(szCmd, "cmd-71") == 0 || strcmp(szCmd, "cmd-72") == 0 ||
- strcmp(szCmd, "cmd-73") == 0
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szJB1501A64P_1Msg[nAddr - 1], pBuffer, sizeof(m_szJB1501A64P_1Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CJB1501A64P::SetJB1501A64P_3VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if( strcmp(szCmd, "cmd-10") == 0)
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_szJB1501A64P_3Msg[nAddr - 1], pBuffer, sizeof(m_szJB1501A64P_3Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CJB1501A64P::SetJB1501A64P_7VarMsg( int nAddr, char szCmd[MAX_CMD], char *pBuffer)
- {
- if(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_szJB1501A64P_7Msg[nAddr - 1], pBuffer, sizeof(m_szJB1501A64P_7Msg[nAddr - 1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- WORD CJB1501A64P::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;
- }
- char CJB1501A64P::GetLCheckSum(char *pBuf, int len)
- {
- //WORD iSum = 0;
- char chCompliment = 0;
- //unsigned char chCompliment[2] = {0};
- for(int i=0; i<len; i++)//求和
- {
- chCompliment += AsciiToBYTE(pBuf[i]);
- //chCompliment += pBuf[i];
- }
- chCompliment = ~chCompliment;//取反
- chCompliment++;
- return chCompliment;
- }
- // 发送设置设备参数请求
- int CJB1501A64P::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[JB1501A64P_SEND_MSG];
- char szType[TYPE_LENGTH] = {0};
- char szCid2[CID2_LENGTH] = {0};
- int type = 0;
- int eol = 255;
- memset(szSendMsg, 0, JB1501A64P_SEND_MSG);
- memset(szMsg, 0, VAR_MSG);
- GetFromIni(szPath, szIniName, szCmd, szCid2, type, nIndex, nLen, StaBit, EndBit);
- szSendMsg[0] = nAddr;
- szSendMsg[1] = type;
- szSendMsg[2] = eol;
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if( nAddr < 1 ) return -1;
- if( strcmp(szCmd, "cmd-10") == 0 )
- {
- SetDeviceParam( pComm, nAddr, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
- }
- return 0;
- }
- //读取配置文件
- int CJB1501A64P::GetFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char szCid2[CID2_LENGTH], //发送Buffer
- int &szType,
- int &nIndex,
- int &nLen,
- int &nStaBit,
- int &nEndBit
- )
- {
- char szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);//只读config下面的ini
- GetPrivateProfileString(szCmd, "SendCmd", "", szCid2, CID2_LENGTH, szFile);//读取配置文件中的一段字符串
- szType = GetPrivateProfileInt(szCmd, "SendCmd", 0, 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 CJB1501A64P::RequestStatus(
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[JB1501A64P_SEND_MSG] //发送Buffer
- )
- {
- #if DEBUG_JB1501A64P
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- int nDataLen = (int)strlen(chSendMsg);
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((unsigned char *)chSendMsg, nDataLen);
- if( nResult == nDataLen )
- {
- ;
- }
- else
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_JB1501A64P_COM_INVALIDRES;
- }
- }
- else
- {
- return ERR_CODE_JB1501A64P_COM_BUSY;
- }
- #endif
- return 0;
- }
- int CJB1501A64P::ResponseStatus(
- CCommProcess *pComm, //串口对象指针
- int nAddr, // 设备地址
- char szSendMsg[JB1501A64P_SEND_MSG],
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- #if DEBUG_JB1501A64P
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
- nReceiveLen = JB1501A64P_RECEIVE_MSG;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if( nReadLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_JB1501A64P_COM_READ_NO_DATA;
- }
- if ( szSendMsg[1] == 100)
- {
- int type = pBuffer[1] ;
- int alarmNum = pBuffer[2] ;
- char szGetMsg[100] = {0};
- memcpy(szGetMsg,"10<000000000000000000000000000000000000000000000000000000000000000000.",sizeof("10<000000000000000000000000000000000000000000000000000000000000000000."));
- if (0 == type)
- {
- for(int j=0; j < 6; j++)
- szGetMsg[63+j] = pBuffer[2 + j] + 48;
- //if ( pBuffer[2 + j] == 0x07 )
- //szGetMsg[63+j] = '7';
- //LOG4C((LOG_NOTICE," 正常情况 "));
- }
- else if (1 == type)
- {
- szGetMsg[1] = 49;
- szGetMsg[2] = alarmNum +48;
- for (int i=3; i< (alarmNum + 3); i++)
- {
- int ctrlNum = (unsigned char)pBuffer[i];
- szGetMsg[ctrlNum+2] = '1';
- }
- for(int j = 0; j < 6; j ++)
- {
- szGetMsg[63+j] = pBuffer[alarmNum+3+j] + 48;
- }
- //LOG4C((LOG_NOTICE," 火警情况 "));
- }
- else if (2 == type)
- {
- szGetMsg[1] = 50;
- szGetMsg[2] = alarmNum +48;
- for (int i=3; i< (alarmNum + 3); i++)
- {
- int ctrlNum = (int)(unsigned char)pBuffer[i];
- szGetMsg[ctrlNum+2] = '2';
- }
- for(int j=0; j<6; j++)
- {
- szGetMsg[alarmNum+3+j] = pBuffer[alarmNum+3+j] + 48;
- }
- //LOG4C((LOG_NOTICE," 故障情况 "));
- }
- //LOG4C_HEX_DUMP((LOG_NOTICE, pBuffer, sizeof(pBuffer)));
- memset(pBuffer,0,sizeof(pBuffer));
- memcpy(pBuffer,szGetMsg,sizeof(szGetMsg));
- SetJB1501A64P_1VarMsg(nAddr, szCmd, pBuffer);
- }
- else
- SetJB1501A64P_7VarMsg(nAddr, szCmd, pBuffer);
- m_dwOnlineTick[nAddr - 1] = GetTickCount();
- // 设置串口等待事件为有信号;
- SetEvent( m_hSemComm );
- if( pBuffer != NULL) {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- int CJB1501A64P::WriteRequestStatus(
- char szCmd[MAX_CMD], // 命令
- CCommProcess *pComm, //串口对象指针
- char chSendMsg[JB1501A64P_SEND_MSG] //发送Buffer
- )
- {
- #if DEBUG_JB1501A64P
- 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_JB1501A64P_COM_WRITE_DATA;
- }
- #endif
- return 0;
- }
- int CJB1501A64P::WriteResponseStatus(
- CCommProcess *pComm, //串口对象指针
- int nAddr, // 设备地址
- char szCmd[MAX_CMD], // 命令
- char szMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH] // 变量数据类型
- )
- {
- #if DEBUG_JB1501A64P
- int nReceiveLen = 0;
- int nProcessLen = 0;
- int nReadLen = 0;
-
- nReceiveLen = JB1501A64P_RECEIVE_MSG;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if( nReadLen <= 0) {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_JB1501A64P_COM_READ_NO_DATA;
- }
- // 设置串口等待事件为有信号
- SetEvent( m_hSemComm );
- if( pBuffer != NULL){
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- void CJB1501A64P::SimulationCommData(void)
- {
- /*cid2 =42 :7E 32 31 30 31 36 30 30 30 34 30 30 43
- 30 30 44 45 30 32 42 43 30 30 30 30 46 42 30 46 0D */
- BYTE pBuffer[70] = {0};
- pBuffer[0] = 0x01 ;
- pBuffer[1] = 0x01 ;
- pBuffer[2] = 0x07 ;
- pBuffer[3] = 0x01 ;
- pBuffer[4] = 0x02 ;
- pBuffer[5] = 0x03 ;
- pBuffer[6] = 0x04 ;
- pBuffer[7] = 0x05 ;
- pBuffer[8] = 0x06 ;
- pBuffer[9] = 0x07 ;
- pBuffer[10] = 0x37 ;
- pBuffer[11] = 0x37 ;
- pBuffer[12] = 0x37 ;
- pBuffer[13] = 0x37 ;
- pBuffer[14] = 0x30 ;
- pBuffer[15] = 0x30 ;
- pBuffer[16] = 0x30 ;
- pBuffer[17] = 0x30 ;
- pBuffer[18] = 0x30 ;
- pBuffer[19] = 0x30 ;
- pBuffer[20] = 0x30 ;
- pBuffer[21] = 0x30 ;
- pBuffer[22] = 0x30 ;
- pBuffer[23] = 0x30 ;
- pBuffer[24] = 0x30 ;
- pBuffer[25] = 0x30 ;
- pBuffer[26] = 0x30 ;
- pBuffer[27] = 0x30 ;
- pBuffer[28] = 0x30 ;
- pBuffer[29] = 0x30 ;
- pBuffer[30] = 0x30 ;
- pBuffer[31] = 0x30 ;
- pBuffer[32] = 0x30 ;
- pBuffer[33] = 0x30 ;
- pBuffer[34] = 0x30 ;
- pBuffer[35] = 0x30 ;
- pBuffer[36] = 0x30 ;
- pBuffer[37] = 0x30 ;
- pBuffer[38] = 0x30 ;
- pBuffer[39] = 0x30 ;
- pBuffer[40] = 0x30 ;
- pBuffer[41] = 0x30 ;
- pBuffer[42] = 0x30 ;
- pBuffer[43] = 0x30 ;
- pBuffer[44] = 0x30 ;
- pBuffer[45] = 0x30 ;
- pBuffer[46] = 0x30 ;
- pBuffer[47] = 0x30 ;
- pBuffer[48] = 0x30 ;
- pBuffer[49] = 0x30 ;
- pBuffer[50] = 0x30 ;
- pBuffer[51] = 0x30 ;
- pBuffer[52] = 0x30 ;
- pBuffer[53] = 0x30 ;
- pBuffer[54] = 0x30 ;
- pBuffer[55] = 0x30 ;
- pBuffer[56] = 0x30 ;
- pBuffer[57] = 0x30 ;
- pBuffer[58] = 0x30 ;
- pBuffer[59] = 0x30 ;
- pBuffer[60] = 0x30 ;
- pBuffer[61] = 0x30 ;
- pBuffer[62] = 0x30 ;
- pBuffer[63] = 0x07 ;
- pBuffer[64] = 0x07 ;
- pBuffer[65] = 0x07 ;
- pBuffer[66] = 0x07 ;
- pBuffer[67] = 0x07 ;
- pBuffer[68] = 0x07 ;
- pBuffer[69] = 0xFF ;
- int type = pBuffer[1] ;
- int alarmNum = pBuffer[2] ;
- char szGetMsg[100] = {0};
- memcpy(szGetMsg,"10<000000000000000000000000000000000000000000000000000000000000000000.",sizeof("10<000000000000000000000000000000000000000000000000000000000000000000."));
- if (0 == type)
- {
- for(int j=0; j < 6; j++)
- {
- if ( pBuffer[2 + j] == 0x07 )
- pBuffer[2 + j] = '7';
- szGetMsg[63+j] = pBuffer[2+j];
- }
- //memcpy((char*)&szGetMsg[2],(char*)&pBuffer[2],6);
- }
- else if (1 == type) {
- szGetMsg[1] = 49;
- szGetMsg[2] = alarmNum +48;
- for (int i=3; i< (alarmNum + 3); i++) {
- int ctrlNum = (unsigned char)pBuffer[i];
- szGetMsg[ctrlNum+3] = '1';
- }
- for(int j=0; j<6; j++) {
- szGetMsg[63+j] = pBuffer[alarmNum+3+j];
- }
- }
- else if (2 == type)
- {
- szGetMsg[1] = 2;
- szGetMsg[2] = alarmNum +48;
- for (int i=3; i< (alarmNum + 3); i++) {
- int ctrlNum = (int)(unsigned char)pBuffer[i];
- szGetMsg[ctrlNum+3] = 2 + 48;
- }
- for(int j=0; j<6; j++) {
- szGetMsg[alarmNum+3+j] = pBuffer[alarmNum+3+j];
- }
- }
- memcpy(m_szJB1501A64P_1Msg, szGetMsg, sizeof(szGetMsg));
- }
- WORD CJB1501A64P::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;
- }
-
- }
|