Modbus Communication

SUMMARY

This document describes the functionality and settings for Modbus-Master and Modbus-Slave.

CONTENTS

  1. Master-Functionality
  2. Slave-Functionality
  3. Status-Analysis
  4. SoftPLC7.ini

Master-Functionality

Introduction
Via library functions the SoftPLC7 as a Modbus-Master can, using the serial interface, collect data from one or more Modbus-Slaves and allocate them for the application program.

The modules work similar to AG_SEND/AG_RECV found in the “alpha-bit Library”.
The SoftPLC7 supports the following Modbus-Functioncodes:

Functioncode Name HLL-Function within SoftPLC7 Modbus.dll FC-Number
1 Read Coil Status Modbus Read Coil Status 2001
2 Read Input Status Modbus Read Input Status 2002
3 Read Holding Register Modbus Read Holding Register 2003
4 Read Input Register Modbus Read Input Register 2004
5 Force Single Coil Modbus Force Single Coil 2005
6 Preset Single Register Modbus Preset Single Register 2006
15 Force Multiple Coils Modbus Force Multiple Coils 2015
16 Preset Multiple Registers Modbus Preset Multiple Registers 2016

To use the functions, copy the modules into the SoftPLC7 project directory and create an entry in the SoftPLC7.ini file for any HLL-Function out of the SoftPLC7 Modbus.dll that substitutes the corresponding FC.


Example:
[HLL-Functions] 
FC2001=SoftPLC7Modbus,ModbusReadCoilStatus.
                  

All functions got the OUT-Parameter status to indicate the progress or error condition when called or executed.
Some parameters need a broader data type to make input easier.
I.E. on registers the type DINT is used instead of WORD that allows writing “L#40001” instead of “W#16#9C41”.

Interface definitions for Modbus-Systems
At present only communication via serial interface is possible; Modbus-TCP is planed at a later date.
It is possible to use more COM-Ports, with anyone is part of a different network of Modbus-Master and several Modbus-Slaves.
The connections are defined in the SoftPLC7.ini. file

Example:
[Modbus]
MasterCount=2
Master1=(2;COM1;5;n;8;1)
Master2=(129;COM2;6;n;8;1)
                  

Initialising of a Modbus-System
To use the Modbus-Master functions, the Master= and Slave-Parameters must be defined in advance.
The following functions are available to be placed in one of the starting OBs (100, 101, 102).



Description HLL-Function withinSoftPLC7 Modbus.dll FC-Number
Initialisation of a Modbus-Master InitModbusMaster 2100
Initialisation of aModbus-Slave InitModbusSlave 2101


The function 'InitModbusMaster' must be called with the parameter 'Intern=TRUE', that defines the SoftPLC7 as the Modbus-Master within that Modbus-System.
Parameter 'MasterID' is merely used for identification and must differ from any SlaveID.


InitModbusMaster
Interface Parameter Name Range Description
IN BOOL Internal FALSE TRUE Defines SoftPLC7 as a MASTER
IN INT MasterID 1 … 255 Unique ID, other modules refer to
IN DINT Timeout 1 … 65535 Timeout (ms) on send/receive telegrams
OUT WORD Status Result of executing a function


For any Modbus-Slave to communicate with, the function InitModbusSlave must be called up once.
The Offsets relate to the special Modbus-Functioncodes and may be set to 0, typical offsets are 1, 10001, 40001 and 30001.



InitModbusSlave
Interface Parameter Name Range Description
IN INT MasterID 1 … 255 MasterID the Slave belongs to
IN INT SlaveID 1 … 255 Unique ID, other modules refer to
IN DINT OffsetCoils 1 … 65535 Register-Offset for functioncode 1, 5, 15 (i.E. 1)
IN DINT OffsetInFlags 1 … 65535 Register-Offset for functioncode 2 (i.E. 10001)
IN INT OffsetHoldRegs 1 … 65535 Register-Offset for functioncode 3, 6, 16(i.E. 40001)
IN DINT OffsetInRegs 1 … 65535 Register-Offset for functioncode 4(i.E. 30001)
OUT WORD Status Result of executing a function


