Class ModbusTCPProtocol
Implementation of Brainboxes.IO.IIOProtocol using the Modbus TCP binary protocol. Used for ED-series devices on TCP port 502.
Assembly: Brainboxes.IO.dll
View Source
public class ModbusTCPProtocol : Protocol, IIOProtocol, IProtocol
Inheritance: System.Object -> Brainboxes.IO.Protocol
Implements:
Brainboxes.IO.IIOProtocol, Brainboxes.IO.IProtocol
Properties
LastRequest
The last Modbus TCP Request sent encoded as string
View Source
public string LastRequest { get; }
LastResponse
The Modbus TCP Response for the last Request sent note this could be null as the request may not have completed at time of asking
View Source
public string LastResponse { get; }
TransactionId
The ModbusTCP transaction Id starts at one and should be incremented each time from Modbus.org: Transaction Identifier - It is used for transaction pairing, the MODBUS server copies in the response the transaction identifier of the request.
View Source
public int TransactionId { get; }
ProtocolId
The ModbusTCP protocol, always 0 From Modbus.org: Protocol Identifier – It is used for intra-system multiplexing. The MODBUS protocol is identified by the value 0.
View Source
public int ProtocolId { get; }
UnitId
The ModbusTCP Unit identifier, From Modbus.org: Unit Identifier – This field is used for intra-system routing purpose. It is typically used to communicate to a MODBUS+ or a MODBUS serial line slave through a gateway between an Ethernet TCP-IP network and a MODBUS serial line.This field is set by the MODBUS Client in the request and must be returned with the same value in the response by the server. For our purposes: always 0xff
View Source
public int UnitId { get; }
RequestLength
The Modbus TCP Length of Request From Modbus.rog: Length - The length field is a byte count of the following fields, including the Unit Identifier and data fields.
View Source
public int RequestLength { get; }
ResponseLength
The Modbus TCP Length of Response From Modbus.rog: Length - The length field is a byte count of the following fields, including the Unit Identifier and data fields.
View Source
public int ResponseLength { get; }
DeviceName
The name of the ED Device
View Source
public string DeviceName { get; set; }
Fields
_requestHistory
The last 64 requests sent to the device indexed by transactionId % 64
View Source
protected byte[][] _requestHistory
_responseHistory
The responses which match the last 64 requests sent to the device
View Source
protected byte[][] _responseHistory
_transactionId
The ModbusTCP transaction Id starts at one and should be incremented each time from Modbus.org: Transaction Identifier - It is used for transaction pairing, the MODBUS server copies in the response the transaction identifier of the request.
View Source
protected int _transactionId
_protocolId
The ModbusTCP protocol, always 0 From Modbus.org: Protocol Identifier – It is used for intra-system multiplexing. The MODBUS protocol is identified by the value 0.
View Source
protected readonly int _protocolId
_unitId
The ModbusTCP Unit identifier, From Modbus.org: Unit Identifier – This field is used for intra-system routing purpose. It is typically used to communicate to a MODBUS+ or a MODBUS serial line slave through a gateway between an Ethernet TCP-IP network and a MODBUS serial line.This field is set by the MODBUS Client in the request and must be returned with the same value in the response by the server. For our purposes: always 0xff
View Source
protected readonly int _unitId
_reqLength
The Modbus TCP Length of Request From Modbus.rog: Length - The length field is a byte count of the following fields, including the Unit Identifier and data fields.
View Source
protected int _reqLength
_resLength
The Modbus TCP Length of Response From Modbus.rog: Length - The length field is a byte count of the following fields, including the Unit Identifier and data fields.
View Source
protected int _resLength
_ioCounterUpdateDirection
The update direction of the digital input counter
View Source
protected IOChangeTypes _ioCounterUpdateDirection
_deviceName
The name of the Brainboxes ED Device
View Source
protected string _deviceName
Methods
GetRequestForTransactionId(int)
Get Modbus TCP Request for a particular TransactionId
View Source
public string GetRequestForTransactionId(int transId)
Returns
System.String
Parameters
| Type | Name |
|---|---|
System.Int32 | transId |
GetResponseForTransactionId(int)
Get Modbus TCP response for particular TransactionId
View Source
public string GetResponseForTransactionId(int transId)
Returns
System.String
Parameters
| Type | Name |
|---|---|
System.Int32 | transId |
PrettyPrintModbusADU(string)
Take a Modbus Command written in ASCII as a hex representation and prettifies it. e.g. 006600000006ff0100000010 0066 0000 0006 ff 01 0000 0010 TranID|ProtID|Len|UnitID|Func|DATA there is a space every 4 characters, and also a space at the 14 char between the UnitID and the Function code
View Source
public string PrettyPrintModbusADU(string adu)
Returns
System.String
Parameters
| Type | Name | Description |
|---|---|---|
System.String | adu | Application Data Unit as defined by ModbusTCP spec, encoded in ASCII as hex |
PrettyPrintModbusADU(byte[])
Take a Modbus Command written in a hex byte array representation and prettifies it. e.g. 0x006600000006ff0100000010 0066 0000 0006 ff 01 0000 0010 TranID|ProtID|Len|UnitID|Func|DATA there is a space every 4 characters, and also a space at the 15 char between the UnitID and the Function code
View Source
public string PrettyPrintModbusADU(byte[] adu)
Returns
System.String
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte[] | adu | Application Data Unit as defined by ModbusTCP spec |
SendCommand(string)
Send a ModbusTCP command (ADU) to a Brainboxes Device and return response <b>The MBAP header is handled internally by this class, only the function and data need to be sent as a command</b> Any spacing is ignore, commands are hex encoded ASCII (e.g. "01 0000 0001") Modbus TCP commands are defined as follows: ADU - Application Data Unit, split into: MBAP Header - the header contains 4 fields:
- transactionId (2 bytes) = id starting at 01 increments by 1 for each new ADU sent
- protocol Id (2 bytes) = always 0x00 = modbus for brainboxes devices
- Length (2 bytes) = the total length in bytes of rest of the ADU from that point
- Unit Id (1 byte) = set by the client in the request the response must contain the same data, always 0xff
- Function Code (2 bytes)- the type of function requested can be:
- 0x01 = Read Coils
- 0x02 = Read Discrete Inputs
- 0x03 = Read Holding Registers
- 0x04 = Read Input Registers
- 0x05 = Write Single Coil
- 0x06 = Write Single Register
- Data (variable length) - The end of the modbusTCP command, which contains data relevant to the function code The format of the command is hex encoded values in ASCII,
The response is of the format:
- transactionId (2 bytes) = is the same transaction ID the PC used in its request
- protocol Id (2 bytes) = is the protocol which is always 00 for Modbus TCP
- Length (2 bytes): is the number of bytes in the rest of the transaction
- Unit Id (1 byte): is the Unit Identifier =0xFF
- Function Code (2 bytes): is the Modbus Function code 01= Read Multiple Coils
- Byte Code (2 bytes): Is the byte count of data to follow in this case 2 bytes
- Data: is the value of data that corresponds to the function code
View Source
public string SendCommand(string pdu)
Returns
System.String: response from the ED device without modbus header MBAP
Parameters
| Type | Name | Description |
|---|---|---|
System.String | pdu | Protocol Data Unit: Function Code and Data of the Modbus TCP command, spaces and dashes are ignored |
SendCommand(byte[])
Send a ModbusTCP command (ADU) to a Brainboxes Device and return response <b>The MBAP header is handled internally by this class, only the function and data need to be sent as a command</b> Any spacing is ignore, commands as encoded hex byte array e.g. 0x0100000001 Modbus TCP commands are defined as follows: ADU: Application Data Unit, split into: MBAP and PDU MBAP Header - the header contains 4 fields:
- transactionId (2 bytes) = id starting at 01 increments by 1 for each new ADU sent
- protocol Id (2 bytes) = always 0x00 = modbus for brainboxes devices
- Length (2 bytes) = the total length in bytes of rest of the ADU from that point
- Unit Id (1 byte) = set by the client in the request the response must contain the same data, always 0xff PDU: Protocol Data Unit, consists of 2 fields: Function Code (2 bytes)- the type of function requested can be:
- 0x01 = Read Coils
- 0x02 = Read Discrete Inputs
- 0x03 = Read Holding Registers
- 0x04 = Read Input Registers
- 0x05 = Write Single Coil
- 0x06 = Write Single Register Data (variable length) - The end of the modbusTCP command, which contains data relevant to the function code The format of the command is hex encoded values in ASCII,
View Source
public byte[] SendCommand(byte[] pdu)
Returns
System.Byte[]: response from the ED device without modbus header MBAP
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte[] | pdu | Protocol Data Unit: Function Code and Data of the Modbus TCP command |
SetOutputLineState(int, int)
Set an individual output line state open (0) or closed (1)
View Source
[Obsolete("SetOutputLineState is deprecated. Replaced by SetDigitalOutputLineState")]
public void SetOutputLineState(int line, int state)
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Int32 | state |
SetDigitalOutputLineState(int, int)
Set an individual output line state open (0) or closed (1)
View Source
public void SetDigitalOutputLineState(int line, int state)
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Int32 | state |
SetAnalogOutputLineState(int, double)
Set an individual analog output line value
View Source
public void SetAnalogOutputLineState(int line, double value)
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Double | value |
GetAnalogLineState(int, int, bool)
Get an individual analog line value
View Source
public double GetAnalogLineState(int line, int numberOfLines = 8, bool isInput = true)
Returns
System.Double
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Int32 | numberOfLines |
System.Boolean | isInput |
GetAllAnalogInputLineStates(int)
Get all analog input values
View Source
public double[] GetAllAnalogInputLineStates(int numberOfInputs = 8)
Returns
System.Double[]
Parameters
| Type | Name |
|---|---|
System.Int32 | numberOfInputs |
GetAllAnalogOutputLineStates(int)
Get all analog output values
View Source
public double[] GetAllAnalogOutputLineStates(int numberOfOutputs = 4)
Returns
System.Double[]
Parameters
| Type | Name |
|---|---|
System.Int32 | numberOfOutputs |
SetAllOutputLineStates(int, int)
Set all output lines on the device open (0) or closed (1) The bit position represents the line number
View Source
[Obsolete("SetAllOutputLineStates is deprecated. Replaced by SetAllDigitalOutputLineStates")]
public void SetAllOutputLineStates(int states, int numberOfOutputs = 8)
Parameters
| Type | Name | Description |
|---|---|---|
System.Int32 | states | The bit position represents the line number |
System.Int32 | numberOfOutputs | The number of outputs on the device |
SetAllDigitalOutputLineStates(int, int)
Set all output lines on the device open (0) or closed (1) The bit position represents the line number
View Source
public void SetAllDigitalOutputLineStates(int states, int numberOfOutputs = 8)
Parameters
| Type | Name | Description |
|---|---|---|
System.Int32 | states | The bit position represents the line number |
System.Int32 | numberOfOutputs | The number of outputs on the device |
GetLineState(int, bool)
Get the state of a digital IO Line
View Source
[Obsolete("GetLineState is deprecated. Replaced by GetDigitalLineState")]
public int GetLineState(int line, bool isInput = true)
Returns
System.Int32: 1: Line closed/High/On, 0: Line Open/Low/Off
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Boolean | isInput |
GetDigitalLineState(int, bool)
Get the state of a digital IO Line
View Source
public int GetDigitalLineState(int line, bool isInput = true)
Returns
System.Int32: 1: Line closed/High/On, 0: Line Open/Low/Off
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
System.Boolean | isInput |
GetAllInputStates()
Get the state of the inputs on the device
View Source
public int GetAllInputStates()
Returns
System.Int32
GetAllOutputStates()
Get all output line states
View Source
public int GetAllOutputStates()
Returns
System.Int32
GetAllLineStates()
Get the state of all the Digital IOLines of the device as an integer the top 16 bits are the outputs, the bottom 16 bits are the inputs
View Source
[Obsolete("GetAllLineStates is deprecated. Replaced by GetAllDigitalLineStates")]
public int GetAllLineStates()
Returns
System.Int32: Each bit in the integer represents an IOLine state
GetAllDigitalLineStates()
Get the state of all the Digital IOLines of the device as an integer the top 16 bits are the outputs, the bottom 16 bits are the inputs
View Source
public int GetAllDigitalLineStates()
Returns
System.Int32: Each bit in the integer represents an IOLine state
_sendCommand(byte[], BBStream)
Any command sent to the ED Device is sent by this function
View Source
protected byte[] _sendCommand(byte[] command, BBStream s)
Returns
System.Byte[]: response from the ED device
Parameters
| Type | Name | Description |
|---|---|---|
System.Byte[] | command | correctly encoded modbusTCP command |
| Brainboxes.IO.BBStream | s | The stream to send the command on (captured locally to avoid race conditions) |
GetDeviceName()
Get the name of the Device
View Source
public string GetDeviceName()
Returns
System.String
ResetToFactoryDefaultSettings()
Reset the ED device to factory default settings
View Source
public void ResetToFactoryDefaultSettings()
Restart()
Power Off and the On the ED Device
View Source
public void Restart()
GetAllLatchedHighInputStates()
Get the HIGH LATCH states of all the Digital Inputs
View Source
[Obsolete("GetAllLatchedHighInputStates is deprecated. Replaced by GetAllLatchedHighDigitalInputStates")]
public int GetAllLatchedHighInputStates()
Returns
System.Int32
GetAllLatchedHighDigitalInputStates()
Get the HIGH LATCH states of all the Digital Inputs
View Source
public int GetAllLatchedHighDigitalInputStates()
Returns
System.Int32
GetAllLatchedLowInputStates()
Get the LOW LATCH state of all the Digital INPUTS
View Source
[Obsolete("GetAllLatchedLowInputStates is deprecated. Replaced by GetAllLatchedLowDigitalInputStates")]
public int GetAllLatchedLowInputStates()
Returns
System.Int32
GetAllLatchedLowDigitalInputStates()
Get the LOW LATCH state of all the Digital INPUTS
View Source
public int GetAllLatchedLowDigitalInputStates()
Returns
System.Int32
GetAllLatchedInputStates(bool)
Get the LATCH state of the INPUTS
View Source
protected int GetAllLatchedInputStates(bool isHigh = true)
Returns
System.Int32
Parameters
| Type | Name | Description |
|---|---|---|
System.Boolean | isHigh | Whether to get the HIGH or LOW LATCH |
ClearAllLatchedInputs()
Clear the Digital INPUT latches
View Source
[Obsolete("ClearAllLatchedInputs is deprecated. Replaced by ClearAllLatchedDigitalInputs")]
public void ClearAllLatchedInputs()
ClearAllLatchedDigitalInputs()
Clear the INPUT latches
View Source
public void ClearAllLatchedDigitalInputs()
GetLineCount(int)
Reads the digital input counter of the specified channel
View Source
[Obsolete("GetLineCount is deprecated. Replaced by GetDigitalInputLineCount")]
public int GetLineCount(int line)
Returns
System.Int32
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
GetDigitalInputLineCount(int)
Reads the digital input counter of the specified channel
View Source
public int GetDigitalInputLineCount(int line)
Returns
System.Int32
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
ClearLineCount(int)
Clears the digital input counter of the specified line,
View Source
[Obsolete("ClearLineCount is deprecated. Replaced by ClearDigitalInputLineCount")]
public void ClearLineCount(int line)
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
ClearDigitalInputLineCount(int)
Clears the digital input counter of the specified line.
View Source
public void ClearDigitalInputLineCount(int line)
Parameters
| Type | Name |
|---|---|
System.Int32 | line |
GetDeviceConfiguration()
Read the device configuration
View Source
public void GetDeviceConfiguration()
SetDeviceConfiguration(int, int, IOChangeTypes, CounterMode, bool)
Sets configuration parameters for a digital IO device.
View Source
public void SetDeviceConfiguration(int newAddress, int newBaudRate, IOChangeTypes ioCounterUpdateDirection, CounterMode counterMode, bool checksum)
Parameters
| Type | Name | Description |
|---|---|---|
System.Int32 | newAddress | The new ASCII/Modbus address for the device (0-255). |
System.Int32 | newBaudRate | The new baud rate (e.g. 9600, 115200). |
| Brainboxes.IO.IOChangeTypes | ioCounterUpdateDirection | Whether the input counter increments on Brainboxes.IO.IOChangeTypes.RisingEdge or Brainboxes.IO.IOChangeTypes.FallingEdge. |
| Brainboxes.IO.CounterMode | counterMode | The counter bit width: 16-bit or 32-bit. |
System.Boolean | checksum | Whether to enable checksum validation on commands. |
Exceptions
System.NotImplementedException
Thrown by Brainboxes.IO.ModbusTCPProtocol — device configuration is only supported via the ASCII protocol.
SetDeviceConfiguration(int, int, AnalogDataFormat, bool, bool, bool, TemperatureUnit)
Sets configuration parameters for an analog IO device.
View Source
public void SetDeviceConfiguration(int newAddress, int newBaudRate, AnalogDataFormat newAnalogDataFormat, bool newFilterSettings, bool newModuleSettings, bool newChecksum, TemperatureUnit temperatureUnit)
Parameters
| Type | Name | Description |
|---|---|---|
System.Int32 | newAddress | The new ASCII/Modbus address for the device (0-255). |
System.Int32 | newBaudRate | The new baud rate (e.g. 9600, 115200). |
| Brainboxes.IO.AnalogDataFormat | newAnalogDataFormat | The analog data format: Brainboxes.IO.AnalogDataFormat.Engineering, Brainboxes.IO.AnalogDataFormat.FullScaleRange, or Brainboxes.IO.AnalogDataFormat.Hexadecimal. |
System.Boolean | newFilterSettings | Whether to enable input filtering. |
System.Boolean | newModuleSettings | Whether to enable module-specific settings. |
System.Boolean | newChecksum | Whether to enable checksum validation on commands. |
| Brainboxes.IO.TemperatureUnit | temperatureUnit | The temperature unit for thermocouple/RTD devices: Brainboxes.IO.TemperatureUnit.Celsius, Brainboxes.IO.TemperatureUnit.Fahrenheit, or Brainboxes.IO.TemperatureUnit.Kelvin. |
Exceptions
System.NotImplementedException
Thrown by Brainboxes.IO.ModbusTCPProtocol — device configuration is only supported via the ASCII protocol.