Skip to main content
Version: v2.0.0

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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
public int ResponseLength { get; }

DeviceName​

The name of the ED Device

View Source​
Declaration
public string DeviceName { get; set; }

Fields​

_requestHistory​

The last 64 requests sent to the device indexed by transactionId % 64

View Source​
Declaration
protected byte[][] _requestHistory

_responseHistory​

The responses which match the last 64 requests sent to the device

View Source​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
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​
Declaration
protected int _resLength

_ioCounterUpdateDirection​

The update direction of the digital input counter

View Source​
Declaration
protected IOChangeTypes _ioCounterUpdateDirection

_deviceName​

The name of the Brainboxes ED Device

View Source​
Declaration
protected string _deviceName

Methods​

GetRequestForTransactionId(int)​

Get Modbus TCP Request for a particular TransactionId

View Source​
Declaration
public string GetRequestForTransactionId(int transId)
Returns​

System.String

Parameters​
TypeName
System.Int32transId

GetResponseForTransactionId(int)​

Get Modbus TCP response for particular TransactionId

View Source​
Declaration
public string GetResponseForTransactionId(int transId)
Returns​

System.String

Parameters​
TypeName
System.Int32transId

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​
Declaration
public string PrettyPrintModbusADU(string adu)
Returns​

System.String

Parameters​
TypeNameDescription
System.StringaduApplication 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​
Declaration
public string PrettyPrintModbusADU(byte[] adu)
Returns​

System.String

Parameters​
TypeNameDescription
System.Byte[]aduApplication 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​
Declaration
public string SendCommand(string pdu)
Returns​

System.String: response from the ED device without modbus header MBAP

Parameters​
TypeNameDescription
System.StringpduProtocol 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​
Declaration
public byte[] SendCommand(byte[] pdu)
Returns​

System.Byte[]: response from the ED device without modbus header MBAP

Parameters​
TypeNameDescription
System.Byte[]pduProtocol 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​
Declaration
[Obsolete("SetOutputLineState is deprecated. Replaced by SetDigitalOutputLineState")]
public void SetOutputLineState(int line, int state)
Parameters​
TypeName
System.Int32line
System.Int32state

SetDigitalOutputLineState(int, int)​

Set an individual output line state open (0) or closed (1)

View Source​
Declaration
public void SetDigitalOutputLineState(int line, int state)
Parameters​
TypeName
System.Int32line
System.Int32state

SetAnalogOutputLineState(int, double)​

Set an individual analog output line value

View Source​
Declaration
public void SetAnalogOutputLineState(int line, double value)
Parameters​
TypeName
System.Int32line
System.Doublevalue

GetAnalogLineState(int, int, bool)​

Get an individual analog line value

View Source​
Declaration
public double GetAnalogLineState(int line, int numberOfLines = 8, bool isInput = true)
Returns​

System.Double

Parameters​
TypeName
System.Int32line
System.Int32numberOfLines
System.BooleanisInput

GetAllAnalogInputLineStates(int)​

Get all analog input values

View Source​
Declaration
public double[] GetAllAnalogInputLineStates(int numberOfInputs = 8)
Returns​

System.Double[]

Parameters​
TypeName
System.Int32numberOfInputs

GetAllAnalogOutputLineStates(int)​

Get all analog output values

View Source​
Declaration
public double[] GetAllAnalogOutputLineStates(int numberOfOutputs = 4)
Returns​

System.Double[]

Parameters​
TypeName
System.Int32numberOfOutputs

SetAllOutputLineStates(int, int)​

Set all output lines on the device open (0) or closed (1) The bit position represents the line number

View Source​
Declaration
[Obsolete("SetAllOutputLineStates is deprecated. Replaced by SetAllDigitalOutputLineStates")]
public void SetAllOutputLineStates(int states, int numberOfOutputs = 8)
Parameters​
TypeNameDescription
System.Int32statesThe bit position represents the line number
System.Int32numberOfOutputsThe 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​
Declaration
public void SetAllDigitalOutputLineStates(int states, int numberOfOutputs = 8)
Parameters​
TypeNameDescription
System.Int32statesThe bit position represents the line number
System.Int32numberOfOutputsThe number of outputs on the device

GetLineState(int, bool)​

Get the state of a digital IO Line

