private List <ModbusTcpDataEntity> GetModbusTcpData(ModbusTcpResult mbTcpResult) { List <ModbusTcpDataEntity> mbTcpDataList = new List <ModbusTcpDataEntity>(); //查找对应的operation var ops = from n in m_config.ModbusOperations where n.Identifier == mbTcpResult.Identifier select n; ModbusTcpOperation mbTcpOp = null; foreach (ModbusTcpOperation op in ops) { mbTcpOp = op; } if (mbTcpOp != null) { for (int i = 0; i < mbTcpOp.RegCount; i++) { ushort currentAddr = (ushort)(mbTcpOp.StartAddr + i); ModbusTcpConfigItem mbTcpCfgItem = GetModbusTcpConfigItemByRegAddr(currentAddr); if (mbTcpCfgItem != null) { ModbusTcpDataEntity dataEntity = new ModbusTcpDataEntity(); dataEntity.RID = Guid.NewGuid().ToString(); dataEntity.Device_Addr = mbTcpCfgItem.DeviceAddr.ToString(); dataEntity.Station = m_config.ServerName; dataEntity.Sensor_Type = "-1"; dataEntity.Sensor_Name = mbTcpCfgItem.Name; dataEntity.Ori_Value = mbTcpResult.ResultData[i]; dataEntity.Trans_Value = dataEntity.Ori_Value * mbTcpCfgItem.Multiplier; dataEntity.Trans_Unit = mbTcpCfgItem.Unit; dataEntity.DataAcqTime = mbTcpResult.DataAcqTime; mbTcpDataList.Add(dataEntity); } } } return(mbTcpDataList); }
private List <ModbusTcpOperation> GetModbusTcpOperations() { List <ModbusTcpOperation> ops = new List <ModbusTcpOperation>(); XmlNode opsNode = GetModbusTcpOperationsNode(); if (opsNode != null) { XmlNodeList opNodeList = opsNode.SelectNodes("operation"); foreach (XmlNode opNode in opNodeList) { ModbusTcpOperation op = new ModbusTcpOperation(); if (opNode.Attributes["device_addr"] != null) { string sDeviceAddr = opNode.Attributes["device_addr"].Value; byte tempDeviceAddr; if (byte.TryParse(sDeviceAddr, out tempDeviceAddr)) { op.DeviceAddr = tempDeviceAddr; } } if (op.DeviceAddr == null) { continue; } if (opNode.Attributes["func_code"] != null) { string sFuncCode = opNode.Attributes["func_code"].Value; byte tempFuncCode; if (byte.TryParse(sFuncCode, out tempFuncCode)) { op.FunctionCode = tempFuncCode; } } if (op.FunctionCode == null) { continue; } if (opNode.Attributes["start_addr"] != null) { string sStartAddr = opNode.Attributes["start_addr"].Value; ushort tempStartAddr; if (ushort.TryParse(sStartAddr, out tempStartAddr)) { op.StartAddr = tempStartAddr; } } if (op.StartAddr == null) { continue; } if (opNode.Attributes["reg_count"] != null) { string sRegCount = opNode.Attributes["reg_count"].Value; ushort tempRegCount; if (ushort.TryParse(sRegCount, out tempRegCount)) { op.RegCount = tempRegCount; } } if (op.RegCount == null) { continue; } op.Length = 6; op.Protocol = 0; op.Identifier = m_identifier++; ServiceLog.LogServiceMessage(op.ToString()); ops.Add(op); } } return(ops); }