用于 SIMATIC S7 PLC Web 服务器 API 的 .NET API 客户端库
This package targeting .NET Framework: 4.8, 6.0, 7.0, 8.0, 9.0, 10.0 and .NET Standard 2.0 and above provides the user with Method calls to the PLC Webserver Api using the HttpClient to make the usage of Api functionalities easier.
This package provides an easy way to use the functionalities of the S7 Webserver API without taking care of the HTTP requests.
In the area of the requesthandling following comfort is in place:
So generally all available methods of the S7 Web API can be called easily without taking care of RequestHandling, Id generation, result parsing, etc. Furthermore some comfort functionality is implemented:
The Functionalities come together in the ApiHttpClientRequestHandler that implements the IAsyncApiRequesthandler (and also in any implementation of the IApiRequestHandler - not given by example).
Further examples of usage are also provided in the UnitTests of the component.
| Package Name | Nuget Package URL | Project URL(s) |
|---|---|---|
| NETStandard.Library | https://www.nuget.org/packages/NETStandard.Library/ | https://dot.net/, https://github.com/dotnet/runtime |
| Microsoft.SourceLink.GitHub | https://www.nuget.org/packages/Microsoft.SourceLink.GitHub/ | https://github.com/dotnet/sourcelink |
| System.Net.Http | https://www.nuget.org/packages/system.net.http/ | https://dot.net/, https://github.com/microsoft/referencesource/tree/master/System/net/System/Net/Http |
| Microsoft.Extensions.Logging.Abstractions | https://www.nuget.org/packages/Microsoft.Extensions.Logging.Abstractions/ | https://dot.net/, https://github.com/dotnet/runtime |
| Newtonsoft.Json | https://www.nuget.org/packages/Newtonsoft.Json/ | https://www.newtonsoft.com/json, https://github.com/JamesNK/Newtonsoft.Json |
| GitVersioning | https://www.nuget.org/packages/Nerdbank.GitVersioning/ | https://github.com/dotnet/Nerdbank.GitVersioning |
| MimeMapping | https://www.nuget.org/packages/MimeMapping/ | https://github.com/zone117x/MimeMapping |
Depending on the plc you are having a different number of sessions can co-exist during operation. For details please check the manual.
See ApiHttpClientRequestHandler.
See ApiHttpClientRequestHandler, if you are able to successfully send the ApiBrowse request and log the responses the setup is fine.
Depending on the requests sent and the plc state there might be errors occurring. As an example there can only be 1 ApiTicket which needs to be closed after use. Therefore when calling e.g. WebApp.CreateResource the result might look like the following: {"error":{"code":4,"message":"No Resources"},"id":"81dcdb3c-0704-454f-94a3-364a75b7eb4d","jsonRpc":"2.0"} In this case the ApiHttpClientRequestHandler will throw an ApiNoResourcesException containing different scenarios when this exception might occur.
In the following example code you'll have to adjust the IpAddress ("192.168.1.1") and the login for your system - the Login "Anonymous"(previously "Everybody") with the password "" is a default user that is downloaded to the plc for the webserver once the webserver is activated in TIA portal. The used functions would fail for the default case that he doesn't have the permissions call the method (that he tried to call). It is not good practice to give a lot of rights to the user "Anonymous" therefore please adjust the connectionconfiguration to your configured user with a safe password that should be able to call the methods.
To use e.g. the Api Method "Api.Browse" to get all the Methods supported by the PLC Api do the following
var serviceFactory = new ApiStandardServiceFactory();
var reqHandler = await serviceFactory.GetApiHttpClientRequestHandlerAsync("192.168.1.1", "Anonymous", "");
var apiBrowseResponse = await reqHandler.ApiBrowseAsync();
foreach(var method in apiBrowseResponse.Result)
{
Console.WriteLine(method.Name);
}Hint: The PLC Certificate is by default not accepted by your PC - therefor you have to either download and store the certificate in the trusted root ca certificates (preferably!) or implement the validation callback for your system - a "plain" check for => true is not good practice but it will get you started in case you dont want to download and install the certificate.
...
using System.Net;
...
// For .net48
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// For .net6.0 and greater
Siemens.Simatic.S7.Webserver.API.Services.ServerCertificateCallback.CertificateCallback = (sender, cert, chain, sslPolicyErrors) => true;Of course you can also implement a check for the sender and so on.
For the PLC WebApps Further Comfort can be accomplished by using the ApiWebAppData and ApiWebAppResource implementations: Generally you are free to configure everything on your own:
…but further comfort can be accomplished with:
You can use Implementations to comfortably deploy the apps to the plc with a Deployer and FileParser for your WebAppDirectory:
…The Parser and Deployer also use a set of comfort functions - in case you want to know or take a look they are using:
namespace Siemens.Simatic.S7.Webserver.API.Services.WebApp {
public class ApiResourceHandler {}
public class ApiWebAppResourceBuilder {}
}Hint: It is possible to add another parameter to the new WebAppConfigParser(,,bool ignoreBOMDifference = false): This is the case because uploading files using javascript on a webpage has shown that the BOM (Byte Order Mark) is not transmitted! Therefor if you upload a file using javascript and then make the deployer do the comparison between the file locally (windows explorer) and the one on the server(plc) the files would be different since the size is different by three bytes. To get rid of this "false positive" on a difference you can set the ignoreBOMDifference to true - but keep in mind that a change of 3 bytes would then also be accepted as no difference. The deployer would still reupload the file since the last_modified dates would not match!
The PlcProgram Methods can be used to Browse available variables and to read or write their value.
…Also for these PlcProgram methods there is an Implementation for an ApiPlcProgramData object that can be used for the Api methods:
…The Modules.* methods of the plc provide access to various data.
The ModulesBrowser can retrieve all stations available at the plc:
var browser = new ModulesBrowser(requestHandler, Logger);
var browseResult = await browser.BrowseAllModulesAsync();Modules.ReadIdentificationMaintenance can be used in the library via 2 ways:
var hwIds = await requestHandler.ModulesBrowseAsync(mode: ModulesBrowseMode.Children);
var resp = await requestHandler.ModulesReadIdentificationMaintenanceAsync(hwIds.Result.Nodes.FirstOrDefault().Children.First().Hwid, ModulesReadIdentificationMaintenanceType.Actual);var hwIds = await requestHandler.ModulesBrowseAsync(mode: ModulesBrowseMode.Children);
var resp = await requestHandler.ModulesReadIdentificationMaintenanceAsync(hwIds.Result.Nodes.FirstOrDefault().Children.First().Hwid, ModulesReadIdentificationMaintenanceNumber.Im0, ModulesReadIdentificationMaintenanceType.Actual);Use the following table to find the correct client version for each Plc version (server)
| Plc Version (S7-1500 + RH) | Client Library Version | S7-1200 G2 | S7-1200 |
|---|---|---|---|
| 2.9.x | 1.0.x | 4.5.x | |
| 2.9.x | 2.0.x | 4.5.x | |
| 3.0.x | 2.1.x | 4.6.x | |
| 3.1.x | 2.2.x | 4.7.x | |
| 4.0.x | 3.0.x | 1.0.x | |
| 4.1.x | 3.3.x | 4.1.x |
Hint: This Library supports more API calls than the current S7-1200 (G2) does - the S7-1200 (G2) will likely(!) support the API calls of last version of the s7-1500 but this does NOT necessarily have to be the case - for details please always check the manual for the according version.
Currently some Featur
暂无开放 Issues,或尚未同步最近议题。