Master Secret in eine PLC laden - TIAPortal

TIA Portal Openness: API für die Automatisierung von Engineering-Workflows

ft:publication_title
TIA Portal Openness: API für die Automatisierung von Engineering-Workflows
Product
TIAPortal
Version
V20
Publication date
01/2025
Language
de-DE
Master Secret in eine PLC laden

Voraussetzung

Verwendung

Mit TIA Portal Openness können Sie Master Secrets über PlcMasterSecretPassword mit Zugriff über die Download-Konfiguration in die PLC laden. Für das Laden müssen Sie ein leeres DownloadConfigurationDelegate (DownloadConfiguration downloadConfiguration) für die Konfiguration vor dem Download implementieren.

Dieses Delegate wird für jede Konfiguration aufgerufen, die eine Handlung des Benutzers erfordert. Die PlcMasterSecretPassword -Konfiguration verarbeitet die Benutzerhandlung für die Eingabe des Master-Secret-Passworts.

Weitere Informationen zur Handhabung von Rückrufen und die Unterstützung des Downloads in PLC-Geräte finden Sie unter Unterstützung von Callbacks und Herunterladen zu den PLCs.

Programmcode: PlcMasterSecretPassword Download

Mit dem folgenden Beispielcode können Sie Software- und Hardware-Komponenten über den Dienst DownloadProvider in ein Gerät laden. Für Informationen über IsSecureCommunication siehe Unterstützung der sicheren S7-Kommunikation mit TLS

Kopiert den nachfolgenden Programmcode in die Zwischenablage.

internal bool DownloadDevice(DeviceItem deviceItem)

{

try

{

//Download software and hardware components to a device via DownloadProvider service that

//can be acquired from a given DeviceItem. If a DeviceItem represents a download able

//target an instance of DownloadProvider will be returned on GetService call. Otherwise

//it will be null.

DownloadProvider myDownloadProvider = deviceItem.GetService<DownloadProvider>();

if (myDownloadProvider == null)

{

return false;

}

//Preparation for IConfigurable object to establish a connection to the device. IConfiguration

//interface is implemented by ConfigurationAddress and ConfigurationTargetInterface. Both

//objects can be accessed via ConnectionConfiguration instance. ConnectionConfiguration

//instance can be acquired from DownloadProvider.Configuration

ConnectionConfiguration connConfiguration = myDownloadProvider.Configuration;

ConfigurationMode confMode = connConfiguration.Modes.Find("PN/IE");

ConfigurationPcInterface pcInterface = ("Intel(R) Ethernet Connection I217-LM", 1);

ConsoleOutput.PrintInterfaces(connConfiguration);

//In order to execute a download to a device, Download method of DownloadProvider have to

//be called. The parameters are an IConfiguration object, two delegates and DownloadOptions

// (Hardware, Software or Hardware and Software).

IConfiguration targetConfiguration = pcInterface.TargetInterfaces[0];

DownloadConfigurationDelegate preDownloadConfigurationDelegate = PreConfigureDownload;

DownloadConfigurationDelegate postDownloadConfigurationDelegate = PostConfigureDownload; DownloadOptions downloadOptions = DownloadOptions.Hardware | DownloadOptions.Software;

// Apply the the configuration and start the download myDownloadProvider.Configuration.ApplyConfiguration(pcInterface.TargetInterfaces.First());

var downLoadResult = myDownloadProvider.Download(targetConfiguration, preDownloadConfigurationDelegate,

postDownloadConfigurationDelegate, downloadOptions);

return downLoadResult != null && downLoadResult.State != DownloadResultState.Error;

}

catch (EngineeringTargetInvocationException ex)

{

//Exception catch-ed on purpose. Client can write own code to handle the exception

//In case of PLC Master Secret exception will be thrown if:

// 1) If a different Master Secret has already been set in the PLC.

//Exception Message: An error has occurred during download: 'A different Master Secret

//has already been set. The PLC has to be reset!

//Possibly provide custom exception handling here

Console.WriteLine("An exception during download was thrown:");

Console.WriteLine(ex.DetailMessageData);

return false;

}

}

Zur Handhabung von PlcMasterSecretPassword in PreDownloadDelegate können Sie den folgenden Beispielcode verwenden:

Kopiert den nachfolgenden Programmcode in die Zwischenablage.

private static void PreConfigureDownload(DownloadConfiguration downloadConfiguration)

{... ... ... ... ...

// Set the PLC Master Secret password configured in the offline project's PLC,

// when the password was never set in the real PLC

if (downloadConfiguration is PlcMasterSecretPassword plcMasterSecret)

{

// Get the PLC Master Secret password from a secure location

SecureString plcMasterSecretPsw = ...;

plcMasterSecret.SetPassword(plcMasterSecretPsw);

return;

}

catch (EngineeringTargetInvocationException ex)

{

// Exception catch-ed on purpose. Client can write own code to handle the exception

// Exception will be thrown if:

// 1) plcMasterSecret password is null.

// Exception Message: The argument "password" may not be null

// 2) plcMasterSecret password is an empty string.

// Exception Message: The password provided is invalid

// 3) plcMasterSecret password is wrong.

// Exception Message: The password provided is invalid

// Possibly provide custom exception handling here

Console.WriteLine("PlcMasterSecretPassword has thrown an exception:");

Console.WriteLine(ex.DetailMessageData);

return;

}

}

... ... ... ... ...

throw new NotSupportedException();

// Exception thrown in the delagate will cancel download

}

Hinweis

Bei einer nicht verarbeiteten Ausnahme im Delegate wird eine EngineeringDelegateInvocationException ausgelöst.