private static PlcType GetPlcType(IPlc plc) { return(plc switch { ModbusPlc _ => PlcType.Modbus, S7Plc _ => PlcType.S7, _ => throw new ArgumentException($"Unsupported PLC {plc}.", nameof(plc)) });
public VariableViewModel CreateVariable(IPlc plc) { return(plc switch { S7Plc s7plc => new S7VariableViewModel(s7plc), ModbusPlc modbusPlc => new ModbusVariableViewModel(modbusPlc), _ => throw new NotImplementedException() });
public async void DoIt() { // use DI here IModulePocoFactory modulePocoFactory = new CpvModulePocoFactory(); // use DI here IModuleBasedAddressConverter moduleBasedAddressConverter = new S7ModuleBasedAddressConverter(); // use DI here S7PlcConfiguration s7PlcConfig = new S7PlcConfiguration { CpuType = "S71500", IpAddress = "192.168.1.1", Rack = 0, Slot = 0, UsePermanentConnection = true }; // make your own IPlcFactory which creates the plc instance // use the IPlcFactory interface for this ModuleBasedPlc moduleBasedPlc = new S7Plc( modulePocoFactory, moduleBasedAddressConverter, s7PlcConfig); ModuleBasedAddress addressToRead = new ModuleBasedAddress("DB1.DBW0", MonitorDataPoco.DB_INDEX); // this way you read by address PlcReadResult readByAddressResult = await moduleBasedPlc.ReadAsync(new string[] { addressToRead.Address }); // this way you parse the measurement for a given address from a read result object measurementByAddress = readByAddressResult.GetMeasurementForAddress(addressToRead.Address); // this way you read by module poco MonitorDataPoco readByModulePocoResult = await moduleBasedPlc.ReadAsync <MonitorDataPoco>(MonitorDataPoco.DB_INDEX); // here you have the module poco and you // cann access the measurments by address like this: object measurmentByAddressFromModulePoco = readByModulePocoResult.GetValueByAddress(addressToRead); // or simply by using the properties short measurementByPropertyFromModulePoco = readByModulePocoResult.Value1; }