Repository navigation
2.5 Abstract Protocol Layer Protocol
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.
-
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.
classDiagram
class IProtocol {
<<interface>>
+ConnectAsync() Task~bool~
+Disconnect() bool
+SendReceiveAsync(params object[]) Task
}
class BaseProtocol {
#ProtocolLinker: IProtocolLinker
+this[Type type] ProtocolUnit
+ConnectAsync()
+SendReceiveAsync()
}
class ProtocolUnit~TParamIn,TParamOut~ {
+Format(IInputStruct) TParamIn
+Unformat(TParamOut, ref pos) IOutputStruct
}
IProtocol <|.. BaseProtocol
BaseProtocol o-- ProtocolUnit : lazy-loads
ProtocolUnit <|-- ReadDataModbusProtocol
ProtocolUnit <|-- WriteDataModbusProtocol
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.
sequenceDiagram
participant U as IUtilityMethod
participant P as IProtocol
participant PL as IProtocolLinker
U->>P: SendReceiveAsync(params)
P->>P: ProtocolUnit.Format() -> byte[]
P->>PL: SendReceiveAsync(byte[])
PL-->>P: response byte[]
P->>P: ProtocolUnit.Unformat() -> IOutputStruct
P-->>U: IOutputStruct
Modbus.Net © 2016-2026 Hangzhou Delian Science Technology Co.,Ltd. | Licensed under MIT
- 1-概要介绍
- 2-Modbus.Net主体框架规范
- 3-利用Modbus.Net实现一个协议
- 4-使用Modbus.Net
- 5-Modbus.Net.Modbus
- 6-Modbus.Net.Siemens
- 7-Modbus.Net.HJ212
- 8-调试与Issue提交
- 1-Overview
-
2-Specification of Modbus.Net main framework
- 2.1-Modbus.Net Architecture
- 2.2-Transmission Control Layer Controller
- 2.3-Transmission Link Layer Connector
- 2.4-Concrete Protocol Layer ProtocolLinker
- 2.5-Abstract Protocol Layer Protocol
- 2.6-Protocol Presentation Layer Utility
- 2.7-Device Application Layer Machine
- 2.8-Task Application Layer MachineJob
- 3-Using Modbus.Net to implement a protocol
- 4-Using Modbus.Net
- 5-Modbus.Net.Modbus
- 6-Modbus.Net.Siemens
- 7-Modbus.Net.HJ212
- 8-Debug method and issue upload