Skip to content

2.5 Abstract Protocol Layer Protocol

luosheng edited this page Jun 7, 2023 · 1 revision

Abstract Protocol Layer is a common core for one type of protocol, and the entrypoint of Concrete Protocol Layer.

IProtocol is the Abstract Protocol Layer of Modbus.Net, included a great number of ProtocolUnit.

Every ProtocolUnit is an abstract protocol, it is not the real protocol but common part of it.
I'll give an example from modbus.
RTU Procol is slave-protocol No.-Address-Data-CRC16
ASCII is slave-protocol no.-Address-Data-LRC
TCP is 0000-Slave-Protocol No.-Address-Data.
So the common part of them is slave-Protocol No.-Address-Data.

BaseProtocol implemented a lazy loading ProtocolUnit dictionary, to use this dictionary, only point type of ProtocolUnit to it and it will load the ProtocolUnit for users.

Implementation

  • ProtocolUnit

    ProtocolUnit inherits from IProtocolFormatting and has two methods: Format and Unformat.

     public abstract TParamIn Format(IInputStruct message);
    
     public abstract IOutputStruct Unformat(TParamOut messageBytes, ref int pos);

    Format change IInputStruct to input parameter, and Unformat change output parameter to IOutputStruct.

    You can reference ModbusProtocol.cs in Modbus.Net.Modbus to implement your own ProtocolUnit.

  • BaseProtocol

    BaseProtocol implement a lazy loading dictionary.

     public ProtocolUnit this[Type type]

    This dicionary uses type of ProtocolUnit for parameters. ProtocolUnit will be automaticlly loaded into dictionary when ProtocolUnit instance doesn't exist, otherwise directly loaded memory cache.

    When BaseProtocol wants to send and receive data, it directly call Format and Unformat to create and read protocol content.

    If you want to implement your own loading methods, inherit IProtocol rather than ProtocolUnit.

inner 4

Interface

When created IProtocol, it requires to add an IProtocolLinker, this linker meads how to send and receive IProtocol to IConnector.

IProtocol only has one main method called SendReceive(Async), when IUtilityMethod sends SendReceive(Async) to pass a message, IProtocol will change Protocol to input parameter(original byte[]), and send SendReceive messsage to IProtocolLinker.

outer 4

Home

Clone this wiki locally