#include "modbus_tcpc_ex.h" #include "se_btl.h" #include "se_log.h" #include #include #include #include #include #include #include #include "stdtostring.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[] = __FILE__; #define new DEBUG_NEW #endif using namespace std; CModbusTcpc::~CModbusTcpc() { } CModbusTcpc::CModbusTcpc() { } void CModbusTcpc::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; mapnum = 0; mapIndex = 0; Policy = 0; WillSendType = 0; cnt_sequence = 0; m_LastSendTime = 0; m_RcvTimeOuts = pLink->GetRxTimeouts(); if (m_RcvTimeOuts <= 0) m_RcvTimeOuts = 10; //em_daq.exe启动到正常 耗时很慢,进行配置表读取操作的滞后 ljk20221026 sleeptime = pLink->GetAllDataScanInterval(); readcfgflag = 0; sint32 rtuno = pLink->GetRtuNo(); // m_SyncTime = pRtu->GetSyncTimeInt(rtuno)*60; m_SyncTime = pRtu->GetSyncTimeInt(rtuno); duquIndex = 0; visitednum = 0; cmdlength = 0; err = false; sleepstarttime = GetNowMilliSecond(); PrintLog(1, "规约初始化完成!"); } void CModbusTcpc::ReadCfg() { char* pathvar; pathvar = getenv(SE_EMS_RUN_DIR); if (pathvar == NULL){ PrintLog(LOG_ERROR, "未找到环境变量"); err = true; return; } std::string route; route.append(pathvar); std::string str = to_string(pLink->GetRtuNo()); route.append("/cfg/modbus_tcpex_rtu"); route.append(str); route.append(".csv"); std::string AI = "AI"; std::string DI = "DI"; std::string m_PI = "PI"; std::string AO = "AO"; std::string DO = "DO"; std::string VA = "VA"; std::string int32 = "int32"; std::string int16 = "int16"; std::string float32 = "float"; std::string uint32 = "uint32"; std::string uint16 = "uint16"; std::string rv_int16 = "rv_int16"; std::string rv_float = "rv_float"; std::string rv_uint16 = "rv_uint16"; std::string sw_int32 = "sw_int32"; std::string sw_uint32 = "sw_uint32"; std::string sw_float = "sw_float"; std::string rv_int32 = "rv_int32"; std::string rv_uint32 = "rv_uint32"; std::string rv_sw_int32 = "sw_rv_int32"; std::string rv_sw_uint32 = "sw_rv_uint32"; std::string rv_sw_float = "sw_rv_float"; std::string rv_bit = "rv_bit"; std::string bit = "bit"; duqulength = 0; maplength = 0; cmdlength = 0; ifstream inf; inf.open(route.c_str(), ios::in); if (!inf.is_open()){ PrintLog(LOG_ERROR, "未成功打开配置文件"); err = true; return; } std::string lineStr; vector > strArray; //将配置文件存入二维strArray while (getline(inf, lineStr)) { stringstream ss(lineStr); std::string str; vector lineArray; while (getline(ss, str, ',')) { lineArray.push_back(str); } strArray.push_back(lineArray); } maplist = new map[strArray.size()]; //存入配置文件中数据类型为DI、AI、PI、VA的行 结构体中内容包括:地址、采集序号、类型、值类型、数据结构体、开始位置、结束位置、是否访问过标志。 cmdlist = new cmd[strArray.size()]; //存入配置文件中数据类型为DO、AO的行 结构体中内容包括:地址、采集序号、类型、值类型、组号、功能码、开始位置、结束位置、功能码为16或6且类型为DO的标志。 duqu = new sint32 *[strArray.size()]; //存入配置文件中每组第一行 注:每组第一行有功能号和时间间隔 存入内容为:地址、寄存器数量(需要计算)、组号、功能码、时间间隔。 int i; for (i = 0; i < strArray.size(); i++) duqu[i] = new sint32[5]; duqu[duqulength][0] = atoi(strArray[1][0].c_str()) - 1; duqu[duqulength][1] = 1; duqu[duqulength][2] = atoi(strArray[1][6].c_str()); duqu[duqulength][3] = atoi(strArray[1][7].c_str()); duqu[duqulength][4] = atoi(strArray[1][8].c_str()); maplist[maplength].address = atoi(strArray[1][0].c_str()) - 1; if (strArray[1][4].c_str() == DI) { maplist[maplength].r_type = 1; if (strArray[1][5].c_str() == bit){ maplist[maplength].m_type = 1; } if (strArray[1][5].c_str() == rv_bit){ maplist[maplength].m_type = 2; } } if (strArray[1][4].c_str() == AI){ if (strArray[1][5].c_str() == int16){ maplist[maplength].r_type = 2; maplist[maplength].m_type = 3; } if (strArray[1][5].c_str() == uint16){ maplist[maplength].r_type = 1; maplist[maplength].m_type = 3; } if (strArray[1][5].c_str() == rv_int16){ maplist[maplength].r_type = 2; maplist[maplength].m_type = 4; } if (strArray[1][5].c_str() == rv_uint16){ maplist[maplength].r_type = 1; maplist[maplength].m_type = 4; } if (strArray[1][5].c_str() == int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 5; } if (strArray[1][5].c_str() == uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 5; } if (strArray[1][5].c_str() == sw_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 6; } if (strArray[1][5].c_str() == sw_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 6; } if (strArray[1][5].c_str() == rv_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 7; } if (strArray[1][5].c_str() == rv_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 7; } if (strArray[1][5].c_str() == rv_sw_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 8; } if (strArray[1][5].c_str() == rv_sw_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 8; } if (strArray[1][5].c_str() == float32){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 5; } if (strArray[1][5].c_str() == sw_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 6; } if (strArray[1][5].c_str() == rv_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 7; } if (strArray[1][5].c_str() == rv_sw_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 8; } } if (strArray[1][4].c_str() == m_PI) { maplist[maplength].m_type = 9; if (strArray[1][5].c_str() == uint16) maplist[maplength].r_type = 1; if (strArray[1][5].c_str() == int16) maplist[maplength].r_type = 2; if (strArray[1][5].c_str() == float32) maplist[maplength].r_type = 3; if (strArray[1][5].c_str() == int32) maplist[maplength].r_type = 4; if (strArray[1][5].c_str() == uint32) maplist[maplength].r_type = 5; } if (strArray[1][4].c_str() == VA) { maplist[maplength].m_type = 0; maplist[maplength].r_type = 1; } maplist[maplength].pointnum = atoi(strArray[1][1].c_str()); maplist[maplength].start = atoi(strArray[1][2].c_str()); maplist[maplength].end = atoi(strArray[1][3].c_str()); maplength++; for (i = 2; i < strArray.size(); i++) { if (i >= strArray.size() || i < 0) { break; } if (strArray[i][4].c_str() == AO || strArray[i][4].c_str() == DO) { if (cmdlength != 0 && atoi(strArray[i][6].c_str()) == cmdlist[cmdlength - 1].group) { cmdlist[cmdlength].address = atoi(strArray[i][0].c_str()) - 1; cmdlist[cmdlength].group = atoi(strArray[i][6].c_str()); cmdlist[cmdlength].pointnum = atoi(strArray[i][1].c_str()); cmdlist[cmdlength].m_type = cmdlist[cmdlength - 1].m_type; cmdlist[cmdlength].func = cmdlist[cmdlength - 1].func; if (strArray[i][5].c_str() == int16){ cmdlist[cmdlength].r_type = 2; } if (strArray[i][5].c_str() == uint16){ cmdlist[cmdlength].r_type = 1; } if (strArray[i][5].c_str() == bit){ cmdlist[cmdlength].r_type = 3; } if ((cmdlist[cmdlength].func == 6 || cmdlist[cmdlength].func == 16) && strArray[i][4].c_str() == DO){ cmdlist[cmdlength].va = 1; cmdlist[cmdlength].start = atoi(strArray[i][2].c_str()); cmdlist[cmdlength].end = atoi(strArray[i][3].c_str()); } } else { cmdlist[cmdlength].address = atoi(strArray[i][0].c_str()) - 1; cmdlist[cmdlength].group = atoi(strArray[i][6].c_str()); cmdlist[cmdlength].func = atoi(strArray[i][7].c_str()); cmdlist[cmdlength].pointnum = atoi(strArray[i][1].c_str()); if (atoi(strArray[i][7].c_str()) == 5 || atoi(strArray[i][7].c_str()) == 15) { cmdlist[cmdlength].m_type = 1; } else if (atoi(strArray[i][7].c_str()) == 6 || atoi(strArray[i][7].c_str()) == 16) { cmdlist[cmdlength].m_type = 2; } if (strArray[i][5].c_str() == bit){ cmdlist[cmdlength].r_type = 3; } if (strArray[i][5].c_str() == int16){ cmdlist[cmdlength].r_type = 2; } if (strArray[i][5].c_str() == uint16){ cmdlist[cmdlength].r_type = 1; } if ((cmdlist[cmdlength].func == 6 || cmdlist[cmdlength].func == 16) && strArray[i][4].c_str() == DO){ cmdlist[cmdlength].va = 1; cmdlist[cmdlength].start = atoi(strArray[i][2].c_str()); cmdlist[cmdlength].end = atoi(strArray[i][3].c_str()); } } cmdlength++; continue; } maplist[maplength].address = atoi(strArray[i][0].c_str()) - 1; if (strArray[i][4].c_str() == DI) { maplist[maplength].r_type = 1; if (strArray[i][5].c_str() == bit){ maplist[maplength].m_type = 1; } if (strArray[i][5].c_str() == rv_bit){ maplist[maplength].m_type = 2; } } if (strArray[i][4].c_str() == AI){ if (strArray[i][5].c_str() == int16){ maplist[maplength].r_type = 2; maplist[maplength].m_type = 3; } if (strArray[i][5].c_str() == uint16){ maplist[maplength].r_type = 1; maplist[maplength].m_type = 3; } if (strArray[i][5].c_str() == rv_int16){ maplist[maplength].r_type = 2; maplist[maplength].m_type = 4; } if (strArray[i][5].c_str() == rv_uint16){ maplist[maplength].r_type = 1; maplist[maplength].m_type = 4; } if (strArray[i][5].c_str() == int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 5; } if (strArray[i][5].c_str() == uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 5; } if (strArray[i][5].c_str() == sw_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 6; } if (strArray[i][5].c_str() == sw_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 6; } if (strArray[i][5].c_str() == rv_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 7; } if (strArray[i][5].c_str() == rv_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 7; } if (strArray[i][5].c_str() == rv_sw_int32){ maplist[maplength].r_type = 4; maplist[maplength].m_type = 8; } if (strArray[i][5].c_str() == rv_sw_uint32){ maplist[maplength].r_type = 5; maplist[maplength].m_type = 8; } if (strArray[i][5].c_str() == float32){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 5; } if (strArray[i][5].c_str() == sw_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 6; } if (strArray[i][5].c_str() == rv_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 7; } if (strArray[i][5].c_str() == rv_sw_float){ maplist[maplength].r_type = 3; maplist[maplength].m_type = 8; } } if (strArray[i][4].c_str() == m_PI) { maplist[maplength].m_type = 9; if (strArray[i][5].c_str() == uint16) maplist[maplength].r_type = 1; if (strArray[i][5].c_str() == int16) maplist[maplength].r_type = 2; if (strArray[i][5].c_str() == float32) maplist[maplength].r_type = 3; if (strArray[i][5].c_str() == int32) maplist[maplength].r_type = 4; if (strArray[i][5].c_str() == uint32) maplist[maplength].r_type = 5; } if (strArray[i][4].c_str() == VA) { maplist[maplength].m_type = 0; maplist[maplength].r_type = 1; } maplist[maplength].pointnum = atoi(strArray[i][1].c_str()); maplist[maplength].start = atoi(strArray[i][2].c_str()); maplist[maplength].end = atoi(strArray[i][3].c_str()); if (atoi(strArray[i][6].c_str()) != duqu[duqulength][2]) { duqulength++; duqu[duqulength][0] = atoi(strArray[i][0].c_str()) - 1; duqu[duqulength][1] = 1; duqu[duqulength][2] = atoi(strArray[i][6].c_str()); duqu[duqulength][3] = atoi(strArray[i][7].c_str()); duqu[duqulength][4] = atoi(strArray[i][8].c_str()); if (i < strArray.size() - 1) { if (atoi(strArray[i + 1][6].c_str()) != duqu[duqulength][2] && maplist[maplength].end > 15 && (duqu[duqulength][3] == 3 || duqu[duqulength][3] == 4)) { duqu[duqulength][1] += maplist[maplength].end / 16; } if (atoi(strArray[i + 1][6].c_str()) != duqu[duqulength][2] && maplist[maplength].end > 0 && (duqu[duqulength][3] == 1 || duqu[duqulength][3] == 2)) { duqu[duqulength][1] += maplist[maplength].end; } } else { if (maplist[maplength].end > 15 && duqu[duqulength][3] == 3) { duqu[duqulength][1] += maplist[maplength].end / 16; } if (maplist[maplength].end > 0 && duqu[duqulength][3] == 1) { duqu[duqulength][1] += maplist[maplength].end; } } } else { duqu[duqulength][1] += atoi(strArray[i][0].c_str()) - atoi(strArray[i - 1][0].c_str()); if (i < strArray.size() - 1) { if (atoi(strArray[i + 1][6].c_str()) != duqu[duqulength][2] && maplist[maplength].end > 15 && (duqu[duqulength][3] == 3 || duqu[duqulength][3] == 4)) { duqu[duqulength][1] += maplist[maplength].end / 16; } if (atoi(strArray[i + 1][6].c_str()) != duqu[duqulength][2] && maplist[maplength].end > 0 && (duqu[duqulength][3] == 1 || duqu[duqulength][3] == 2)) { duqu[duqulength][1] += maplist[maplength].end; } } else { if (maplist[maplength].end > 15 && (duqu[duqulength][3] == 3 || duqu[duqulength][3] == 4)) { duqu[duqulength][1] += maplist[maplength].end / 16; } if (maplist[maplength].end > 0 && (duqu[duqulength][3] == 1 || duqu[duqulength][3] == 2)) { duqu[duqulength][1] += maplist[maplength].end; } } } maplength++; } GetWillSendType(); } void CModbusTcpc::GetWillSendType() //功能码类型判断 { if (visitednum >= duqu[duquIndex][1]) //读取点位置大于等于组的寄存器数量 记录本组点是否读完 { duquIndex++; if (duquIndex > duqulength) { duquIndex = 0; visitednum = 0; IndexDI = 0; IndexAI = 0; for (int i = 0; i < maplength; i++) { maplist[i].visited = 0; } mapnum = 0; mapIndex = 0; Policy = 1; } visitednum = 0; m_SyncTime = duqu[duquIndex][4]; //时间间隔 if (duqu[duquIndex][3] == 1) { IndexDI = duqu[duquIndex][0]; //当前读取到的DI点位置设为每组开始时的地址 scopeDI = duqu[duquIndex][1] + duqu[duquIndex][0]; //每一次读取长度 开始地址+每组寄存器数量 WillSendType = 1; } if (duqu[duquIndex][3] == 2) { IndexDI = duqu[duquIndex][0]; scopeDI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 2; } if (duqu[duquIndex][3] == 3) { IndexAI = duqu[duquIndex][0]; scopeAI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 3; } if (duqu[duquIndex][3] == 4) { IndexAI = duqu[duquIndex][0]; scopeAI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 4; } } else { visitednum = 0; m_SyncTime = duqu[duquIndex][4]; if (duqu[duquIndex][3] == 1) { IndexDI = duqu[duquIndex][0]; scopeDI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 1; } if (duqu[duquIndex][3] == 2) { IndexDI = duqu[duquIndex][0]; scopeDI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 2; } if (duqu[duquIndex][3] == 3) { IndexAI = duqu[duquIndex][0]; scopeAI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 3; } if (duqu[duquIndex][3] == 4) { IndexAI = duqu[duquIndex][0]; scopeAI = duqu[duquIndex][1] + duqu[duquIndex][0]; WillSendType = 4; } } } uint8 CModbusTcpc::Operate_16(uint16 Address, uint16 num, uint16 pValue, uint8 func) { if (num > 125 || pValue == NULL) { PrintLog(LOG_ERROR, ">>>>>> Wrong Func_16 parameter"); return 0; } uint8 txbuff[256]; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuadd = pRtu->GetRtuAddr(rtuno); uint8 len = 0; if (cnt_sequence >= 65535) cnt_sequence = 0; else cnt_sequence++; txbuff[len++] = cnt_sequence / 0x100;//H Transaction Identifier txbuff[len++] = cnt_sequence % 0x100;//L txbuff[len++] = 0;//Protocol Identifier "0 = MODBUS protocol" txbuff[len++] = 0; txbuff[len++] = 0;//len_H if (func == 15) { txbuff[len++] = num + 7;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = 0x0F; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = num / 0x100; txbuff[len++] = num % 0x100; txbuff[len++] = num; //本规约只使用15、16功能码来强制一位线圈和寄存器,类似于05,06命令 所以省略了循环 线圈于字节数的转化 //uint8 i = 0; //for (i = 0; i < num; i++) //{ txbuff[len++] = pValue; //} } if (func == 16) { txbuff[len++] = num * 2 + 7;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = 0x10; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = num / 0x100; txbuff[len++] = num % 0x100; txbuff[len++] = num * 2; //uint8 i = 0; //for (i = 0; i < num; i++) //{ txbuff[len++] = pValue / 0x100;//高 txbuff[len++] = pValue % 0x100;//低 //} } // pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len); pLink->RegisterFrm(FRAME_TX_SUC); pTxBuf->Write(txbuff, len); pLink->SetCommStatus(CMST_RX_CNT); m_LastSendTime = GetNowMilliSecond(); PrintLog(LOG_INFORMATION, ">>>>>> SendType = 16 ,start=%d ,num=%d", Address, num); return len; } uint8 CModbusTcpc::Operate_16(uint16 Address, uint16 num, sint16 pValue, uint8 func) { if (num > 125 || pValue == NULL) { PrintLog(LOG_ERROR, ">>>>>> Wrong Func_16 parameter"); return 0; } uint8 txbuff[256]; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuadd = pRtu->GetRtuAddr(rtuno); uint8 len = 0; if (cnt_sequence >= 65535) cnt_sequence = 0; else cnt_sequence++; txbuff[len++] = cnt_sequence / 0x100;//H Transaction Identifier txbuff[len++] = cnt_sequence % 0x100;//L txbuff[len++] = 0;//Protocol Identifier "0 = MODBUS protocol" txbuff[len++] = 0; txbuff[len++] = 0;//len_H if (func == 15) { txbuff[len++] = num + 7;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = 0x0F; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = num / 0x100; txbuff[len++] = num % 0x100; txbuff[len++] = num; //uint8 i = 0; //for (i = 0; i < num; i++) //{ txbuff[len++] = pValue; //} } if (func == 16) { txbuff[len++] = num * 2 + 7;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = 0x10; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = num / 0x100; txbuff[len++] = num % 0x100; txbuff[len++] = num * 2; //uint8 i = 0; //for (i = 0; i < num; i++) //{ txbuff[len++] = pValue / 0x100;//高 txbuff[len++] = pValue % 0x100;//低 //} } // pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len); pLink->RegisterFrm(FRAME_TX_SUC); pTxBuf->Write(txbuff, len); pLink->SetCommStatus(CMST_RX_CNT); m_LastSendTime = GetNowMilliSecond(); PrintLog(LOG_INFORMATION, ">>>>>> SendType = 16 ,start=%d ,num=%d", Address, num); return len; } uint8 CModbusTcpc::ProcCmd() //下控 针对DO、AO使用05,06,15,16命令 使用cmdlist结构体 { uint8 b_ret = FALSE; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuaddr = pRtu->GetRtuAddr(rtuno); if (pCmdMem->GetCmdNum(rtuno) <= 0) return FALSE; S_RAWCMD rawcmd; if (pCmdMem->GetACmd(rtuno, &rawcmd) == 0) return FALSE; PrintLog(LOG_INFORMATION, "收到%d号终端 rtuaddr:%d 命令:%d", rtuno, rtuaddr, rawcmd.type); uint8 len = 0; m_cmdaddr = rawcmd.src; switch (rawcmd.type) { case DC_K_CMD_DOOPER: //遥控 { S_DO_CTL* cmdinfo; cmdinfo = (S_DO_CTL*)(&rawcmd.data); uint16 ykno = (uint16)cmdinfo->ptAddr;//遥控号 switch (cmdinfo->funcCode) { case DC_K_CTL_DOSEL://选择 //直接返校 rawcmd.type = DC_K_CMDRET_DOOPER; rawcmd.len = sizeof(cmdinfo); rawcmd.src = m_cmdaddr; cmdinfo->retCode = 1; pCmdMem->RptAResult(rtuno, rawcmd); break; case DC_K_CTL_DOEXE://执行 { PrintLog(LOG_INFORMATION, "执行"); for (int i = 0; i < cmdlength; i++) { if (cmdlist[i].pointnum == ykno) { //PrintLog(LOG_INFORMATION, "%d号点 地址:%d 功能号:%d", cmdlist[i].pointnum, cmdlist[i].address, cmdlist[i].func); if (cmdlist[i].func == 15) { Operate_16(cmdlist[i].address, 1, (uint16)cmdinfo->ctlVal, cmdlist[i].func); b_ret = TRUE; break; } if (cmdlist[i].func == 5) { uint16 ykvalue; if (cmdinfo->ctlVal == 0) {//分闸 ykvalue = 0x0000; } else {//if(cmdinfo->ctlVal==1){//合闸 ykvalue = 0xff00; } if (ykno >= 1) { //WillSendType = 5; len = OperateRegister(5, cmdlist[i].address, ykvalue); b_ret = TRUE; } break; } if (cmdlist[i].func == 6||cmdlist[i].func == 16) //此为新加的特殊功能 使用06命令控制一个寄存器的某一位DO点 控制点已被读取为uint 的AI点(配置文件中设置) { PrintLog(LOG_INFORMATION, "帧处理%d号点 地址:%d 功能号:%d", cmdlist[i].pointnum, cmdlist[i].address, cmdlist[i].func); PrintLog(LOG_INFORMATION, "ykno=%d va=%d ", ykno, cmdlist[i].va); if (ykno >= 1) { if (cmdlist[i].va == 1) //va=1 ->功能码为6或16并且类型为DO { uint16 yctmp = 0; uint16 ytVal = 1 << cmdlist[i].start; for (int index = maplength-1; index >= 0; index--) { if (maplist[index].address == cmdlist[i].address) { yctmp = maplist[index].data.datauint16; //已被读取为AI点 存入了寄存器的值 uint16 break; } } uint16 ycVal = (uint16)yctmp; uint16 ykvalue = cmdinfo->ctlVal; if (ykvalue == 0) {//分闸 ycVal = ycVal & (~ytVal); //指定位置强制为0 } if (ykvalue == 1) {//合闸 ycVal = ycVal | (ytVal); //指定位置强制为1 } PrintLog(LOG_INFORMATION, "发送帧,%d号点 地址:%d 功能号:%d,数据:%d", cmdlist[i].pointnum, cmdlist[i].address, cmdlist[i].func, ycVal); if (cmdlist[i].func == 16) { if (cmdlist[i].r_type == 1) { Operate_16(cmdlist[i].address, 1, (uint16)ycVal, cmdlist[i].func); } else { Operate_16(cmdlist[i].address, 1, (sint16)ycVal, cmdlist[i].func); } } else { if (cmdlist[i].func == 6) len = OperateRegister(/*6*/cmdlist[i].func, cmdlist[i].address, (uint16)ycVal); } } else continue; //WillSendType = 6; b_ret = TRUE; } break; } } } PrintLog(LOG_WARNING, "<<<<< YK no=%d val=%d", ykno, cmdinfo->ctlVal); } break; case DC_K_CTL_DODEL://撤消 break; } } break; case DC_K_CMD_AOOPER: { S_AO_CTL* aoinfo; aoinfo = (S_AO_CTL*)(&rawcmd.data); switch (aoinfo->funcCode) { case DC_K_CTL_AOSEL://选择 //直接返校 rawcmd.type = DC_K_CMDRET_AOOPER; rawcmd.len = sizeof(aoinfo); rawcmd.src = m_cmdaddr; aoinfo->retCode = 1; pCmdMem->RptAResult(rtuno, rawcmd); break; case DC_K_CTL_AOEXE://执行 { sint32 ytvalue = (sint32)(aoinfo->ptVal); uint16 ptno = aoinfo->ptAddr; for (int i = 0; i < cmdlength; i++) { if (cmdlist[i].m_type == 1) continue; if (cmdlist[i].pointnum == ptno) { if (cmdlist[i].func == 16) { if (cmdlist[i].r_type == 1) { Operate_16(cmdlist[i].address, 1, (uint16)ytvalue, cmdlist[i].func); } else { Operate_16(cmdlist[i].address, 1, (sint16)ytvalue, cmdlist[i].func); } b_ret = TRUE; break; } if (cmdlist[i].func == 6) { if (ptno >= 1) { if (cmdlist[i].va == 1) //如果类型为DO { continue; float32 yctmp = 0; uint16 ytVal = 1 << cmdlist[i].start; PRawCtrl->GetAYc(rtuno, cmdlist[i].pointnum, &yctmp); uint16 ycVal = (uint16)yctmp; uint16 ykvalue; if (ytvalue == 0) {//分闸 ycVal = ycVal & (~ytVal); } if (ytvalue == 1) {//合闸 ycVal = ycVal | (ytVal); } len = OperateRegister(6, cmdlist[i].address, (uint16)ycVal); } else { if (cmdlist[i].r_type == 1) { len = OperateRegister(6, cmdlist[i].address, (uint16)ytvalue); } else if (cmdlist[i].r_type == 2) { len = OperateRegister(6, cmdlist[i].address, (sint16)ytvalue); } } //WillSendType = 6; b_ret = TRUE; } break; } } } // PrintLog(LOG_WARNING, "<<<<<< YT no=%d val=%d", ptno, ytvalue); } break; case DC_K_CTL_AODEL://撤销 break; default: break; } } break; default: //对其它的下发命令不处理 break; } pCmdMem->DelACmd(rtuno); return b_ret; } sint32 CModbusTcpc::CheckFrameHeader(uint8* buff, sint32 len) //消息号检验 { if (len < 7) return 0; if (buff[0] != cnt_sequence / 256 || buff[1] != cnt_sequence % 256) return -1; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuaddr = pRtu->GetRtuAddr(rtuno); if (buff[2] != 0 || buff[3] != 0 || buff[6] != rtuaddr) return -2; return 1; } uint8 CModbusTcpc::ErrorHandle(uint8 errfunc) { uint8 retFunc = errfunc & 0x7f; if (retFunc != WillSendType) //答非所问 return 0; duquIndex = 0; visitednum = 0; GetWillSendType(); pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); PrintLog(LOG_ERROR, "------- 错误,重新读取!"); return 1; } uint8 CModbusTcpc::OperateRegister(uint8 FuncNo, uint16 Address, sint16 Value) //功能码1、2、3、4、5、6的组包 { uint8 txbuff[256]; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuadd = pRtu->GetRtuAddr(rtuno); uint8 len = 0; if (cnt_sequence >= 65535) cnt_sequence = 0; else cnt_sequence++; //7bytes Head txbuff[len++] = cnt_sequence / 0x100;//H Transaction Identifier txbuff[len++] = cnt_sequence % 0x100;//L txbuff[len++] = 0;//Protocol Identifier "0 = MODBUS protocol" txbuff[len++] = 0; txbuff[len++] = 0;//len_H txbuff[len++] = 6;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = FuncNo; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = Value / 0x100;//高 txbuff[len++] = Value % 0x100;//低 // pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len); pLink->RegisterFrm(FRAME_TX_SUC); pTxBuf->Write(txbuff, len); pLink->SetCommStatus(CMST_RX_CNT); m_LastSendTime = GetNowMilliSecond(); PrintLog(LOG_INFORMATION, ">>>>>> SendType = %d ,start=%d ,num=%d", WillSendType, Address, Value); return len; } uint8 CModbusTcpc::OperateRegister(uint8 FuncNo, uint16 Address, uint16 Value) { uint8 txbuff[256]; sint32 rtuno = pLink->GetRtuNo(); uint8 rtuadd = pRtu->GetRtuAddr(rtuno); uint8 len = 0; if (cnt_sequence >= 65535) cnt_sequence = 0; else cnt_sequence++; //7bytes Head txbuff[len++] = cnt_sequence / 0x100;//H Transaction Identifier txbuff[len++] = cnt_sequence % 0x100;//L txbuff[len++] = 0;//Protocol Identifier "0 = MODBUS protocol" txbuff[len++] = 0; txbuff[len++] = 0;//len_H txbuff[len++] = 6;//len_L txbuff[len++] = rtuadd;//Unit Identifier txbuff[len++] = FuncNo; txbuff[len++] = Address / 0x100;//高 txbuff[len++] = Address % 0x100;//低 txbuff[len++] = Value / 0x100;//高 txbuff[len++] = Value % 0x100;//低 // pLink->RegisterFrmCode(RAW_CODEDIR_DOWN,(char *)txbuff,len); pLink->RegisterFrm(FRAME_TX_SUC); pTxBuf->Write(txbuff, len); pLink->SetCommStatus(CMST_RX_CNT); m_LastSendTime = GetNowMilliSecond(); PrintLog(LOG_INFORMATION, ">>>>>> SendType = %d ,start=%d ,num=%d", WillSendType, Address, Value); return len; } int CModbusTcpc::ComparePoint(uint16 YxYcId, uint8& value) //对报文响应后数据处理 01、02 命令针对DI单个点 bit { if (WillSendType == 1 || WillSendType == 2) { if (maplist[mapIndex].address == YxYcId) //为了减少循环 特殊判断 不同才去下面遍历 { int fbit = 0; if (maplist[mapIndex].end == 0) //end为0说明为一位 取一个字节数据的第0位并将数据右移一位 { maplist[mapIndex].data.datauint16 = value & 1; maplist[mapIndex].visited = 1; mapIndex++; mapnum++; value >>= 1; return 0; } else //end不为0 取一个字节end位并右移一位 { for (int i = 0; i <= maplist[mapIndex].end; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datauint16 = value & fbit; maplist[mapIndex].visited = 1; mapIndex++; mapnum++; value >>= 1; return 0; } } if (maplist[mapIndex].address > YxYcId) //由于有end不为0的情况 使得地址大于当前YxYcId 右移1位直到相等 { value >>= 1; return -1; } for (int i = 0; i < maplength; i++) //如果上述判断地址不相等就遍历找一遍 { if (maplist[i].visited == 1) continue; if (maplist[i].address == YxYcId) { int fbit = 0; if (maplist[i].end == 0) { maplist[i].data.datauint16 = value & 1; maplist[i].visited = 1; mapIndex = i + 1; mapnum++; value >>= 1; return 0; } else { for (int i = 0; i <= maplist[i].end; i++) { fbit += pow(2, i); } maplist[i].data.datauint16 = value & fbit; maplist[i].visited = 1; mapIndex = i + 1; mapnum++; value >>= 1; return 0; } } } return -1; value >>= 1; } return -2; } int CModbusTcpc::ComparePoint_reg(uint16 YxYcId, uint8* buf, int i) //对报文响应后数据处理03、04命令 处理不同类型的DI、AI、PI、VA数据 { if (WillSendType == 3 || WillSendType == 4) { if (maplist[mapIndex].address == YxYcId) //先判断 为了减少循环 { if (maplist[mapIndex].m_type == 0) //VA数据,直接计算数据 占一个寄存器 两字节 16位 { maplist[mapIndex].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; //高8位数据左移8位+低8位 maplist[mapIndex].visited = 1; //读取标志置为1 mapIndex++; mapnum++; return 0; } if (maplist[mapIndex].start == 0 && maplist[mapIndex].end == 15) //整1个寄存器情况 { if (maplist[mapIndex].m_type == 3) //AI数据 16位无前缀 { if (maplist[mapIndex].r_type == 1) //uint型数据 maplist[mapIndex].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; //高8位数据左移8位+低8位 else //int型数据 maplist[mapIndex].data.datasint16 = (sint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; maplist[mapIndex].visited = 1; mapIndex++; mapnum++; return 0; } if (maplist[mapIndex].m_type == 4) //AI数据 16位rv前缀 rv:前缀代表寄存器内部字节前低后高 { if (maplist[mapIndex].r_type == 1) maplist[mapIndex].data.datauint16 = (uint16)256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]; //低8位数据左移8位+高8位 else maplist[mapIndex].data.datasint16 = (sint16)256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]; maplist[mapIndex].visited = 1; mapIndex++; mapnum++; return 0; } if (maplist[mapIndex].m_type == 9) //PI数据 注:在PI点中只有int16,int32,uint16,uint32,float这5种类型 { if (maplist[mapIndex].r_type == 1) maplist[mapIndex].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; else maplist[mapIndex].data.datasint16 = (sint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; maplist[mapIndex].visited = 1; mapIndex++; mapnum++; return 0; } } if (maplist[mapIndex].start > 0 || maplist[mapIndex].end < 15) //数据不满一整个寄存器的情况 { int fbit = 0; if (maplist[mapIndex].m_type == 3 || maplist[mapIndex].m_type == 1) //不为rv情况 { for (int i = maplist[mapIndex].start; i <= maplist[mapIndex].end; i++) //置fbit为从开始到结束位为1 { fbit += pow(2, i); //pow函数为求2的i次方 } if (maplist[mapIndex].r_type == 1){ maplist[mapIndex].data.datauint16 = (uint16)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; //取开始到结束位数据,其他位置为0 maplist[mapIndex].data.datauint16 >>= maplist[mapIndex].start; //右移开始位数据即将右侧无用置为0的数据去除 } else{ maplist[mapIndex].data.datasint16 = (sint16)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[mapIndex].data.datasint16 >>= maplist[mapIndex].start; } maplist[mapIndex].visited = 1; mapIndex++; mapnum++; return 1; } if (maplist[mapIndex].m_type == 4 || maplist[mapIndex].m_type == 2) //rv情况 区别为高低字节的右移计算 { for (int i = maplist[mapIndex].start; i <= maplist[mapIndex].end; i++) { fbit += pow(2, i); } if (maplist[mapIndex].r_type == 1){ maplist[mapIndex].data.datauint16 = (uint16)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datauint16 >>= maplist[mapIndex].start; } else{ maplist[mapIndex].data.datasint16 = (sint16)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datasint16 >>= maplist[mapIndex].start; } maplist[mapIndex].visited = 1; mapIndex++; mapnum++; return 1; } } if (maplist[mapIndex].end > 15) //用到两个寄存器的情况 { int fbit = 0; if (maplist[mapIndex].m_type == 5) //32位无前缀 AI点 { if (maplist[mapIndex].r_type == 3) //float型 { FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 2]; value.val2.b3 = buf[3 + 2 * i + 1 + 2]; value.val2.b2 = buf[3 + 2 * i]; value.val2.b1 = buf[3 + 2 * i + 1]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i] + buf[3 + 2 * i + 1]); maplist[mapIndex].data.datafloat = (256 * buf[3 + 2 * i + 2] + buf[3 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datafloat;*/ maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 4) //int32 型 { if (maplist[mapIndex].start > 0) //开始位不为0 前寄存器不满情况 { for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; //计算低寄存器中相应值 maplist[mapIndex].data.datasint32 >>= maplist[mapIndex].start; //右移开始位 去除右侧无用数据0 fbit = 0; } else{ maplist[mapIndex].data.datasint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); //前寄存器满的情况 } if (maplist[mapIndex].end < 31) //后寄存器即高寄存器不满情况 { for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; //计算后寄存器相应的值并取出对应位数据 其余置0 fbit = 0; maplist[mapIndex].data.datasint32 = (sint32)temp << (16 - maplist[mapIndex].start) | maplist[mapIndex].data.datasint32; //前后寄存器数据合并 方法为高寄存器左移低寄存器值的位数后与低寄存器值相或 } else{ maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datasint32; //后寄存器满的情况 直接计算右移16位后与前寄存器值相加 } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 5) //uint32 型 { if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[mapIndex].data.datauint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datauint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datauint32 = (uint32)temp << (16 - maplist[mapIndex].start) | maplist[mapIndex].data.datauint32; } else{ maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datauint32; } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } } if (maplist[mapIndex].m_type == 6) //32位AI点 拥有sw前缀 sw:前缀代表前寄存器为高位,后寄存器为低位; { if (maplist[mapIndex].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i]; value.val2.b3 = buf[3 + 2 * i + 1]; value.val2.b2 = buf[3 + 2 * i + 2]; value.val2.b1 = buf[3 + 2 * i + 1 + 2]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i] + buf[3 + 2 * i + 1]); maplist[mapIndex].data.datafloat = maplist[mapIndex].data.datafloat *pow(2, 16) + (256 * buf[3 + 2 * i + 2] + buf[3 + 2 * i + 1 + 2]);*/ maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 4){ if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[mapIndex].data.datasint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datasint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datasint32 = (sint32)maplist[mapIndex].data.datasint32 << (maplist[mapIndex].end - 15) | temp; //主要区别为前寄存器值左移后寄存器值位数后与后寄存器值相或 } else{ maplist[mapIndex].data.datasint32 = (sint32)maplist[mapIndex].data.datasint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]); } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 5){ if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[mapIndex].data.datauint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datauint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datauint32 = (uint32)maplist[mapIndex].data.datauint32 << (maplist[mapIndex].end - 15) | temp; } else{ maplist[mapIndex].data.datauint32 = (uint32)maplist[mapIndex].data.datauint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]); } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } } if (maplist[mapIndex].m_type == 7) //32位AI点 拥有rv前缀 rv:前缀代表寄存器内部字节前低后高 { if (maplist[mapIndex].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 1 + 2]; value.val2.b3 = buf[3 + 2 * i + 2]; value.val2.b2 = buf[3 + 2 * i + 1]; value.val2.b1 = buf[3 + 2 * i]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i + 1] + buf[3 + 2 * i]); maplist[mapIndex].data.datafloat = (256 * buf[3 + 2 * i + 2 + 1] + buf[3 + 2 * i + 2]) *pow(2, 16) + maplist[mapIndex].data.datafloat;*/ maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 4){ if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datasint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datasint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datasint32 = (sint32)temp << (16 - maplist[mapIndex].start) | maplist[mapIndex].data.datasint32; } else{ maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) *pow(2, 16) + maplist[mapIndex].data.datasint32; } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 5){ if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datauint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datauint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datauint32 = (uint32)temp << (16 - maplist[mapIndex].start) | maplist[mapIndex].data.datauint32; } else{ maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) *pow(2, 16) + maplist[mapIndex].data.datauint32; } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } } if (maplist[mapIndex].m_type == 8) //32位AI点 同时拥有sw、rv前缀 { if (maplist[mapIndex].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 1]; value.val2.b3 = buf[3 + 2 * i]; value.val2.b2 = buf[3 + 2 * i + 1 + 2]; value.val2.b1 = buf[3 + 2 * i + 2]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i + 1] + buf[3 + 2 * i]); maplist[mapIndex].data.datafloat = maplist[mapIndex].data.datafloat *pow(2, 16) + (256 * buf[3 + 2 * i + 2 + 1] + buf[3 + 2 * i + 2]);*/ maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 4) { if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datasint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datasint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[mapIndex].end < 31) { for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datasint32 = (sint32)maplist[mapIndex].data.datasint32 << (maplist[mapIndex].end - 15) | temp; } else{ maplist[mapIndex].data.datasint32 = (sint32)maplist[mapIndex].data.datasint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]); } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } if (maplist[mapIndex].r_type == 5){ if (maplist[mapIndex].start > 0){ for (int i = maplist[mapIndex].start; i < 16; i++) { fbit += pow(2, i); } maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[mapIndex].data.datauint32 >>= maplist[mapIndex].start; fbit = 0; } else{ maplist[mapIndex].data.datauint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[mapIndex].end < 31){ for (int i = 0; i <= maplist[mapIndex].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[mapIndex].data.datauint32 = (uint32)maplist[mapIndex].data.datauint32 << (maplist[mapIndex].end - 15) | temp; } else{ maplist[mapIndex].data.datauint32 = (uint32)maplist[mapIndex].data.datauint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]); } maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 1; } } if (maplist[mapIndex].m_type == 9) //32位PI点 { if (maplist[mapIndex].r_type == 3) { maplist[mapIndex].data.datafloat = (float)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[mapIndex].data.datafloat = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datafloat; maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 0; } if (maplist[mapIndex].r_type == 4) { maplist[mapIndex].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[mapIndex].data.datasint32 = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datasint32; maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 0; } if (maplist[mapIndex].r_type == 5) { maplist[mapIndex].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[mapIndex].data.datauint32 = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datauint32; maplist[mapIndex].visited = 1; mapIndex = mapIndex + 1; mapnum++; return 0; } } } } if (maplist[mapIndex].address > YxYcId) //上为判断减少循环 下为循环里实现 基本相同 { return -1; } for (int j = 0; j < maplength; j++) { if (maplist[j].visited == 1) continue; if (maplist[j].address == YxYcId) { if (maplist[j].m_type == 0){ maplist[j].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } if (maplist[j].start == 0 && maplist[j].end == 15) { if (maplist[j].m_type == 3){ if (maplist[j].r_type == 1) maplist[j].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; else maplist[j].data.datasint16 = (sint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } if (maplist[j].m_type == 4){ if (maplist[j].r_type == 1) maplist[j].data.datauint16 = (uint16)256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]; else maplist[j].data.datasint16 = (sint16)256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } if (maplist[j].m_type == 9) { if (maplist[j].r_type == 1) maplist[j].data.datauint16 = (uint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; else maplist[j].data.datasint16 = (sint16)256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } } if (maplist[j].start > 0 || maplist[j].end < 15) { int fbit = 0; if (maplist[j].m_type == 3 || maplist[j].m_type == 1){ for (int i = maplist[j].start; i <= maplist[j].end; i++) { fbit += pow(2, i); } if (maplist[j].r_type == 1){ maplist[j].data.datauint16 = (uint16)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datauint16 >>= maplist[j].start; } else{ maplist[j].data.datasint16 = (sint16)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datasint16 >>= maplist[j].start; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].m_type == 4 || maplist[j].m_type == 2){ for (int i = maplist[j].start; i <= maplist[j].end; i++) { fbit += pow(2, i); } if (maplist[j].r_type == 1){ maplist[j].data.datauint16 = (uint16)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datauint16 >>= maplist[j].start; } else{ maplist[j].data.datasint16 = (sint16)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datasint16 >>= maplist[j].start; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } } if (maplist[j].end > 15) { int fbit = 0; if (maplist[j].m_type == 5){ if (maplist[j].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 2]; value.val2.b3 = buf[3 + 2 * i + 1 + 2]; value.val2.b2 = buf[3 + 2 * i]; value.val2.b1 = buf[3 + 2 * i + 1]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i] + buf[3 + 2 * i + 1]); maplist[mapIndex].data.datafloat = (256 * buf[3 + 2 * i + 2] + buf[3 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[mapIndex].data.datafloat;*/ maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 4){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datasint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datasint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[j].data.datasint32 = (sint32)temp << (16 - maplist[j].start) | maplist[j].data.datasint32; } else{ maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[j].data.datasint32; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 5){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datauint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datauint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[j].data.datauint32 = (uint32)temp << (16 - maplist[j].start) | maplist[j].data.datauint32; } else{ maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[j].data.datauint32; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } } if (maplist[j].m_type == 6){ if (maplist[j].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i]; value.val2.b3 = buf[3 + 2 * i + 1]; value.val2.b2 = buf[3 + 2 * i + 2]; value.val2.b1 = buf[3 + 2 * i + 1 + 2]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i] + buf[3 + 2 * i + 1]); maplist[mapIndex].data.datafloat = maplist[mapIndex].data.datafloat *pow(2, 16) + (256 * buf[3 + 2 * i + 2] + buf[3 + 2 * i + 1 + 2]);*/ maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 4){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datasint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datasint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[j].data.datasint32 = (sint32)maplist[j].data.datasint32 << (maplist[j].end - 15) | temp; } else{ maplist[j].data.datasint32 = (sint32)maplist[j].data.datasint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]); } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 5){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]) & fbit; maplist[j].data.datauint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datauint32 = (256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) & fbit; fbit = 0; maplist[j].data.datauint32 = (uint32)maplist[j].data.datauint32 << (maplist[j].end - 15) | temp; } else{ maplist[j].data.datauint32 = (uint32)maplist[j].data.datauint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]); } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } } if (maplist[j].m_type == 7){ if (maplist[j].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 1 + 2]; value.val2.b3 = buf[3 + 2 * i + 2]; value.val2.b2 = buf[3 + 2 * i + 1]; value.val2.b1 = buf[3 + 2 * i]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i + 1] + buf[3 + 2 * i]); maplist[mapIndex].data.datafloat = (256 * buf[3 + 2 * i + 2 + 1] + buf[3 + 2 * i + 2]) *pow(2, 16) + maplist[mapIndex].data.datafloat;*/ maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 4){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datasint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datasint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[j].data.datasint32 = (sint32)temp << (16 - maplist[j].start) | maplist[j].data.datasint32; } else{ maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) *pow(2, 16) + maplist[j].data.datasint32; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 5){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datauint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datauint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[j].data.datauint32 = (uint32)temp << (16 - maplist[j].start) | maplist[j].data.datauint32; } else{ maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) *pow(2, 16) + maplist[j].data.datauint32; } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } } if (maplist[j].m_type == 8){ if (maplist[j].r_type == 3){ FCHAR4 value; value.val2.b4 = buf[3 + 2 * i + 1]; value.val2.b3 = buf[3 + 2 * i]; value.val2.b2 = buf[3 + 2 * i + 1 + 2]; value.val2.b1 = buf[3 + 2 * i + 2]; maplist[mapIndex].data.datafloat = value.val1; /*maplist[mapIndex].data.datafloat = (float)(256 * buf[3 + 2 * i + 1] + buf[3 + 2 * i]); maplist[mapIndex].data.datafloat = maplist[mapIndex].data.datafloat *pow(2, 16) + (256 * buf[3 + 2 * i + 2 + 1] + buf[3 + 2 * i + 2]);*/ maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 4){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datasint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datasint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[j].data.datasint32 = (sint32)maplist[j].data.datasint32 << (maplist[j].end - 15) | temp; } else{ maplist[j].data.datasint32 = (sint32)maplist[j].data.datasint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]); } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } if (maplist[j].r_type == 5){ if (maplist[j].start > 0){ for (int i = maplist[j].start; i < 16; i++) { fbit += pow(2, i); } maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]) & fbit; maplist[j].data.datauint32 >>= maplist[j].start; fbit = 0; } else{ maplist[j].data.datauint32 = (256 * buf[9 + 2 * i + 1] + buf[9 + 2 * i]); } if (maplist[j].end < 31){ for (int i = 0; i <= maplist[j].end - 16; i++) { fbit += pow(2, i); } uint16 temp = (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]) & fbit; fbit = 0; maplist[j].data.datauint32 = (uint32)maplist[j].data.datauint32 << (maplist[j].end - 15) | temp; } else{ maplist[j].data.datauint32 = (uint32)maplist[j].data.datauint32 *pow(2, 16) + (256 * buf[9 + 2 * i + 2 + 1] + buf[9 + 2 * i + 2]); } maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 1; } } if (maplist[j].m_type == 9) { if (maplist[j].r_type == 3){ maplist[j].data.datafloat = (float)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[j].data.datafloat = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[j].data.datafloat; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } if (maplist[j].r_type == 4){ maplist[j].data.datasint32 = (sint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[j].data.datasint32 = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[j].data.datasint32; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } if (maplist[j].r_type == 5){ maplist[j].data.datauint32 = (uint32)(256 * buf[9 + 2 * i] + buf[9 + 2 * i + 1]); maplist[j].data.datauint32 = (256 * buf[9 + 2 * i + 2] + buf[9 + 2 * i + 1 + 2]) *pow(2, 16) + maplist[j].data.datauint32; maplist[j].visited = 1; mapIndex = j + 1; mapnum++; return 0; } } } } } return -1; } return -2; } sint64 CModbusTcpc::GetNowMilliSecond() //获取当前毫秒时间 { CSeTime SE_T; TCriterionTime tmptime;//sint32 unsigned short millsecond; SE_T.GetNow(&tmptime, &millsecond); sint64 nowmilltime = (sint64)tmptime * 1000 + millsecond; return nowmilltime; } sint32 CModbusTcpc::RxProc() { if (err) return -1; //PrintLog(LOG_INFORMATION, "------- 调用RxProc"); sint32 rtuno = pLink->GetRtuNo(); uint8 buf[1024]; int datalen = 0, datanum = 0; int i, j; sint32 buflen = pRxBuf->GetReadableSize(); sint64 nowtime = GetNowMilliSecond(); if (mapnum >= maplength) { mapnum = 0; return 0; } E_RAW_ComStat commstatus = pLink->GetCommStatus(); if (commstatus == CMST_RX_CNT && (nowtime - m_LastSendTime) >= m_RcvTimeOuts * 1000) { //超时不重发,直接跳到下一个终端 pRxBuf->Move(buflen); pLink->RegisterFrm(FRAME_RX_TIMEOUT); pLink->SetCommStatus(CMST_TX_CNT); PrintLog(LOG_VIOLATION, " 数据超时,%d\n", nowtime - m_LastSendTime); return -1; } if (buflen < 7) //无数据或数据不够不改变链路通讯状态,继续等待 return -1; datalen = pRxBuf->Read(buf, 7, DEF_BUFF_NOMOVE); //读前7位报文头 int ret = CheckFrameHeader(buf, 7); if (ret<0) { pRxBuf->Move(buflen); pLink->RegisterFrm(FRAME_RX_MISSTEP); PrintLog(LOG_VIOLATION, "ModbusHead不一致\n"); return -1; } else if (ret == 0) return 0; uint16 d_len = buf[4] * 256 + buf[5] + 6; //计算报文整体长度 if (buflen < d_len) return -1; datalen = pRxBuf->Read(buf, d_len, DEF_BUFF_MOVE); // PrintLog(LOG_INFORMATION,"<<<<< ReceiveType=%d, numbyte=%d",buf[7],buf[8]); if (buf[7] & 0x80){ pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return -1;//ErrorHandle(buf[7]); } uint16 YxYcId = 0; switch (buf[7]) //功能码判断 { case 1: if (WillSendType == 1) { //处理遥信 for (i = 0; i < buf[8]; i++) { uint8 value = buf[9 + i]; for (j = 0; j < 8; j++) { YxYcId = 8 * i + j + IndexDI; if (YxYcId >= (IndexDI + numDI)) break; int ret = ComparePoint(YxYcId, value); if (ret == 0) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } } } PrintLog(LOG_INFORMATION, "---- Receive Yx data "); if (buf[8] * 8 < numDI) { IndexDI += buf[8] * 8; visitednum += buf[8] * 8; } else { IndexDI += numDI; visitednum += numDI; } pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; case 2: if (WillSendType == 2) { //处理遥信 for (i = 0; i < buf[8]; i++) { uint8 value = buf[9 + i]; for (j = 0; j < 8; j++) { YxYcId = 8 * i + j + IndexDI; if (YxYcId >= (IndexDI + numDI)) break; int ret = ComparePoint(YxYcId, value); if (ret == 0) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } } } PrintLog(LOG_INFORMATION, "---- Receive Yx data "); if (buf[8] * 8 < numDI) { IndexDI += buf[8] * 8; visitednum += buf[8] * 8; } else { IndexDI += numDI; visitednum += numDI; } pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; case 3: //处理遥测 if (WillSendType == 3) { for (i = 0; i < buf[8] / 2; i++) { YxYcId = i + IndexAI; if (YxYcId >= (IndexAI + numAI)) break; int ret = ComparePoint_reg(YxYcId, buf, i); if (ret == 0) { if (maplist[mapIndex - 1].m_type == 1 || maplist[mapIndex - 1].m_type == 2) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } if (maplist[mapIndex - 1].m_type == 3 || maplist[mapIndex - 1].m_type == 4 || maplist[mapIndex - 1].m_type == 5 || maplist[mapIndex - 1].m_type == 6 || maplist[mapIndex - 1].m_type == 7 || maplist[mapIndex - 1].m_type == 8) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 9) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 0) { uint16 data = 1; if (maplist[mapIndex - 1].data.datauint16 == 0) { for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); } else if (maplist[mapIndex - 1].data.datauint16 > maplist[mapIndex - 1].end + 1) { for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); PrintLog(LOG_ERROR, "VA data error 采集序号:%d,数据:%d", maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } else if (maplist[mapIndex - 1].data.datauint16 <= maplist[mapIndex - 1].end + 1 && maplist[mapIndex - 1].data.datauint16 > 0) { uint16 data = 1; data <<= maplist[mapIndex - 1].data.datauint16 - 1; for (int index = 0; index <= maplist[mapIndex - 1].end; index++){ PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, data & 1); data >>= 1; } } } } while (ret == 1) { if (maplist[mapIndex - 1].m_type == 1 || maplist[mapIndex - 1].m_type == 2) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } if (maplist[mapIndex - 1].m_type == 3 || maplist[mapIndex - 1].m_type == 4 || maplist[mapIndex - 1].m_type == 5 || maplist[mapIndex - 1].m_type == 6 || maplist[mapIndex - 1].m_type == 7 || maplist[mapIndex - 1].m_type == 8) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: //float32 a; //会有失真的问题 //a = maplist[mapIndex - 1].data.datasint32; PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 9) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 0) { uint16 data = 1; if (maplist[mapIndex - 1].data.datauint16 == 0){ for (int index = 0; index <= maplist[mapIndex - 1].end + 1; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); } else if (maplist[mapIndex - 1].data.datauint16 > maplist[mapIndex - 1].end + 1){ for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); PrintLog(LOG_ERROR, "VA data error 采集序号:%d,数据:%d", maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } else if (maplist[mapIndex - 1].data.datauint16 <= maplist[mapIndex - 1].end + 1 && maplist[mapIndex - 1].data.datauint16 > 0){ uint16 data = 1; data <<= maplist[mapIndex - 1].data.datauint16 - 1; for (int index = 0; index <= maplist[mapIndex - 1].end; index++){ PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, data & 1); data >>= 1; } } } ret = ComparePoint_reg(YxYcId, buf, i); } } PrintLog(LOG_INFORMATION, ">>>> Receive Yc data "); if (buf[8] / 2 < numAI) { IndexAI += buf[8] / 2; visitednum += buf[8] / 2; } else { IndexAI += numAI; visitednum += numAI; } pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; case 4: //处理遥测 if (WillSendType == 4) { for (i = 0; i < buf[8] / 2; i++) { YxYcId = i + IndexAI; if (YxYcId >= (IndexAI + numAI)) break; int ret = ComparePoint_reg(YxYcId, buf, i); if (ret == 0) { if (maplist[mapIndex - 1].m_type == 1 || maplist[mapIndex - 1].m_type == 2) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } if (maplist[mapIndex - 1].m_type == 3 || maplist[mapIndex - 1].m_type == 4 || maplist[mapIndex - 1].m_type == 5 || maplist[mapIndex - 1].m_type == 6 || maplist[mapIndex - 1].m_type == 7 || maplist[mapIndex - 1].m_type == 8) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 9) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 0) { uint16 data = 1; if (maplist[mapIndex - 1].data.datauint16 == 0){ for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); } else if (maplist[mapIndex - 1].data.datauint16 > maplist[mapIndex - 1].end + 1){ for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); PrintLog(LOG_ERROR, "VA data error 采集序号:%d,数据:%d", maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } else if (maplist[mapIndex - 1].data.datauint16 <= maplist[mapIndex - 1].end + 1 && maplist[mapIndex - 1].data.datauint16 > 0){ uint16 data = 1; data <<= maplist[mapIndex - 1].data.datauint16 - 1; for (int index = 0; index <= maplist[mapIndex - 1].end; index++){ PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, data & 1); data >>= 1; } } } } while (ret == 1) { if (maplist[mapIndex - 1].m_type == 1 || maplist[mapIndex - 1].m_type == 2) { PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } if (maplist[mapIndex - 1].m_type == 3 || maplist[mapIndex - 1].m_type == 4 || maplist[mapIndex - 1].m_type == 5 || maplist[mapIndex - 1].m_type == 6 || maplist[mapIndex - 1].m_type == 7 || maplist[mapIndex - 1].m_type == 8) { switch (maplist[mapIndex - 1].r_type) { case 1: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAYc(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 9) { switch (maplist[mapIndex - 1].r_type){ case 1: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); break; case 2: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint16); break; case 3: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datafloat); break; case 4: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datasint32); break; case 5: PRawCtrl->PutAKwh(rtuno, maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint32); break; } } if (maplist[mapIndex - 1].m_type == 0) { uint16 data = 1; if (maplist[mapIndex - 1].data.datauint16 == 0){ for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); } else if (maplist[mapIndex - 1].data.datauint16 > maplist[mapIndex - 1].end + 1){ for (int index = 0; index <= maplist[mapIndex - 1].end; index++) PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, 0); PrintLog(LOG_ERROR, "VA data error 采集序号:%d,数据:%d", maplist[mapIndex - 1].pointnum, maplist[mapIndex - 1].data.datauint16); } else if (maplist[mapIndex - 1].data.datauint16 <= maplist[mapIndex - 1].end + 1 && maplist[mapIndex - 1].data.datauint16 > 0){ uint16 data = 1; data <<= maplist[mapIndex - 1].data.datauint16 - 1; for (int index = 0; index <= maplist[mapIndex - 1].end; index++){ PRawCtrl->PutAYx(rtuno, maplist[mapIndex - 1].pointnum + index, data & 1); data >>= 1; } } } ret = ComparePoint_reg(YxYcId, buf, i); } } PrintLog(LOG_INFORMATION, ">>>> Receive Yc data "); if (buf[8] / 2 < numAI) { IndexAI += buf[8] / 2; visitednum += buf[8] / 2; } else { IndexAI += numAI; visitednum += numAI; } pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; case 5: //遥控返回 // if(WillSendType == 5) { pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; case 6: //遥调返回 // if(WillSendType == 6) { pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } case 15: //遥控返回 // if(WillSendType == 6) { pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } case 16: //遥调返回 // if(WillSendType == 6) { pLink->RegisterFrm(FRAME_RX_SUC); pLink->SetCommStatus(CMST_NORMAL); return 1; } break; default: return 0; break; } return 1; } sint32 CModbusTcpc::TxProc() { if (err) return -1; if (readcfgflag == 0) { sint64 nowtime = GetNowMilliSecond(); if (nowtime - sleepstarttime > sleeptime * 1000) { sint64 cfgstart = 0; sint64 cfgend = 0; cfgstart = GetNowMilliSecond(); ReadCfg(); cfgend = GetNowMilliSecond(); sint64 pasttime = 0; pasttime = cfgend - cfgstart; PrintLog(1, "配置文件读取时间 %d 毫秒", pasttime); readcfgflag = 1; } else { return 0; } } //PrintLog(LOG_INFORMATION, "------- 调用TxProc"); E_RAW_ComStat commstatus = pLink->GetCommStatus(); if (commstatus != CMST_TX_CNT && commstatus != CMST_NORMAL) return 0; uint16 rxbuflen = 0; rxbuflen = pRxBuf->GetReadableSize(); if (rxbuflen > 0) { PrintLog(LOG_ERROR, "------- 丢弃 %d垃圾数据!", rxbuflen); pRxBuf->Move(rxbuflen); } if (Policy && ProcCmd()) { pLink->SetCommStatus(CMST_RX_CNT); return 1; //处理外部下发命令 } //temp // if(( nowtime - m_LastSendTime)<1) // return 0; uint8 len = 0; sint64 nowtime = GetNowMilliSecond(); if (duqu[duquIndex][4] > 0 && ((uint64)(nowtime - m_LastSendTime) < (uint64)duqu[duquIndex][4])) { PrintLog(LOG_ERROR, "未达到间隔时间,距离上次发送%d毫秒,%d", nowtime - m_LastSendTime, (uint64)duqu[duquIndex][4]); return -1; } if (visitednum >= duqu[duquIndex][1]) { GetWillSendType(); } switch (WillSendType) { case 1://read DIO R/W if ((scopeDI - IndexDI) > MAX_BYTE_NUM * 8) numDI = MAX_BYTE_NUM * 8; else numDI = scopeDI - IndexDI; if (numDI > 0) len = OperateRegister(WillSendType, IndexDI, numDI); break; case 2://read DIO R/W if ((scopeDI - IndexDI) > MAX_BYTE_NUM * 8) numDI = MAX_BYTE_NUM * 8; else numDI = scopeDI - IndexDI; if (numDI > 0) len = OperateRegister(WillSendType, IndexDI, numDI); break; case 3://read AIO R/W if ((scopeAI - IndexAI) > MAX_BYTE_NUM / 2) numAI = MAX_BYTE_NUM / 2; else numAI = scopeAI - IndexAI; if (numAI > 0) len = OperateRegister(WillSendType, IndexAI, numAI); break; case 4://read AIO R/W if ((scopeAI - IndexAI) > MAX_BYTE_NUM / 2) numAI = MAX_BYTE_NUM / 2; else numAI = scopeAI - IndexAI; if (numAI > 0) len = OperateRegister(WillSendType, IndexAI, numAI); break; default: break; } if (len > 0) pLink->SetCommStatus(CMST_RX_CNT); return len; } //规约对象创建函数 #ifdef __unix extern "C" CProtocol * CreateProtocol(char* defpara) #else extern "C" __declspec(dllexport) CProtocol * CreateProtocol(char* defpara) #endif { CProtocol* pEpv = new CModbusTcpc; return pEpv; }