// Modbus_C.cpp: implementation of the CModbus_TrainC class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Modbus_TRAINc.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CModbus_TrainC::~CModbus_TrainC() { } void CModbus_TrainC::Init(S_PROTOCOLCFG * pcfg ) { PRawCtrl = pcfg->PRawCtrl; pRxBuf = pcfg->pRxBuf; pTxBuf = pcfg->pTxBuf; pCmdMem = pcfg->pCmdMem; pRtu = pcfg->pRtu; pLink = pcfg->pLink; pTable = pcfg->pTable; pZfDataCtrl = pcfg->pZfDataCtrl; // pHis = pcfg->pHis; cnt_sequence=0; m_LastSendTime = GetNowSecond(); //初始化当前通信正常 m_current_com_status = DEF_MSG_COMMDOWN ; //DEF_MSG_COMMUP代表正常,DEF_MSG_COMMDOWN代表不正常 pLink->SetCommStatus(CMST_RX_CNT); m_ResponseFlag = 0; m_initFlag = 1; } //规约发送过程 sint32 CModbus_TrainC::TxProc() { /* if(m_ResponseFlag == 1) { } */ return 1; } void CModbus_TrainC::initTrain() { S_VstData vst; vst.DataType = DEF_VST_TRAIN; vst.BuffLen = 12; //memset(vst.VstBuf,0,12); int i=0; for(i=0; i<12; i++) { vst.VstBuf[i] = 0; } for(i=1;i<100;i++) { vst.VstBuf[0] = i; PRawCtrl->PutAVstData(vst); PrintLog(LOG_INFORMATION,"初始化第%d辆列车数据",i); } m_initFlag = 0; } sint32 CModbus_TrainC::MsgProc(sint32 msg) { if(msg == DEF_MSG_COMMDOWN) { m_current_com_status = DEF_MSG_COMMDOWN; } return 1; } //规约接收函数 sint32 CModbus_TrainC::RxProc() { if((m_initFlag == 1)||(m_current_com_status == DEF_MSG_COMMDOWN)) { initTrain(); m_current_com_status = DEF_MSG_COMMUP; } sint32 rtuno = pLink->GetRtuNo(); unsigned char rtuaddr = pRtu->GetRtuAddr(rtuno); int nowtime=GetNowSecond(); uint32 buflen = pRxBuf->GetReadableSize(); E_RAW_ComStat commstatus = pLink->GetCommStatus(); if ( commstatus==CMST_RX_CNT && (nowtime-m_LastSendTime) >= 20) { //超时不重发,直接跳到下一个终端 pRxBuf->Move(buflen); pLink->RegisterFrm(FRAME_RX_TIMEOUT); pLink->SetCommStatus(CMST_RX_CNT); PrintLog(LOG_VIOLATION, " 数据超时\n"); m_LastSendTime = GetNowSecond(); return -1; } uint8 buf[1024]; uint32 datanum =0; char stmp[90] = { 0 }; int i = 0; // if(0= 21) { if(buflen>1024)buflen=1024; pRxBuf->Read(buf, buflen, DEF_BUFF_NOMOVE ); if((buf[0]==0xEB)&&(buf[1]==0x90)&&buf[7]==66)//请求数据train { S_VstData vst; int datalen =(buf[4]<<8) + buf[5]; //按功能码区分数据 PrintLog(LOG_INFORMATION,"取到列车数据长度%d",datalen); vst.DataType = DEF_VST_TRAIN; vst.BuffLen = buf[8]; buf[9] %= 100; for(i=0; iPutAVstData(vst); m_ResponseFlag = 1; //PrintLog(LOG_INFORMATION,"收到列车数据"); PrintLog(LOG_INFORMATION,"收到列车数据序列号%d:列车号:%d,方向:%d,边线号:%d,偏移量:%dCM,终点站号:%d,描述码%d,阻塞%d",(buf[2]*100+buf[3]),buf[9],buf[10],buf[11]+(buf[12]<<8),(buf[16]<<24)+(buf[15]<<16)+(buf[14]<<8)+buf[13],buf[17],(buf[20]<<8)+buf[19],buf[9+9]); // PrintLog(LOG_INFORMATION,"收到列车数据序列号%d:列车号:%d,方向:%d,边线号:%d,偏移量:%dCM,终点站号:%d,描述码%d,阻塞%d",(buf[2]*100+buf[3]),buf[9],buf[10],buf[11],(buf[14]<<8)+buf[13],buf[17-2],(buf[20-2]<<8)+buf[19-2],buf[9+9-2]); pLink->RegisterFrm(FRAME_RX_SUC); pRxBuf->Move(datalen+6); buflen = pRxBuf->GetReadableSize(); SendSomething(); } else{ memset(stmp,0,90); if (buflen > 30)buflen = 30; for (i = 0; i < buflen; i++) { sprintf(stmp + 3 * i, "%02x ", buf[i]); } PrintLog(LOG_INFORMATION, "收到非列车数据,移除数据长度%d:%s", stmp); pRxBuf->Move(1); } } return 1; } void CModbus_TrainC::SendSomething() { int bytelen = 0; sint32 rtuno = pLink->GetRtuNo(); unsigned char rtuadd = pRtu->GetRtuAddr(rtuno); if(cnt_sequence>=65535) cnt_sequence=0; else cnt_sequence++; unsigned char buf[1024]; int buflen =0; buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier buf[buflen]=cnt_sequence%0x100; buflen++;//L buf[buflen]=0; buflen++; buf[buflen]=0; buflen++; //buf[buflen]=HIBYTE(bytelen); buflen++; //buf[buflen]=LOBYTE(bytelen); buflen++; buf[buflen]=rtuadd; buflen++; buf[buflen]=0; buflen++;//fun buf[buflen] = 2; buflen++;bytelen++; //随便发点东西 buf[buflen]=cnt_sequence/0x100;buflen++;//H Transaction Identifier buf[buflen]=cnt_sequence%0x100; buflen++;//L buf[4]=HIBYTE(bytelen); buf[5]=LOBYTE(bytelen); // pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)buf,buflen); pTxBuf->Write(buf,buflen); m_LastSendTime=GetNowSecond(); m_ResponseFlag = 0; // pLink->SetCommStatus(CMST_NORMAL); pLink->SetCommStatus(CMST_RX_CNT); } sint32 CModbus_TrainC::GetNowSecond() { CSeTime SE_T; TCriterionTime tmptime;//sint32 SE_T.GetNow(&tmptime); return (sint32)tmptime; } //规约对象创建函数 #ifdef __unix extern "C" CProtocol* CreateProtocol(char *defpara) #else extern "C" __declspec(dllexport) CProtocol* CreateProtocol(char *defpara) #endif { CProtocol *pEpv = new CModbus_TrainC; return pEpv; }