| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934 |
- //////////////////////////////////////////////////////////////////////////////
- ////// //////
- ////// 文 件: Sda_ii.cpp //////
- ////// 作 者: wangjianfeng //////
- ////// 创建时间: //////
- ////// 说 明: Sda_ii协议 //////
- ////// //////
- ////// 修改时间:2010-08-16 //////
- ////// 修改说明:新版本 //////
- ////// //////
- //////////////////////////////////////////////////////////////////////////////
- #include "StdAfx.h"
- #include "CommProcess.h"
- #include "Global.h"
- #include ".\sda_ii.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- CSda_ii::CSda_ii(
- 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_sz4CMsg[i], 0, sizeof(m_sz4CMsg[i]));
- memset(m_sz52Msg[i], 0, sizeof(m_sz52Msg[i]));
- m_devOnline[i] = TRUE;
- m_dwOnlineTick[i] = 0;
- }
- }
- CSda_ii::~CSda_ii()
- {
- #if IS_USE_READMSG_CS
- DeleteCriticalSection( &m_csReadMsg );
- #endif
- MTVERIFY( CloseHandle( m_hSemComm ) );
- CloseComm();
- }
- BOOL CSda_ii::AirSdaOpenComm(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 );
- if (bResult == FALSE)
- {
- //LOG4C((LOG_NOTICE,"依米康OpenCommFail"));
- }
- return bResult;
- }
- //请求数据
- int CSda_ii::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,Funid = 0,startbit = 0,endbit = 0;
- char SendCmd[SEND_MSG] = {0};
- char szType[TYPE_LENGTH] = {0};
- GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit, endbit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL )
- {
- //LOG4C((LOG_NOTICE,"串口句柄无效"));
- return -1;
- }
- int nRet = -1;
- if (
-
- (strlen(m_sz4CMsg[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
-
- ))
- ||
- (strlen(m_sz52Msg[nAddr-1]) == 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-1") == 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
- )
- {
- nRet = GetDeviceParam( nAddr,pComm, SendCmd, szCmd, szMsg, nIndex, nLen, szType,Funid );
- 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 = Get4CVarMsg(szCmd, szMsg, nIndex, nLen, szType,startbit, endbit,nAddr);
- nRet = Get52VarMsg(szCmd, szMsg, nIndex, nLen, szType,nAddr);
- return nRet;
- }
- int CSda_ii::GetDeviceParam(
- int nAddr,
- CCommProcess *pComm, //串口对象指针
- char SendCmd[SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szRecvMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH], // 变量数据类型
- int Funid
- )
- {
- int nRet = -1;
- nRet = RequestReadStatus(nAddr,pComm, SendCmd,Funid);
- if( nRet != 0 )
- {
- return nRet;
- }
- nRet = ResponseReadStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr);
- return nRet;
- }
- int CSda_ii::Get4CVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startbit, int endbit,int nAddr)
- {
- int nRet = 0;
- if(
- strcmp(szCmd, "cmd-1") == 0 ||
- strcmp(szCmd, "cmd-2") == 0
- //温湿度;
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen);
- GetDataDi(szRecvMsg);
-
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- if(
- 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
- //9~36 告警值;
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen);//命令对应的需要的数据szRecvMsg;
- //GetDataDi(szRecvMsg);
- GetByteHex(szRecvMsg);
- int iStatus = ::atoi(szRecvMsg);
- /*LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, szRecvMsg = %s" ,szCmd ,iStatus, szRecvMsg));*/
- GetBitStatu(iStatus,szRecvMsg,startbit,endbit);
- LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, 位信息 = %s" ,szCmd ,iStatus, szRecvMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- int CSda_ii::Get52VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH],int nAddr)
- {
- int nRet = 0;
- if(
- 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(szRecvMsg, m_sz52Msg[nAddr - 1] + nIndex ,nLen);
- //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg));
- GetDataDi(szRecvMsg);
- //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- nRet = 0;
- }
- return nRet;
- }
- // 发送设置设备参数请求
- int CSda_ii::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 nIndex = 0, nLen = 0,Funid = 0 ,startbit = 0,endbit = 0;
- char SendCmd[SEND_MSG] = {0};
- char szType[TYPE_LENGTH] = {0};
- GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit ,endbit);
- CCommProcess *pComm = FindComm(nCommPort);
- if( pComm == NULL ) return -1;
- int nRet = -1;
- if (strcmp(szCmd,"cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 )
- {
- nRet = SetDeviceParam( nAddr,pComm, SendCmd, szCmd, SendCmd, nIndex, nLen, szType,Funid );
- if( nRet != 0 ) return nRet;
- }
- return 0;
- }
- int CSda_ii::SetDeviceParam(
- int nAddr,
- CCommProcess *pComm, //串口对象指针
- char SendCmd[SEND_MSG], //发送Buffer
- char szCmd[MAX_CMD], // 命令
- char szRecvMsg[VAR_MSG], // 接收Buffer
- int &nIndex, // 变量索引,针对接收Buffer而言
- int &nLen, // 变量长度
- char szType[TYPE_LENGTH], // 变量数据类型
- int Funid)
- {
- int nRet = -1;
- nRet = RequestWriteStatus(nAddr,pComm, SendCmd,Funid);
- if( nRet != 0 )
- {
- return nRet;
- }
- nRet = ResponseWriteStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr);
- return nRet;
- }
- int CSda_ii:: RequestWriteStatus( int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid)
- {
- #if DEBUG_SDA
- RESPONSE_4CSTRUCT RequestPara;
- int iLen = sizeof(RequestPara);
- memset( &RequestPara, 0, iLen );
- RequestPara.SOI = 0x02;
- if (strcmp(SendCmd,"4F") == 0 )
- {
- RequestPara.CMD = 0x4F;
- }
- else if (strcmp(SendCmd, "46") == 0)
- {
- RequestPara.CMD = 0x46;
- }
- RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);
- RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
- RequestPara.EOI = 0x03;
- if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
- {
- int nDataLen = (int)sizeof(RequestPara);
- ResetEvent( m_hSemComm );
- int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
- Sleep(3000);
- if( nResult != nDataLen )
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_Sda_ii_COM_INVALIDRES;
- }
- }
- else
- return ERR_CODE_Sda_ii_COM_BUSY;
- #endif
- return 0;
- }
- int CSda_ii::ResponseWriteStatus(
- CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char SendCmd[VAR_MSG],
- int &nIndex,
- int &nLen,
- char szType[TYPE_LENGTH],
- int nAddr
- )
- {
- #if DEBUG_SDA
- int nReceiveLen = 6;
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- int nReadLen = 0;
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if( nReadLen <= 0)
- {
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_Sda_ii_COM_READ_NO_DATA;
- }
- m_dwOnlineTick[nAddr -1] = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete[] pBuffer;
- pBuffer = NULL;
- }
- #endif
- return 0;
- }
- int CSda_ii::ResponseReadStatus( CCommProcess *pComm,
- char szCmd[MAX_CMD],
- char SendCmd[VAR_MSG],
- int &nIndex,
- int &nLen,
- char szType[TYPE_LENGTH],
- int nAddr)
- {
- #if DEBUG_SDA
- int nReceiveLen = 0;
- if (stricmp(SendCmd,"4C") == 0)
- {
- nReceiveLen = 25;
- }
- else if (strcmp(SendCmd ,"52") == 0)
- {
- nReceiveLen = 13;
- }
-
- if (nReceiveLen == 0)
- {
- LOG4C((LOG_NOTICE, "nReceiveLen = %d ", nReceiveLen));
- return -1;
- }
- char *pBuffer = new char[ nReceiveLen ];
- memset(pBuffer, 0, nReceiveLen);
- int nReadLen = 0;
- nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
- if ( !GetCheckSum(pBuffer, nReceiveLen -1 ))
- {
- //LOG4C((LOG_NOTICE, "ResponseReadStatus::请求%s校验码校验出错", szCmd));
- SetEvent(m_hSemComm);
- if (pBuffer !=NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_Sda_ii_COM_CHKSUM_LOST; //校验码校验出错
- }
- if( nReadLen <= 0)
- {
- //LOG4C((LOG_NOTICE, "ResponseReadStatus::串口没有读到数据"));
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- return ERR_CODE_Sda_ii_COM_READ_NO_DATA;
- }
- Set4CVarMsg( szCmd, pBuffer,nAddr);
- Set52VarMsg( szCmd, pBuffer,nAddr);
- m_dwOnlineTick[nAddr -1] = GetTickCount();
- SetEvent( m_hSemComm );
- if( pBuffer != NULL)
- {
- delete []pBuffer;
- pBuffer = NULL;
- }
- #else
- SimulationCommData();
- #endif
- return 0;
- }
- int CSda_ii::RequestReadStatus(int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid)
- {
- #if DEBUG_SDA
- if (stricmp(SendCmd,"4C") == 0)
- {
- RESPONSE_4CSTRUCT RequestPara;
- int iLen = sizeof(RequestPara);
- memset( &RequestPara, 0, iLen );
- RequestPara.SOI = 0x02;
- RequestPara.CMD = 0x4C;
- RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);;
- RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
- RequestPara.EOI = 0x03;
- 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 != nDataLen )
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_Sda_ii_COM_INVALIDRES;
- }
- }
- else
- return ERR_CODE_Sda_ii_COM_BUSY;
- }
- else if (strcmp(SendCmd,"52") == 0)
- {
- RESPONSE_53STRUCT RequestPara;
- int iLen = sizeof(RequestPara);
- memset( &RequestPara, 0, iLen );
- RequestPara.SOI = 0x02;
- RequestPara.CMD = 0x52;
- RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);;
- RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
- RequestPara.INDEX[0] = ByteToAscii((Funid >> 4) & 0x0f);;
- RequestPara.INDEX[1] = ByteToAscii(Funid & 0x0f);
- RequestPara.EOI = 0x03;
-
- 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 != nDataLen )
- {
- SetEvent( m_hSemComm );
- return ERR_CODE_Sda_ii_COM_INVALIDRES;
- }
- }
- else
- {
- return ERR_CODE_Sda_ii_COM_BUSY;
- }
- }
-
- #endif
- return 0;
- }
- void CSda_ii::Set4CVarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr)
- {
- 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 ||
- 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_sz4CMsg[nAddr-1],pBuff,sizeof(m_sz4CMsg[nAddr-1]));
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- void CSda_ii::Set52VarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr)
- {
- if(
- 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
- )
- {
- #if IS_USE_READMSG_CS
- EnterCriticalSection( &m_csReadMsg );
- #endif
- memcpy(m_sz52Msg[nAddr-1],pBuff,sizeof(m_sz52Msg[nAddr-1]));
-
- #if IS_USE_READMSG_CS
- LeaveCriticalSection(&m_csReadMsg);
- #endif
- }
- }
- //获取校验码;
- BOOL CSda_ii::GetCheckSum(char *pBuf, int iLen)
- {
- WORD iSum = 0;
- char chDest[3] = {0};
- for(int i=1; i< iLen - 2 ; i++)//求和
- {
- iSum += pBuf[i];
- }
- char chTemp[3] = {0};
- chTemp[0] = pBuf[iLen-2];
- chTemp[1] = pBuf[iLen-1];
- WORD iCompliment = iSum;
- chDest[0] = ByteToAscii((iSum >> 4) & 0x0f);
- chDest[1] = ByteToAscii(iSum & 0x0f);
- if ( chDest[0] == chTemp[0] && chDest[1] == chTemp[1])
- {
- return TRUE;
- }
- return FALSE;
- }
- int CSda_ii::GetULFromIni(
- char szPath[MAX_PATH], //服务器程序所在目录
- char szIniName[MAX_PATH], //配置文件名
- char szCmd[MAX_CMD], //命令
- char SendCmd[MAX_CMD], //发送Buffer
- char nType[MAX_CMD],
- int &nIndex,
- int &nLen,
- int &Funid,
- int &startbit,
- int &endbit
- )
- {
- CHAR szFile[MAX_PATH + 1] = "";
- wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
- GetPrivateProfileString(szCmd, "SendCmd", "", SendCmd, 10, szFile);
- SendCmd[strlen(SendCmd)] = '\0';
- GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
- nType[strlen(nType)] = '\0';
- Funid = GetPrivateProfileInt(szCmd, "Funid", 0, szFile);
- nIndex = GetPrivateProfileInt(szCmd, "index", 0, szFile);
- nLen = GetPrivateProfileInt(szCmd, "len", 0, szFile);
- startbit = GetPrivateProfileInt(szCmd, "startbit", 0 ,szFile);
- endbit = GetPrivateProfileInt(szCmd, "endbit", 0 ,szFile);
- return 0;
- }
- //获取bit位状态值 ;
- void CSda_ii::GetBitStatu(int iVaule,char *bytV,int startbit,int endbit)
- {
- BYTE bytValue;
- bytValue = iVaule;
- int ByteBit = 0;
- char tempbyte[9] = {0};
- if (startbit == endbit)
- {
- switch(startbit)
- {
- case 0:
- ByteBit = bytValue & 0x01;
- break;
- case 1:
- ByteBit = (bytValue & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue & 0x80) / 0x80;
- break;
- }
- _snprintf(bytV,sizeof(bytV),"%d",ByteBit);
- }
- else
- {
- int tempbit = startbit;
- int TiLen = endbit - startbit;
- for (int i = 0; i < TiLen;i++)
- {
- tempbit = tempbit + i;
- switch(tempbit)
- {
- case 0:
- ByteBit = bytValue & 0x01;
- break;
- case 1:
- ByteBit = (bytValue & 0x02) / 0x02 ;
- break;
- case 2:
- ByteBit = (bytValue & 0x04) / 0x04 ;
- break;
- case 3:
- ByteBit = (bytValue & 0x08) / 0x08 ;
- break;
- case 4:
- ByteBit = (bytValue & 0x10) / 0x10 ;
- break;
- case 5:
- ByteBit = (bytValue & 0x20) / 0x20 ;
- break;
- case 6:
- ByteBit = (bytValue & 0x40) / 0x40 ;
- break;
- case 7:
- ByteBit = (bytValue & 0x80) / 0x80;
- break;
- }
- _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit);
- }
- strcpy(bytV,tempbyte);
- //bytValue = tempbyte;
- }
- }
- //十六进制转换十进制
- void CSda_ii::GetDataDi(char *szMsg)
- {
- char chDest[2] = {0};
- chDest[0] = szMsg[0];
- chDest[1] = szMsg[1];
- //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg));
- int iConvMsg = 0;
- int iszMsg_1 = 0 ,iszMsg_2 = 0;
- //16进制转换十进制
- if ((szMsg[2] >= 'A')&&(szMsg[2] <= 'F'))
- {
- iszMsg_1 = szMsg[2] - 'A' + 10;
- }
- else if ((szMsg[2] >= 'a')&&(szMsg[2] <= 'f'))
- {
- iszMsg_1 = szMsg[2] - 'a' + 10;
- }
- else if ((szMsg[2] >= '0')&&(szMsg[2] <= '9'))
- {
- iszMsg_1 =szMsg[2] - '0';
- }
- if ((szMsg[3] >= 'A')&&(szMsg[3] <= 'F'))
- {
- iszMsg_2 = szMsg[3] - 'A' + 10;
- }
- else if ((szMsg[3] >= 'a')&&(szMsg[3] <= 'f'))
- {
- iszMsg_2 = szMsg[3] - 'a' + 10;
- }
- else if ((szMsg[3] >= '0')&&(szMsg[3] <= '9'))
- {
- iszMsg_2 = szMsg[3] - '0';
- }
- iConvMsg = iszMsg_1*16 + iszMsg_2;
- itoa(iConvMsg,szMsg,10);
- //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg));
- }
- //十六进制转换十进制
- void CSda_ii::GetByteHex(char *szMsg)
- {
- char chDest[2] = {0};
- chDest[0] = szMsg[0];
- chDest[1] = szMsg[1];
- //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg));
- int iConvMsg = 0;
- int iszMsg_1 = 0 ,iszMsg_2 = 0;
- //16进制转换十进制
- if ((szMsg[0] >= 'A')&&(szMsg[0] <= 'F'))
- {
- iszMsg_1 = szMsg[0] - 'A' + 10;
- }
- else if ((szMsg[0] >= 'a')&&(szMsg[0] <= 'f'))
- {
- iszMsg_1 = szMsg[0] - 'a' + 10;
- }
- else if ((szMsg[0] >= '0')&&(szMsg[0] <= '9'))
- {
- iszMsg_1 =szMsg[0] - '0';
- }
- if ((szMsg[1] >= 'A')&&(szMsg[1] <= 'F'))
- {
- iszMsg_2 = szMsg[1] - 'A' + 10;
- }
- else if ((szMsg[1] >= 'a')&&(szMsg[1] <= 'f'))
- {
- iszMsg_2 = szMsg[1] - 'a' + 10;
- }
- else if ((szMsg[1] >= '0')&&(szMsg[1] <= '9'))
- {
- iszMsg_2 = szMsg[1] - '0';
- }
- iConvMsg = iszMsg_1*16 + iszMsg_2;
- itoa(iConvMsg,szMsg,10);
- //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg));
- }
- void CSda_ii::SimulationCommData(void)
- {
- m_sz4CMsg[0][0]=0x02 ;
- m_sz4CMsg[0][1]=0x4c;
- m_sz4CMsg[0][2]=0x30;
- m_sz4CMsg[0][3]=0x31;
- m_sz4CMsg[0][4]=0x30;
- m_sz4CMsg[0][5]=0x30;
- m_sz4CMsg[0][6]=0x43;
- m_sz4CMsg[0][7]=0x33;
- m_sz4CMsg[0][8]=0x30;
- m_sz4CMsg[0][9]=0x30;
- m_sz4CMsg[0][10]=0x32;
- m_sz4CMsg[0][11]=0x31;
- //.状态;
- m_sz4CMsg[0][12]=0x30;
- m_sz4CMsg[0][13]=0x42;
- m_sz4CMsg[0][14]=0x30;
- m_sz4CMsg[0][15]=0x30;
- m_sz4CMsg[0][16]=0x30;
- m_sz4CMsg[0][17]=0x31;
- m_sz4CMsg[0][18]=0x30;
- m_sz4CMsg[0][19]=0x30;
- m_sz4CMsg[0][20]=0x30;
- m_sz4CMsg[0][21]=0x30;
- m_sz4CMsg[0][22]=0x33;
- m_sz4CMsg[0][23]=0x39;
- m_sz4CMsg[0][24]=0x03;
- }
|