Calling RpcMethods and accessing PLC Properties
RpcMethods can be directly called type safe on the value dynamically read by a symbolic Read-TcValue. The RpcMethod In-/ and Out-/ Parameters are automatically marshalled to/from they .NET counterparts – so that the access is easy and transparently.
Accessing the RPC Methods needs to open an ADS Session and loading the symbolic information. Here we open a new session to the local PLC.
PS> $session = new-tcSession -port 851
PS> $rpcSymbol | Get-TcSymbol -path 'MAIN.fbRpc'
The ‘rpcSymbol’ object now contains the Symbol of the FB instance ‘fbRcp’.
This FB has a RpcMethod ‘AddValues’ with 2 INT parameters and a Property ‘PropInt’ defined (on the PLC side).
Information about these defined symbol instance can be browsed.
PS> $rpcSymbol
InstancePath Category DataType Size Static Persistant IG IO
------------ -------- -------- ---- ------ ---------- -- --
MAIN.fbRpc Struct FB_RpcPou 1152 False False 4040 10DEE8
Beneath Properties and Methods of the Instance base class, the ‘rpcSymbol’ contains dynamically generated access properties and methods.
PS> $rpcSymbol | Get-Member
TypeName: TwinCAT.TypeSystem.DynamicStructInstance
…
Name MemberType Definition
---- ---------- ----------
AddValues Dynamic dynamic AddValues
AddValuesAsync Dynamic dynamic AddValuesAsync
PropInt Dynamic dynamic PropInt
….
Accessing the Rpc Method metadata of the symbol:
PS> $rpcSymbol.RpcMethods
Name ReturnType Declaration
---- ---------- -----------
AddValues INT INT AddValues([in] INT i1,[in] INT i2)
__getPropInt INT INT __getPropInt()
__setPropInt __setPropInt([in] INT PropInt)
The method can be called transparently in Powershell
PS> $rpcSymbol.AddValues(39,3)
42
The same is true for the dynamic property
PS> $rpcSymbol.PropInt
InstancePath Category DataType Size Static Persistant IG IO
------------ -------- -------- ---- ------ ---------- -- --
MAIN.fbRpc.PropInt Primitive INT 2 False False 4040 10DEE8
PS> $rpcSymbol.PropInt | Read-TcValue
29224