Communication Modules



ModbusReadCoilStatus
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify a function call up
IN INT Master ID 1 … 255 ID of Master sending the telegram
IN INT SlaveID 1 … 255 ID of addressed Slave
IN DINT RegFrom 0 … 65535 First requested Register
IN DINT RegCount 1 … 255 No of requested Register
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether mission is completed
OUT BOOL Error Tells whether mission failed
OUT WORD Status Result of the executed function


ModbusReadInputStatus
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT RegFrom 0 … 65535 First requested Register
IN DINT RegCount 1 … 255 No of requested Register
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether mission is completed
OUT BOOL Error Tells whether mission failed
OUT WORD Status Result of the executed function


ModbusReadHoldingRegisters
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT RegFrom 0 … 65535 First requested Register
IN DINT RegCount 1 … 127 No of requested Register
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function


ModbusReadInputRegisters
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT RegFrom 0 … 65535 First requested Register
IN DINT RegCount 1 … 127 No of requested Register
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function


ModbusForceSingleCoil
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT Reg 0 … 65535 Register to be written to
IN BOOL Value FALSE,TRUE value to be writen
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function


ModbusPresetSingleRegister
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT Reg 0 … 65535 Register to be written to
IN WORD Value 0 … 65535 value to be writen
IN BOOL Req FALSE,TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function


ModbusForceMultipleCoil
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT RegFrom 0 … 65535 First register to write to
IN DINT RegCount 0 … 255 Number of registers to be written to
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSER, TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function


ModbusPresetMultipleRegisters
Interface Parameter Name Range Description
IN DINT Channel 0… 65535 ID to identify the function call
IN INT Master ID 1 … 255 ID of the Master sending the telegram
IN INT SlaveID 1 … 255 ID of the addressed Slave
IN DINT RegFrom 0 … 65535 First register to write to
IN DINT RegCount 0 … 127 Number of registers to be written to
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to internal S7 memory
IN BOOL Req FALSER, TRUE Starts transmitting on rising edge
OUT BOOL Done Tells whether the mission is completed
OUT BOOL Error Tells whether the mission failed
OUT WORD Status Result of the executed function

Slave-Functionality

Introduction
In addition to Master-Mode, any SoftPLC7 may act as one or more Slave(s).
The initialisation modules 'InitModbusMaster' and 'InitModbusSlave' must be called analogue. The only difference: Parameter Intern=FALSE must be set to indicate the SoftPLC7 acts as one or more slave(s).
To respond to enquiries from an external Modbus-Slave it is necessary to allocate Register-Bands to internal S7-addresses within the starting OB.



Description HLL-Function within SoftPLC7 Modbus.dll FC-Number
Definition of a register band AddModbusRegDef 2102


AddModbusRegDef
Interface Parameter Name Range Description
IN INT Master ID 1 … 255 ID of the Modbus-Master whose telegram should be respond to.
IN INT SlaveID 1 … 255 ID of the Slave represented by the SoftPLC7
IN DINT RegType 1..4 1=Coils, 2=InFlags, 3=HoldRegs, 4=InRegs according to the first four function codes
IN DINT RegFrom 0 … 65535 First registers to define
IN DINT RegCount 0..255 (bits) resp. 0..127 (words) Number of registers to define
IN ANY Buffer Pointer to DB, M, E, A, P Pointer to the first Register (regFrom). The definition of the length of the pointer is not evaluated
OUT WORD Status Result of the executed function


The ANY, parameter ‘Buffer’ defines the location of register ‘RegFrom’ within the SoftPLC7. Any further registers(RegCount –1) are joined behind. Words use 2 bytes, bits 1 byte.



1.Example:

