Server ¶ ExampleDataModel (FunctionBlock) Server (FunctionBlock) Supported function codes “data model” “data item” offset / “data item” numbers DataModel CheckDataModel (Method) InitDataModel (Method) LogDataModel (Method) Log LogStatusInfo (Method) ResetRequestStatistics (Method) SupportFc (Method) SupportsFc (Method) ServerSerial (FunctionBlock) Log LogStatusInfo (Method) ServerTCP (FunctionBlock) Log LogStatusInfo (Method)
ExampleDataModel (FB) ¶ FUNCTION_BLOCK ExampleDataModel Simple “data model” example, contains four memory-separated sections containg two: - “discrete inputs” - “coils” - “input registers” - “holding registers” “data item” start index for all sections is 5 InOut: Scope Name Type Initial Comment Output tableDefs TableDefinitions STRUCT(tableDiscreteInputs := STRUCT(uiNumSections := (SIZEOF(_aDiscreteInputsSections) / SIZEOF(_aDiscreteInputsSections[0])), pSections := ADR(_aDiscreteInputsSections[0])), tableCoils := STRUCT(uiNumSections := (SIZEOF(_aCoilsSections) / SIZEOF(_aCoilsSections[0])), pSections := ADR(_aCoilsSections[0])), tableInputRegisters := STRUCT(uiNumSections := (SIZEOF(_aInputRegistersSections) / SIZEOF(_aInputRegistersSections[0])), pSections := ADR(_aInputRegistersSections[0])), tableHoldingRegisters := STRUCT(uiNumSections := (SIZEOF(_aHoldingRegistersSections) / SIZEOF(_aHoldingRegistersSections[0])), pSections := ADR(_aHoldingRegistersSections[0]))) the “data model” tables
Server (FB) ¶ FUNCTION_BLOCK ABSTRACT Server MODBUS server (slave) base class. Supported function codes ¶ Supported function codes can be configured, see fcsSupported. By default the MODBUS server supports: ReadCoils ReadDiscreteInputs ReadHoldingRegisters ReadInputRegisters WriteSingleCoil WriteSingleRegister WriteMultipleRegisters ReadWriteMultipleRegisters Optionally it supports also: MaskWriteRegister “data model” ¶ The “data model” allows: non-consecutive index for the “data items” (for example: index 0..9, 20..29) multiple (non-consecutive) memory blocks for “data items” in one “data block” (for example: index 0..9 => memory block 1, index 20..29 => memory block 2) not-memory-mapped “data items” (user code needs to be written to handle reads/writes for those “data items”) “discrete inputs” / “coils” : data item size 1 bit or 8 bit (basically: ARRAY [0..numBits/8] OF BYTE vs. ARRAY [0..numBits-1] OF BOOL) “input registers” / “holding registers” : data item size of 16 bit (basically: ARRAY [0..numRegisters-1] OF UDINT) overlapping memory blocks, could overlap “registers” with “registers” as well as “registers” with “discrete inputs” / “coils” (caution - hard to use feature) To support all this the “data model” ( TableDefinitions ) defines zero or more TableSection for each of the MODBUS “primary tables” ( TableDefinition ). Some more or less obvious limitations: all “data items” in one section need to have the same TableSection.uiDataItemSize all “data item” index in one “primary table” have to be unique, so no section index overlap is allowed of course read/write requests must not span more than one section “data item” offset / “data item” numbers ¶ Please note: unlike standard MODBUS, in CODESYS MODBUS the “data item” numbers are equal to the “data addresses”. The MODBUS standard states: “In the MODBUS data Model each element within a data block is numbered from 1 to n.” In CODESYS MODBUS the “data items” are numbered from 0 to n-1. InOut: Scope Name Type Initial Comment Input xEnable BOOL FALSE Enables the server, take over the configuration information fcsSupported and dataModel. If the “data model” (tableDefinitions) contains configuration errors, the Server will not got busy (xBusy = FALSE). fcsSupported SupportedFcs Constants.SUPPORTED_FCS_SIMPLE_SERVER Supported “function codes”, accepted at xEnable FALSE => TRUE. In case fcsSupported is not assigned by the application it defaults to Constants.SUPPORTED_FCS_SIMPLE_SERVER . dataModel TableDefinitions “data model”, accepted at xEnable FALSE => TRUE. tInactivityInfoTime UDINT 0 “inactivity info time” (in milliseconds). If no valid requests are received in this time xInactive is signaling inactivity - default = 0 -> no “inactivity info”. Output xRunning BOOL Server is up and running, waiting for requests. xError BOOL Error eErrorID Error Error status xInactive BOOL No valid requests received for tInactivityTimeOut. udiNumMsgRecv UDINT Request statistics: number of request messages received since enabling the Server. udiNumMsgReply UDINT Request statistics: number of reply messages send since enabling the Server. udiNumMsgExcReply UDINT Request statistics: number of exception reply messages send since enabling the Server. udiNumMsgExcReplyIllFct UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal function. udiNumMsgExcReplyIllDataAdr UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal data address. udiNumMsgExcReplyIllDataValue UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal data value. xReadRequest BOOL Read request(s) happened since last call, rejected requests included. udiNumReadRequests UDINT Request statistics: read request counter, rejected requests included. xWriteRequest BOOL Write request(s) happened since last call, rejected requests included. udiNumWriteRequests UDINT Request statistics: write request counter, rejected requests included. Methods: ResetRequestStatistics SupportFc SupportsFc CheckDataModel InitDataModel LogDataModel LogStatusInfo Structure: DataModel CheckDataModel (Method) InitDataModel (Method) LogDataModel (Method) Log LogStatusInfo (Method) ResetRequestStatistics (Method) SupportFc (Method) SupportsFc (Method)
ServerSerial (FB) ¶ FUNCTION_BLOCK ServerSerial EXTENDS Server IMPLEMENTS ISysComUser MODBUS serial server (slave). For details about supported function codes and “data model” see Server . Please note: some input variables related to connection configuration are read when rising edge on xEnable occurs. To change connection configuration the application needs to - disconnect (xEnable := FALSE and execute the FB) - modify the related input variables - connect (xEnable := TRUE and execute the FB) The Server provides some statistics of received valid requests messages and sent reply messages. Invalid messages are dropped at the communication level, so doesnt appear in the statistics. To analyse situations where invalid reply messages might occur, you can use udiLogOptions with LoggingOptions.WarnOnReceivedInvalidFrames. The maximum number of serial ports usable with ClientSerial / ServerSerial is limited to MAX_NUM_COMPORTS. In case there is a need to use more serial ports, anyone can set the library parameter MAX_NUM_COMPORTS within an application - see Library Manager -> ModbusFB -> Parameter -> MAX_NUM_COMPORTS. Please visit https://forge.codesys.com/prj/codesys-example/modbus/home to find examples. InOut: Scope Name Type Initial Comment Inherited from Input xEnable BOOL FALSE Enables the server, take over the configuration information fcsSupported and dataModel. If the “data model” (tableDefinitions) contains configuration errors, the Server will not got busy (xBusy = FALSE). Server fcsSupported SupportedFcs Constants.SUPPORTED_FCS_SIMPLE_SERVER Supported “function codes”, accepted at xEnable FALSE => TRUE. In case fcsSupported is not assigned by the application it defaults to Constants.SUPPORTED_FCS_SIMPLE_SERVER . Server dataModel TableDefinitions “data model”, accepted at xEnable FALSE => TRUE. Server tInactivityInfoTime UDINT 0 “inactivity info time” (in milliseconds). If no valid requests are received in this time xInactive is signaling inactivity - default = 0 -> no “inactivity info”. Server Output xRunning BOOL Server is up and running, waiting for requests. Server xError BOOL Error Server eErrorID Error Error status Server xInactive BOOL No valid requests received for tInactivityTimeOut. Server udiNumMsgRecv UDINT Request statistics: number of request messages received since enabling the Server. Server udiNumMsgReply UDINT Request statistics: number of reply messages send since enabling the Server. Server udiNumMsgExcReply UDINT Request statistics: number of exception reply messages send since enabling the Server. Server udiNumMsgExcReplyIllFct UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal function. Server udiNumMsgExcReplyIllDataAdr UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal data address. Server udiNumMsgExcReplyIllDataValue UDINT Request statistics: number of exception reply messages send since enabling the Server, signaling illegal data value. Server xReadRequest BOOL Read request(s) happened since last call, rejected requests included. Server udiNumReadRequests UDINT Request statistics: read request counter, rejected requests included. Server xWriteRequest BOOL Write request(s) happened since last call, rejected requests included. Server udiNumWriteRequests UDINT Request statistics: write request counter, rejected requests included. Server Input uiUnitId UINT 1 Unit-Id / Slave address. Only read when rising edge on xEnable occurs. Slave address has to be in range 1 .. 247 according to MODBUS standard. iPort SysCom.SYS_COM_PORTS SysCom.SYS_COM_PORTS.SYS_COMPORT_NONE Serial port. Only read when rising edge on xEnable occurs. dwBaudrate SysCom.SYS_COM_BAUDRATE SysCom.SYS_COM_BAUDRATE.SYS_BR_115200 Baud rate. Only read when rising edge on xEnable occurs. byDataBits BYTE 8 Number of data bits/BYTE, 5-8. Only read when rising edge on xEnable occurs. eParity SysCom.SYS_COM_PARITY SysCom.SYS_COM_PARITY.SYS_NOPARITY Parity. Only read when rising edge on xEnable occurs. eStopBits SysCom.SYS_COM_STOPBITS SysCom.SYS_COM_STOPBITS.SYS_ONESTOPBIT Stop bits. Only read when rising edge on xEnable occurs. eDTRcontrol SysCom.SYS_COM_DTR_CONTROL SysCom.SYS_COM_DTR_CONTROL.SYS_DTR_CONTROL_DISABLE DTR control. Only read when rising edge on xEnable occurs. eRTScontrol SysCom.SYS_COM_RTS_CONTROL SysCom.SYS_COM_RTS_CONTROL.SYS_RTS_CONTROL_DISABLE RTS control. Only read when rising edge on xEnable occurs. eRtuAscii RtuAscii RtuAscii.RTU RTU / ASCII. Only read when rising edge on xEnable occurs. udiLogOptions UDINT LoggingOptions.ServerStartStop Logging options. Methods: CheckDataModel , inherited from Server InitDataModel , inherited from Server LogDataModel , inherited from Server LogStatusInfo ResetRequestStatistics , inherited from Server SupportFc , inherited from Server SupportsFc , inherited from Server Structure: Log LogStatusInfo (Method)
ClientRequestMaskWriteRegister (FB) ¶ FUNCTION_BLOCK ClientRequestMaskWriteRegister EXTENDS ClientRequestWriteSingle MaskWriteRegister client request (FC22). For details about client request see ClientRequest . InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiItem UINT 0 “data item” to write. ClientRequestWriteSingle uiAndMask UINT 0 The “AND mask”. uiOrMask UINT 0 The “OR mask”.
ClientRequestRead (FB) ¶ FUNCTION_BLOCK ABSTRACT ClientRequestRead EXTENDS ClientRequest Read client request base class for ReadCoils, ReadDiscreteInputs, ReadHoldingRegisters and ReadInputRegisters. InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiStartItem UINT 0 First “data item” to read. uiQuantity UINT 1 Number of “data items” to read. ReadCoils / ReadDiscreteInputs: 1 to 2000 ReadHoldingRegisters / ReadInputRegisters: 1 to 125
ClientRequestReadBits (FB) ¶ FUNCTION_BLOCK ABSTRACT ClientRequestReadBits EXTENDS ClientRequestRead Read client request base class for ReadCoils, ReadDiscreteInputs InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiStartItem UINT 0 First “data item” to read. ClientRequestRead uiQuantity UINT 1 Number of “data items” to read. ReadCoils / ReadDiscreteInputs: 1 to 2000 ReadHoldingRegisters / ReadInputRegisters: 1 to 125 ClientRequestRead pData POINTER TO ARRAY [0..0] OF BOOL 0 Pointer to result data array, memory has to be provided by caller.
ClientRequestReadCoils (FB) ¶ FUNCTION_BLOCK ClientRequestReadCoils EXTENDS ClientRequestReadBits ReadCoils client request (FC01). For details about client request see ClientRequest . InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiStartItem UINT 0 First “data item” to read. ClientRequestRead uiQuantity UINT 1 Number of “data items” to read. ReadCoils / ReadDiscreteInputs: 1 to 2000 ReadHoldingRegisters / ReadInputRegisters: 1 to 125 ClientRequestRead pData POINTER TO ARRAY [0..0] OF BOOL 0 Pointer to result data array, memory has to be provided by caller. ClientRequestReadBits
ClientRequestReadDiscreteInputs (FB) ¶ FUNCTION_BLOCK ClientRequestReadDiscreteInputs EXTENDS ClientRequestReadBits ReadDiscreteInputs client request (FC02). For details about client request see ClientRequest . InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiStartItem UINT 0 First “data item” to read. ClientRequestRead uiQuantity UINT 1 Number of “data items” to read. ReadCoils / ReadDiscreteInputs: 1 to 2000 ReadHoldingRegisters / ReadInputRegisters: 1 to 125 ClientRequestRead pData POINTER TO ARRAY [0..0] OF BOOL 0 Pointer to result data array, memory has to be provided by caller. ClientRequestReadBits
ClientRequestReadHoldingRegisters (FB) ¶ FUNCTION_BLOCK ClientRequestReadHoldingRegisters EXTENDS ClientRequestReadRegisters ReadHoldingRegisters client request (FC03). For details about client request see ClientRequest . InOut: Scope Name Type Initial Comment Inherited from Input xExecute BOOL Rising edge: Starts defined operation FALSE : Resets the defined operation after ready condition was reached ETrigTo udiTimeOut UDINT Max. operating time for executing [µs], 0: No operating time limit ETrigTo Output xDone BOOL Ready condition reached ETrigTo xBusy BOOL Operation is running ETrigTo xError BOOL Error condition reached ETrigTo Inout rClient Client Reference to Client . ClientRequest Input uiUnitId UINT 0 Unit-Id to send the request to. ClientRequest udiReplyTimeout UDINT (50 * 1000) Reply timeout in µs - accepted maxmimum time between request message send and reply message receveived - default 50ms. ClientRequest uiMaxRetries UINT 0 Maximum number of request retries in case of “reply timeout”. ClientRequest Output eErrorID Error Error status ClientRequest eException ExceptionCodes Request exception code. ClientRequest uiRetryCnt UINT 0 Number of request retries in case of “reply timeout”. ClientRequest Input uiStartItem UINT 0 First “data item” to read. ClientRequestRead uiQuantity UINT 1 Number of “data items” to read. ReadCoils / ReadDiscreteInputs: 1 to 2000 ReadHoldingRegisters / ReadInputRegisters: 1 to 125 ClientRequestRead pData POINTER TO ARRAY [0..0] OF UINT 0 Pointer to result data array, memory has to be provided by caller. ClientRequestReadRegisters