View Source​
Declaration
[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​
TypeName
System.Int32line
System.BooleanisInput

GetDigitalLineState(int, bool)​

Get the state of a digital IO Line

View Source​
Declaration
public int GetDigitalLineState(int line, bool isInput = true)
Returns​

System.Int32: 1: Line closed/High/On, 0: Line Open/Low/Off

Parameters​
TypeName
System.Int32line
System.BooleanisInput

GetAllInputStates()​

Get the state of the inputs on the device

View Source​
Declaration
public int GetAllInputStates()
Returns​

System.Int32

GetAllOutputStates()​

Get all output line states

View Source​
Declaration
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​
Declaration
[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​
Declaration
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​
Declaration
protected byte[] _sendCommand(byte[] command, BBStream s)
Returns​

System.Byte[]: response from the ED device

Parameters​
TypeNameDescription
System.Byte[]commandcorrectly encoded modbusTCP command
Brainboxes.IO.BBStreamsThe stream to send the command on (captured locally to avoid race conditions)

GetDeviceName()​

Get the name of the Device

View Source​
Declaration
public string GetDeviceName()
Returns​

System.String

ResetToFactoryDefaultSettings()​

Reset the ED device to factory default settings

View Source​
Declaration
public void ResetToFactoryDefaultSettings()

Restart()​

Power Off and the On the ED Device

View Source​
Declaration
public void Restart()

GetAllLatchedHighInputStates()​

Get the HIGH LATCH states of all the Digital Inputs

View Source​
Declaration
[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​
Declaration
public int GetAllLatchedHighDigitalInputStates()
Returns​

System.Int32

GetAllLatchedLowInputStates()​

Get the LOW LATCH state of all the Digital INPUTS

View Source​
Declaration
[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​
Declaration
public int GetAllLatchedLowDigitalInputStates()
Returns​

System.Int32

GetAllLatchedInputStates(bool)​

Get the LATCH state of the INPUTS

View Source​
Declaration
protected int GetAllLatchedInputStates(bool isHigh = true)
Returns​

System.Int32

Parameters​
TypeNameDescription
System.BooleanisHighWhether to get the HIGH or LOW LATCH

ClearAllLatchedInputs()​

Clear the Digital INPUT latches

View Source​
Declaration
[Obsolete("ClearAllLatchedInputs is deprecated. Replaced by ClearAllLatchedDigitalInputs")]
public void ClearAllLatchedInputs()

ClearAllLatchedDigitalInputs()​

Clear the INPUT latches

View Source​
Declaration
public void ClearAllLatchedDigitalInputs()

GetLineCount(int)​

Reads the digital input counter of the specified channel

View Source​
Declaration
[Obsolete("GetLineCount is deprecated. Replaced by GetDigitalInputLineCount")]
public int GetLineCount(int line)
Returns​

System.Int32

Parameters​
TypeName
System.Int32line

GetDigitalInputLineCount(int)​

Reads the digital input counter of the specified channel

View Source​
Declaration
public int GetDigitalInputLineCount(int line)
Returns​

System.Int32

Parameters​
TypeName
System.Int32line

ClearLineCount(int)​

Clears the digital input counter of the specified line,

View Source​
Declaration
[Obsolete("ClearLineCount is deprecated. Replaced by ClearDigitalInputLineCount")]
public void ClearLineCount(int line)
Parameters​
TypeName
System.Int32line

ClearDigitalInputLineCount(int)​

Clears the digital input counter of the specified line.

View Source​
Declaration
public void ClearDigitalInputLineCount(int line)
Parameters​
TypeName
System.Int32line

GetDeviceConfiguration()​

Read the device configuration

View Source​
Declaration
public void GetDeviceConfiguration()

SetDeviceConfiguration(int, int, IOChangeTypes, CounterMode, bool)​

Sets configuration parameters for a digital IO device.

View Source​
Declaration
public void SetDeviceConfiguration(int newAddress, int newBaudRate, IOChangeTypes ioCounterUpdateDirection, CounterMode counterMode, bool checksum)
Parameters​
TypeNameDescription
System.Int32newAddressThe new ASCII/Modbus address for the device (0-255).
System.Int32newBaudRateThe new baud rate (e.g. 9600, 115200).
Brainboxes.IO.IOChangeTypesioCounterUpdateDirectionWhether the input counter increments on Brainboxes.IO.IOChangeTypes.RisingEdge or Brainboxes.IO.IOChangeTypes.FallingEdge.
Brainboxes.IO.CounterModecounterModeThe counter bit width: 16-bit or 32-bit.
System.BooleanchecksumWhether 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​
Declaration
public void SetDeviceConfiguration(int newAddress, int newBaudRate, AnalogDataFormat newAnalogDataFormat, bool newFilterSettings, bool newModuleSettings, bool newChecksum, TemperatureUnit temperatureUnit)
Parameters​
TypeNameDescription
System.Int32newAddressThe new ASCII/Modbus address for the device (0-255).
System.Int32newBaudRateThe new baud rate (e.g. 9600, 115200).
Brainboxes.IO.AnalogDataFormatnewAnalogDataFormatThe analog data format: Brainboxes.IO.AnalogDataFormat.Engineering, Brainboxes.IO.AnalogDataFormat.FullScaleRange, or Brainboxes.IO.AnalogDataFormat.Hexadecimal.
System.BooleannewFilterSettingsWhether to enable input filtering.
System.BooleannewModuleSettingsWhether to enable module-specific settings.
System.BooleannewChecksumWhether to enable checksum validation on commands.
Brainboxes.IO.TemperatureUnittemperatureUnitThe 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.

Implements​