AddModbusRegDef(MasterId=1,SlaveId=17,RegType=3,RegFrom=40100,RegCount=10,
Buffer=P#DB100.DBX30.0BYTE2,Status=MW0)
                  

For the Slave with ID=17, belonging to the Master with ID=1, a Word-Register Band starting at DB100.DBX30.0 is defined. The register 40100 is read by DB100.DBW30, register 40101 by DB100.DBW32 a.s.o. The area defined by parameter Buffer is not large enough, but the size is not checked. Defining the registers as RegType=3 declares them as Holding-Register.



2.Example:

AddModbusRegDef(MasterId=1,SlaveId=17,RegType=2,RegFrom=10100,RegCount=10,
Buffer=P#DB100.DBX30.0BYTE2,Status=MW0)
                  

For the Slave with ID=17, belonging to the Master with ID=1, a Bit-Register Band starting at DB100.DBX30.0 is defined. The register 10100 is read by DB100.DBB30, register 10101 by DB100.DBW31 a.s.o. Defining the registers as RegType=2 it appoints them to Functioncode 2 “ReadInputStatus”.

Status-Analysis

The value of Parameter 'Status' informs about the progress in executing an FC and in case of an error it gives further details.

An error condition is indicated by Bit 15 (Status>=0x8000).

Enquiry of the BIE-Flag: TRUE –no error, FALSE –error offers the same information.


Possible Status values in detail:


Status Value Description
0x0000 No error occurred, the mission is accomplished or was not started.
0x0001 The data of a Master-Telegram were taken over to send; sending is in progress.
0x0002 Master waiting for response telegram.
0x0003 Master received a response telegram complete.
0x0004 Master evaluated a response telegram.
0x0005 Slave received 8 characters.
0x0006 Slave received remaining characters of a Master-Telegram.
0x0007 Slave received a complete Master-Telegram.
0x0008 Slave sends an answer telegram to a Master.
0x8000 Error when copying into a request telegram.
0x8001 Error on sending.
0x8002 Error on receiving.
0x8003 Error on copying into the answer telegram.
0x8100 Error on initialising the interface of a Master (INI-Entry).
0x8101 Error on opening a connection.
0x8102 Error on initialising a connection.
0x8200 Parameter error: Out of range.
0x8201 Error on reading an ANY-Pointer.
0x8202 Parameter ‘RegType’ error: Value out of [1,4].
0x8203 Parameter ‘RegFrom’ error: Value to low and not within the register band.
0x8204 Parameter ‘RegCount’ error: Value to high.
0x8300 Error when initialising Master: Master already initialised.
0x8301 Error when initialising Master: Master not yet initialised.
0x8302 Error when initialising Master: Slave= and Master ID identical.
0x8303 Error when initialising Slave: Slave already initialised.
0x8304 Error at communication block: Slave not yet initialised.

INI-FILES

SoftPLC7.ini

Section [HLL-Functions]

Entry Type Description / Example Default
(FB |FC |SFB |SFC )<num> string
Defines the DLL-Function that replaces the Program-Module with the following syntax: <dll, function>
Example
FC13=MyDll, MyFunction
				

For Modbus Communication the line should look like:
[HLL-Function]
FC2001=SoftPLC7Modbus,ModbusReadCoilStatus
			        

Section [Modbus]

Entry Type Description / Example Default
MasterCount integer
Defines the number of the Modbus-System.
The following syntax is used: <Number Modbus-System>
Example
MasterCount=2
				
Master <n> string
Defines the communication parameter with the following syntax:
(<MasterID>;COM<PortNo>; <Baudrate-Index>;<Parity>;<Databits>;<Stopbits>)

BAUTRATE-index:
0=300, 1=600, 2=1200, 3=2400, 4=4800, 5=9600, 6=19200, 7=38400, 8=57600, 9=76800

PARITY:
n=no parity, e=even parity, o=odd parity

DATA BITS:
7 or 8

STOP BITS:
1 or 2
Example
Master1=(2;COM1;5;n;8;1)
Master2=(129;COM2;6;n;8;1)