This is a wrapper of the Beckhoff TcAdsServer
base class, allowing the usage of middlewares to extend the server functionalities.
namespace Poh.PLC.Beckhoff.ADS
The Server
object is the go-to object to be instantiated in the main program. It converts the requests into the AdsContext
object. The AdsContext
is passed on to the middleware to be handled accordingly.
Server _server = new Server(_port, _name);
// Additional stuffs
// Add middlewares
// ...
_server.Start();
namespace Poh.PLC.Beckhoff.ADS.Extensions
Extend the abstract MiddlewareBase class or implement the IMiddleware interface.
When implementing the MiddlewareBase class, override the bool Capture(AdsContext)
and void Bubble(AdsContext)
method. Middlewares can be chained together, and the chain propagation can be stopped by returning true
in the Capture
method.
Sample middleware implementation
class MyMiddleware : MiddlewareBase {
public override void Bubble(AdsContext context) {
//Do stuff
//Otherwise
return;
}
public override bool Capture(AdsContext context) {
if (context.Request.Command == AdsCommand.Read && context.Request.IndexGroup == 0x1000) {
if (context.Request.IndexOffset == 0x0001 && context.Request.Length == 0x0003) {
context.Reponse.Error = Error.ApiError;
}
else {
context.Reponse.Error = Error.NoError;
context.Response.Payload = new byte[] {0,1,2};
}
return true; //To prevent further propagation down the middleware chain
}
return false:
}
}
Sample middleware usage
Server _server = new Server(_port, _name);
_server.Use(new MyMiddleware);
// ...
_server.Start();
//...
_server.Use(MiddlewareA);
_server.Use(MiddlewareB);
_server.Use(MiddlewareC);
//...
The AdsContext
will be processed MiddlewareA.Capture → MiddlewareB.Capture → MiddlewareC.Capture → MiddlewareC.Bubble → MiddlewareB.Bubble → MiddlewareA.Bubble