/// <summary> /// 设置DataPiont的值 /// </summary> /// <param name="setDpDto"></param> public void AddDataPointToSetValue(SetDataPointValue setDpDto) { var toFindModbusUnit = FindModbusUnit(this.ModbusUnits, setDpDto.DataPointNumber); if (toFindModbusUnit != null) { List <DataPoint> toWriteDataPoints = new List <DataPoint>(); var toWriteDataPoint = toFindModbusUnit.AllDataPoints.FirstOrDefault(p => p.Number == setDpDto.DataPointNumber); if (toWriteDataPoint != null) { var copyDp = toWriteDataPoint.CopyNew(); copyDp.ValueToSet = setDpDto.ValueToSet; toWriteDataPoints.Add(copyDp); } List <List <byte> > requestBytes = ModbusTCP.CreateWriteRegisterCommands(toFindModbusUnit.DataAnalyzeMode, toWriteDataPoints); foreach (var requestByte in requestBytes) { lock (writeMobusThreadLoker) { WriteRegisterCommand writeRegisterCommand = new WriteRegisterCommand(); writeRegisterCommand.WriteCommand = requestByte; toFindModbusUnit.ToWriteRegisterCommands.Enqueue(writeRegisterCommand); } } } }
public void SetDataPointRealValueByFunNun16Test() { ModbusTCPServer modbusTCPServer = new ModbusTCPServer(); modbusTCPServer.InitializeFromConfigFile(modbusConfigFile); foreach (var modbusUint in modbusTCPServer.ModbusUnits) { List <DataPoint> dataPointsWhoseRealTimeDataChanged = new List <DataPoint>(); List <DataPoint> dataPoints = new List <DataPoint>(); dataPoints.Add(new DataPoint() { DeviceAddress = 1, StartRegisterAddress = 83, DataPointType = DataPointType.WriteAndReadByFunNum03, DataPointDataType = DataPointDataType.F32, RealTimeValue = -1, ValueToSet = 101.7 }); List <List <byte> > writeRegisterCommandBytes = ModbusTCP.CreateWriteRegisterCommands(DataAnalyzeMode.DataHighToLow, dataPoints); if (modbusUint.Connector.Connect()) { foreach (var writeBytes in writeRegisterCommandBytes) { var recvBytes = modbusUint.Connector.Send(writeBytes); if (recvBytes != null) { AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, writeBytes, recvBytes); if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess) { dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, dataPoints); } } } } } }
public void SetDataPointRealValueByTest() { ModbusTCPServer modbusTCPServer = new ModbusTCPServer(); modbusTCPServer.InitializeFromConfigFile(modbusConfigFile); foreach (var modbusUint in modbusTCPServer.ModbusUnits) { List <DataPoint> allDataPointsWhoseRealTimeDataChangeds = new List <DataPoint>(); List <WriteRegisterCommand> allWriterRegisterCommands = new List <WriteRegisterCommand>(); List <DataPoint> canWriteDataPoints = modbusUint.AllDataPoints.FindAll(p => p.DataPointType == DataPointType.WriteAndReadByFunNum01 || p.DataPointType == DataPointType.WriteAndReadByFunNum03); #region 模拟数据 foreach (var writeDataPoint in canWriteDataPoints) { if (writeDataPoint.DataPointType == DataPointType.WriteAndReadByFunNum01) { var dataPointNo = Convert.ToInt32(writeDataPoint.Number); if (dataPointNo % 2 == 0) { writeDataPoint.ValueToSet = 1; } else { writeDataPoint.ValueToSet = 0; } } else if (writeDataPoint.DataPointType == DataPointType.WriteAndReadByFunNum03) { writeDataPoint.ValueToSet = Convert.ToDouble(writeDataPoint.Number); } } #endregion List <List <byte> > requestBytes = ModbusTCP.CreateWriteRegisterCommands(modbusUint.DataAnalyzeMode, canWriteDataPoints); foreach (var requestByte in requestBytes) { WriteRegisterCommand writeRegisterCommand = new WriteRegisterCommand(); writeRegisterCommand.WriteCommand = requestByte; allWriterRegisterCommands.Add(writeRegisterCommand); } if (modbusUint.Connector.Connect()) { int i = 0; foreach (var writeRegisterCommand in allWriterRegisterCommands) { ++i; var recvBytes = modbusUint.Connector.Send(writeRegisterCommand.WriteCommand); if (recvBytes != null) { AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, writeRegisterCommand.WriteCommand, recvBytes); if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess) { var dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, modbusUint.AllDataPoints); allDataPointsWhoseRealTimeDataChangeds.AddRange(dataPointsWhoseRealTimeDataChanged); } } } } } }