/// <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); } } } }
private void WriteModbus() { try { foreach (var modbusUint in this.ModbusUnits) { if (!modbusUint.Connector.IsConnect()) { modbusUint.Connector.Connect(); } } while (CanWirteModbus()) { foreach (var modbusUint in this.ModbusUnits) { if (modbusUint.ToWriteRegisterCommands != null && modbusUint.ToWriteRegisterCommands.Count > 0) { WriteRegisterCommand currentWriteRegisterCommand = null; List <byte> recvBytes = null; lock (writeMobusThreadLoker) { currentWriteRegisterCommand = modbusUint.ToWriteRegisterCommands.Dequeue(); } if (currentWriteRegisterCommand != null) { recvBytes = modbusUint.Connector.Send(currentWriteRegisterCommand.WriteCommand); if (recvBytes != null) { AnalyzeRecivedDataReponse reponse = ModbusTCP.AnalyzeRecivedDataStatic(modbusUint.DataAnalyzeMode, currentWriteRegisterCommand.WriteCommand, recvBytes); if (reponse.ModbusReponseSuccess && reponse.AnalyzeRecivedDataSuccess) { var dataPointsWhoseRealTimeDataChanged = ModbusTCP.SetDataPointValueFromRegisterValue(reponse.Registers, modbusUint.AllDataPoints); List <DataPointRealValue> dataPointRealValues = DataObjectMapper.ConvertToListFrom(dataPointsWhoseRealTimeDataChanged); RaiseCurrentReceiveDataChangedEvent(dataPointRealValues); } } } } } } } catch (Exception ex) { WriteModbus(); } }
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); } } } } } }