+ All Categories
Home > Documents > MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware...

MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware...

Date post: 25-May-2018
Category:
Upload: buidung
View: 218 times
Download: 2 times
Share this document with a friend
38
MODBUS RTU compatible master and slave driver Book E26 - Version 2.12 for High Performance Controllers
Transcript
Page 1: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master andslave driver

Book E26 - Version 2.12

for

High Performance Controllers

Page 2: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverE26 2.12

© 2014 E.D.&A. 1HPC

Version

Table of contents

Chapter 1 Introduction 3

................................................................................................................................... 31 Overview

................................................................................................................................... 32 Required hardware

................................................................................................................................... 33 Required software

................................................................................................................................... 44 Required option lock

................................................................................................................................... 45 Cabling

Chapter 2 Installation of the driver 5

................................................................................................................................... 51 Initialization of the serial port

................................................................................................................................... 52 Installing a MODBUS master

................................................................................................................................... 73 Installing a MODBUS slave

................................................................................................................................... 94 Uninstalling a MODBUS slave

Chapter 3 Do reads and writes with the MODBUSmaster 10

Chapter 4 More about numbers and types of variables 13

................................................................................................................................... 141 Mapping table of ED&A digital inputs to modbus numbers (using standard i/o configuration)

................................................................................................................................... 152 Mapping table of ED&A digital outputs to modbus numbers (using standard i/o configuration)

................................................................................................................................... 163 Mapping table of ED&A analogue inputs to modbus numbers (using standard i/o configuration)

................................................................................................................................... 164 Mapping table of ED&A analogue outputs to modbus numbers (using standard i/o configuration)

Chapter 5 Broadcasting 18

Chapter 6 Protocol details 19

Chapter 7 Troubleshooting guide 24

................................................................................................................................... 241 Does MODBUS master send messages?

................................................................................................................................... 252 Does the slave receive these messages?

................................................................................................................................... 253 Does the slave react to the messages?

................................................................................................................................... 264 Does the answers go from the slave to the master?

................................................................................................................................... 265 Are the answers OK or do they contain error codes?

................................................................................................................................... 276 Do cable problems cause interruptions?

Chapter 8 Examples 28

................................................................................................................................... 281 Modsla1.epl

................................................................................................................................... 292 Modmas1.epl

................................................................................................................................... 303 Modmas2.epl

................................................................................................................................... 324 Modmas3.epl

................................................................................................................................... 335 Modmas4.epl

Page 3: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverE26 2.12

© 2014 E.D.&A. 2HPC

Version

................................................................................................................................... 346 Modsla4.epl

Chapter 9 Versions book 26 36

Page 4: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 3HPC

1 Introduction

Because communication between industrial controllers and interface equipment is becoming ever moreimportant, E.D.&A. has implemented the MODBUS protocol in their HPC's. MODBUS is a fail safe, multi-client and easy to use protocol that makes communication possible between HPC's or between HPC's andarbitrary MODBUS masters or slaves of other manufacturers.

1.1 Overview

This manual describes the use of this MODBUS protocol implementation for all HPC's using the EPLprogramming language. For details concerning the E.D.&A. HPC hardware and programming, please referto the E.D.&A. technical reference manuals. There are two MODBUS implementations:

a MODBUS master driver,a MODBUS slave driver.

The MODBUS master implementation is really the master of the serial line, it reads data from, and writesdata to one or more connected slave systems. There is always one MODBUS master on a serial line, butthere can be one or more slave systems.The MODBUS slave only listens to the master and sends answers back if it receives valid requests.The driver can be installed multiple times on a HPC, which allows it to communicate through multiple serialports at the same time, each connecting one or more other systems.Only RTU mode is implemented, this means that data is transferred in binary mode, using a CRC-16 errorcheck sequence. ASCII mode is not supported.

1.2 Required hardware

The use of the MODBUS software requires a serial port and an option lock. This option lock is a securitykey that allows the use of optional software. The hardware reference manuals of each HPC model describethe placement of the option locks:These MODBUS drivers can be used on any serial port of the HPC's, the desired port number must bechosen when the protocol is installed by the HPC program.

1.3 Required software

To use the MODBUS driver software, the HPC must have a certain minimum system software version.There are some additions to the MODBUS slave driver that are available from a later system softwareversion. These additions are: reading and writing of char and long variables, relaxed timing capability forcommunication through modems, RS485 multipoint capability, increased maximum transfers of 512 boolsand 64 shorts, and a deinstall() function.This table shows what minimum system software version is required to use a given capability:

Capability V5-2xx/3xx V10-2xx V50-2xx

MODBUS slave driver v1.30 v1.23 v1.33

MODBUS slave additions v1.36 v2.02 v2.01

MODBUS master driver v1.32 v1.29 v1.33

MODBUS master broadcast v1.36 v2.02 v2.01

The EWB programming software must be version 2.04 or newer.

Page 5: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 4HPC

1.4 Required option lock

One option lock is required for each installation of a MODBUS master or slave driver. When, for example,a driver must run on 4 serial ports, it must be installed 4 times, which requires 4 options. One MODBUSmaster driver communicating with multiple slaves requires only one option lock. The hardware reference manuals of each HPC system describe the installation and use of the option locksin detail.

1.5 Cabling

The MODBUS protocol can run on RS232, RS422, RS485 or current-loop serial communication ports. Nohandshake signals are required. Their state is ignored, even if they are connected through the serial line.The use of a shielded cable is recommended to avoid interference problems. For RS232, a simple 3-wire cable is sufficient for distances up to 15 meters.For RS485 multipoint cabling, we've got a few rules-of-thumb:

When using speeds up to 9600 bps, cables up to 450 meters can be used without termination.When using thin cable (0.14mm²/AWG26), the use of terminators may prevent the network fromworking because the terminators draw so much current that the cable's resistance causes toomuch voltage loss.When using longer cables, terminators are required and therefore thicker cable (0.5mm²/AWG20)must be used to keep voltage loss low.

The hardware reference manuals for the different HPC's describe the pinning and interconnections of thecables.

Page 6: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 5HPC

2 Installation of the driver

To use the MODBUS driver, two things must be done:

1. first open a serial port, 2. then install the driver on it.

2.1 Initialization of the serial port

Two functions can be used to open a serial port on a HPC: ComOpen() and ComOpenq().

ComOpen():Is simpler to use, but it configures the port with the default buffer sizes of 256 bytes for the transmit bufferand 128 bytes for the receive buffer, which are rather large for MODBUS. This works fine but may be aproblem when many serial ports are used because they all need there transmit and receive buffers.

Example:open com port 1 for 9600 baud, no parity, 8 data bits and 1 stop bit. Store the return value in the variableS1:

S1 = ComOpen( 1, "9600,n,8,1");

ComOpenq():Is a bit more complex, but it allows the programmer to control the sizes of the queues (buffers) andtherefore use more serial ports without running out of memory.

Example: open serial port 1 for 9600 baud, no parity, 8 data bits and 1 stop bit. Allocate a 100-byte queue fortransmission and a 50-byte queue for reception. Store the return value in the variable S1:

S1 = ComOpenq( 1, "9600,n,8,1", 100, 50);

The number of data bits MUST be 8 for MODBUS. The other parameters: baud rate, parity- and stop bits can be chosen freely. It's necessary howeverthat they are the same for the master and all the slaves on the serial line.It's possible to use the programming port of the HPC. This serial port can be used as serial port 0. The port must be in "communication" mode and not in "programming" mode. The hardware manualsdescribe the settings of the programmer port.

2.2 Installing a MODBUS master

Purpose:Installs a MODBUS master driver on a serial port. Other functions must then be used to initiate read andwrite operations to connected slaves. The MODBUS master driver must be installed and initialized before it can be used. The operationalparameters like timeout and number of retries in case of an error, are specified with this installation.

Notation:

Page 7: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 6HPC

short InstallModbusMas( short comnr, short buffer, short timeout, shorttries);

Parameters:

comnr:

Parameter of the type short. Number of the serial port where the MODBUS master is installed on.

buffer:

Parameter of the type short. The size of the buffer that the driver uses to build the requests that it transmits and to store theanswers that it receives. See the remarks below on how to choose the size of the buffer that is required for your application.

timeout:

Parameter of the type short. The time that the driver allows for a slave to respond, in milliseconds. If the slave does not answer intime, the request has failed. An other request is started then if the number of retries is bigger the 0.

tries:

Parameter of the type short. The number of times that the driver will try to send each request to a slave. If all these attempts fail,the request will be returned to the user program with status 'failed'.

Return value:InstallModbusMas() returns 1 if the installation was successful. Negative values indicate an error:

-41: means that one of the initialization parameters is wrong. -45: means there are no more option locks free to allow the use of the driver. -47: means the serial port is not available or not initialized.

Remarks:The parameters are used for all the subsequent reads and writes through this port, they are permanent andcannot be changed without restarting the HPC. To install more MODBUS master drivers, simply call the same function for the other serial ports. The serial port must be initialized first with the ComOpen() or ComOpenq() function. How to determine the required buffer size:

The worst case for reading or writing registers is 9 fixed bytes + 2 bytes per 16-bit register. When transferring Boolean variables (bool), 1 byte is needed per 8 Boolean variables. The 9 fixedbytes are also needed.

For example, read 20 registers from a slave:

8 bytes needed to transmit the read request . The answer containing the data can be received with a buffer of 9+2*20 = 49 bytes. Rounded to 50bytes will do fine.

In the included examples, a buffer of 100 bytes is configured, which allows reading or writing 45 registers inone transfer. A MODBUS master does not have a number, because it is the only node that initiates requests, andanswers are always directed to the master. Only slaves have numbers.

Page 8: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 7HPC

Only one MODBUS master can be active in a network. A master can not communicate with anothermaster. A HPC can be MODBUS master and slave at the same time on different serial ports.

Example:Install the driver on COM port 1, use a buffer of 100 bytes, allow a response time of 1 second, which agreeswith 1000 milliseconds, and try 3 times before giving up. Store the return value in the variable S1:

S1 = InstallModbusMas( 1, 100, 1000, 3);

2.3 Installing a MODBUS slave

Purpose:Install a MODBUS slave driver on a serial port. Once installed, the driver will automatically work in thebackground and process incoming request. The MODBUS slave driver must be installed and initialized before it can be used. The slave number towhich the driver will respond is also specified when the driver is installed.

Notation:

short InstallModbus( short comnr, short slavenr, short options);

Parameters:

comnr:

Parameter of the type short. Number of the serial port where the MODBUS slave is installed on.

slavenr:

Parameter of the type short. This number defines the slave number to which the driver will respond. Each slave must have aunique number on a serial line. The master sends its request to a certain slave number, and onlythat slave will answer. There are two ways to define the slave number:

If slavenr is zero will the HPC address be taken as slave number. The adjustment of the addressnumber is described in the hardware manual. The slavenr parameter is taken as slave number directly if the number is greater the zero.

option:

Parameter of the type short.

The options parameter, that normally = o, is a bit pattern by which several options of the modbusslave driver can be switched on. If you want to switch on several options together, the sum of theoption values has to be inserted.

Option value 1: MODBUS slave will work with a minor strict timing (relaxed timing). Thetimeout that indicates the end of a message is now 40 character times instead of 4character times. This allows the use of a modem in a serial line. Only point to pointconnections are possible with this relaxed timing. Multipoint RS485 is not possible.Option value 2: this is only available for the recent HPCs. All the markers and the shortvariables are accessible via modem instead of only the first 9000. The digital inputs are nolonger approachable from marker 9001 and the analog outputs are no longer approachable

Page 9: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 8HPC

from short 9001.

Return value:InstallModbus() returns 1 if the installation was successful. Negative values indicate an error:

-41: means that one of the initialization parameters is wrong. -45: means there are no more option locks free to allow the use of the driver. -47: means the serial port is not available or not initialized.

Remarks:The MODBUS slave driver runs automatically. Nothing must be done besides initializing a serial port andinstalling the protocol driver on that port. Because it's a slave driver, it will automatically answer all read orwrite requests from the MODBUS master. The number of variables that can be read or written in one read or write request is limited: variables of type 'bool': maximum 512 variables of type 'short': maximum 64 variables of type 'char': maximum 128 variables of type 'long': maximum 32. The data that are read or written with the MODBUS protocol, are processed between two cycles of theprogram:

If the MODBUS master reads data are this data placed in a buffer and then transmitted to themaster. If the MODBUS master writes data to the HPC, are this data placed in a buffer until the completemessages is received.

All the data are then written to the variables of the user program at once, just before the user programstarts a new cycle. In this way is it impossible that during a cycle of the user program data is read or written by the MODBUSmaster.

Example 1:These statements initialize serial port 1 for 9600 baud, no parity, 8 data bits and 1 stop bit and install aMODBUS slave on that serial port with slave number 1. Store the return value of the ComOpen() function inthe variable S1:

S1 = ComOpen( 1, "9600,N,8,1");InstallModbus( 1, 1, 0);

The return value of the InstallModbus() function is not stored in a variable.

On a V5-2xx and a V5-3xx HPC, specify 0 as serial port number with the comopen() andinstallmodbus() functions.The parameters for the serial port (baud rate, parity and stop bits) must always be the same for theMODBUS master and slave.The number of data bits must always be 8 for MODBUS.

Example 2:

Install modbus on 2 serial ports. On com3 with even parity and slave number 1. On com4 without parity, asslave number the HPC address that was adjusted via the dip switches is taken. All the markers and shortvariables are made accessible.

Page 10: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 9HPC

init(){ S1 = ComOpen( 3, "9600,E,8,1"); S2 = InstallModbus( 3, 1, 0);

S3 = ComOpen( 4, "9600,N,8,1"); S4 = InstallModbus( 4, 0, 2);}

2.4 Uninstalling a MODBUS slave

Purpose:Remove a MODBUS slave driver from a serial port. This allows the application program to use the serial port for other purposes. The MODBUS slave driver canbe re-installed using the normal InstallModbus() function.

Notation:

short DeinstallModbus( short comnr);

Parameters:

comnr:

Parameter of the type short. Number of the serial port where the MODBUS will be removed from.

Return value:DeinstallModbus() returns 1 if the removal was successful. Negative values indicate an error:

-41: means that the removal failed because no MODBUS slave driver was installed on the port.

Example:Remove the MODBUS slave driver if it was previously installed on port 1. Store the return value in thevariable S1:

S1 = DeInstallModbus( 1);

Page 11: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 10HPC

3 Do reads and writes with the MODBUS master

Purpose:Do read and write actions to the MODBUS slaves. The MODBUS master can communicate with all the slaves on the serial line. There's a function to readvalues from a slave, ModbusRead(), and a function to write values to a slave, ModbusWrite(). A MODBUSmaster driver must be previously installed on the serial port.

Notation:

short ModbusRead( bool *startflag, short comnr, long *transferblock);

short ModbusWrite( bool *startflag, short comnr, long *transferblock);

Parameters:

startflag:

Parameter of the type 'pointer to bool' or 'address of bool'. This is a marker to indicate that the ModbusRead() or ModbusWrite() function must do its datatransfer. The user program must set this flag (set the marker to 1). The transfer will be done as soon as theport becomes available. The flag remains set while the function waits for the port to becomeavailable, and while the transfer is actually done. When the transfer is done, the startflag is automatically set to 0 by the ModbusRead() orModbusWrite() function. If the user sets this startflag to 0, the transfer will be canceled.

comnr:

Parameter of the type short. This is the serial port number on which the read or write action must be done.

transferblock:

Parameter of the type 'pointer to long' or 'address of long'. The beginning of a block of 5 long variables. These contain more details about the transfer; such asslave number and which variables are transferred. See below for more information.

Return value:The function returns a value that indicates whether the transfer is busy or finished, and whether the transferwas successful or not. Negative values indicate an error.The return value is also placed in the fourth long of the transfer block (see below). This makes it easy toverify whether a transfer was successful. All you have to do is display the fourth long of the transferblock.The return values have the following meaning:

2 Indicates a successful transfer has been done.1 Indicates the function is waiting for the port to become available, or it is actually doing the

transfer.0 Indicates that the function is not active. It is just waiting for the startflag to become set.-100 No MODBUS driver on the com port.-101 The number of the variable is invalid. This indicates an error in the second long of the

transferblock.-102 The buffer is too small for the transfer of the requested number of variables. You need to lower

the number of variables specified in the third long of the transferblock, or specify a larger bufferduring installation of the MODBUS master driver.

-103 Timeout, the master did not receive a valid answer from the slave. The serial port is in"programmer" mode and not in "communication" mode. A jumper connection on the

Page 12: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 11HPC

programming connector can cause this, or a dip switch that puts com port 0 intoprogramming mode, like dip switch 6 on the V5-1xxx HPC's.

-104 Checksum error.-110 Protocol error: an invalid reply message was received from the slave.-111 Protocol error: the slave replied with exception code 01, 'illegal function'.

This means that it did not understand the function code in the request message.-112 Protocol error: the slave replied with exception code 02, 'invalid address'.

This means that the request message had an invalid variable address, invalid length, or thataddress + length goes past the last variable of the slave.

-113 Protocol error: the slave replied with exception code 03, 'illegal data'.This means that the master attempted to write an invalid value into a variable.

-114 Protocol error: The slave replied with exception code 04 "Slave device failure". For somereason, the slave can not execute the command. Check the documentation of the slave.

-116 Protocol error: the slave replied with exception code 06, 'busy'.This means that it is busy. The request could not be processed.

-117 Protocol error: The slave replied with exception code 07 "Nak-negative acknowledgement".For some reason, the slave can not execute the command. Check the documentation of theslave.

-120 Protocol gateway error: The gateway replied with exception code 10 "Gateway pathunavailable". Can occur when the modbus communication passes through a gateway. Thegateway doesn't know how to forward the modbus command to the slave.

-121 Protocol gateway error: The gateway replied with exception code 11 "Gateway pathunavailable". Can occur when the modbus communication passes through a gateway. Thegateway has forwarded the modbus command to the slave, but did not receive a responsefrom that slave.

Remarks:The MODBUS master driver must be installed on a serial port first, by calling the functionInstallModbusMas().Multiple read or write transfers can be started at the same time. The system will automatically do onetransfer after another.Startflag must be unique for each transfer; different start markers must be used for each read or writerequest.It's very important that the ModbusRead() or ModbusWrite() statements are executed in every cycle of theprogram when a read of write action is in progress. This means that the following example is wrong:

if( ^/b5s) /* read every 5 seconds */{

m20 = 1;

S1 = ModbusRead( &m20, 1, &L100); /* WILL NOT WORK!! */}

But this is correct:

if( ^/b5s) /* read every 5 seconds */{

m20 = 1;}S1 = ModbusRead( &m20, 1, &L100); /* WORKS */

The return value is stored in the variable S1.

The transfer block:This is a block of 5 longs, which contain the details of the read or write actions.

Page 13: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 12HPC

First long:The slave number: values between 1 and 250 are permitted.

Second long:Variable number in the slave. This defines the type and the exact number of variable that will bewritten to, or read from the slave. See below for more information on how to specify the variablenumber.

Third long:The number of variables that are read or written. The maximum number of variables that can be transferred in a single read or write transaction islimited: variables of type 'bool': maximum 512 variables of type 'short': maximum 64 These maximums can be lower if the slave device has lower maximums.

Fourth long:The status of the last action remains in this variable. This number is the same as the return value ofthe ModbusRead() or ModbusWrite() functions.

Fifth long:Contains the address of the HPC variable(s) where the data from the read action must be stored, orwhere the data for the write action must come from.

Transfer block example:When this block is used for a READ action, it reads registers 10 up to 19 from slave number 1, and storesthe data in this HPC's short S20 up to S29.When this block is used for a WRITE action, it writes into registers 10 up to 19 of slave number 1, and thedata comes from this HPC's short S20 up to S29.

L100 = 1;L101 = 40010;L102 = 10;L103 = 0;L104 = (long)&s20;

Page 14: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 13HPC

4 More about numbers and types of variables

MODBUS has its own way to define the different types of variables. All the types of variables (markers,inputs, outputs, numerical registers, …) are indicated with a number between 1 and 49999: a list thatcontains all types of variables. E.D.&A. HPC's work differently, each type of variable has its own listing (e.g. E1.1, S1, M1, …).To be compatible, E.D.&A. HPC's do a conversion between the MODBUS names and their own names. In MODBUS masters, including the E.D.&A. MODBUS master, the MODBUS name (number between 1and 49999) usually must be used.The following table indicates what the MODBUS numbers stand for:

Variable number in MODBUS form Name in E.D.&A. EPL HPC's Type

00001 01000 M1 M1000 internal digital flag (marker)

09001 09256 A1.1 A16.16 digital output

10001 10256 E1.1 E16.16 digital input

30001 30128 AE1.1 AE16.8 analog input

40001 40500 S1 S500 internal numerical register(short)

49001 49064 AA1.1 AA16.4 analog output

See also the 4 mapping tables below. These give the standard mapping of the modbus i/o variables tothe ED&A variables for the standard i/o configuration. These tables can not be used if the number ofinputs or outputs of the i/o cards are different!

This list is correct for all E.D.&A. HPC's that use the EPL programming language. The HPC's that use theABC programming language have other names for their variables. They can be used with the MODBUSmaster but the mapping table of the ABC slaves has to be used (see book E25).The maximum usable MODBUS variable numbers are restricted to the capabilities of the MODBUS slave.For an E.D.&A. HPC MODBUS slave, this depends on the number of variables that are configured by thememory configuration of the HPC application program. For example, the short variables have a standardconfiguration of S1 to S500. This means that the MODBUS variable number has a valid range from 40001 to40500. If another configuration is used that defines more or less short variables, the valid variable numberrange will grow of shrink equally.Inputs and outputs are numbered linear in the MODBUS protocol: they are numbered in the order of theHPC i/o configuration. So modbus input 10001 maps to input E1.1. Modbus input 10018 maps to inputE1.18 if E1 has 18 or more inputs, or it may map to E2.2 if E1 has 16 inputs.

Remarks:Char and long variables can not be read or written by the MODBUS master. Only E.D.&A. HPC slavessupport this. The only MODBUS master that can use these extra features is the E.D.&A. MODBUSmaster driver for PC's.

Example 1:Read from a slave with number 1,read S1 up to S40, which are 40 registers, store in S101 to S140:

L200 = 1;L201 = 40001;L202 = 40;L203 = 0;L204 = (long)&S101;

Page 15: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 14HPC

Example 2:Read from a slave with number 12,read M400 up to M450, which are 51 flags or coils, store in M400 to M450:

L200 = 12;L201 = 400;L202 = 51;L203 = 0;L204 = (long)&M400;

4.1 Mapping table of ED&A digital inputs to modbus numbers(using standard i/o configuration)

V5-2xx V5-3xx

V5-12xx V5-13xx V5-14xx

V5-22xx V5-23xx V5-24xx

V10-210V10-22xV10-23x

V50-20x V50-22x

RCC30-20RCC30-40

E1.1 10001(E1.1-26)

10001 (E1.1-26)

10001 (E1.1-18)

10001 (E1.1-16)

10001 (E1.1-16)

10001 (E1.1-16)

E2.1 10027 (E2.1-18)

10019 (E2.1-16)

10017 (E2.1-16)

10017 (E2.1-16)

10017 (E2.1-16)

E3.1 10045 (E3.1-16)

10035 (E3.1-16)

10033 (E3.1-16)

10033 (E3.1-16)

10033 (E3.1-16)

E4.1 10061 (E4.1-16)

10051 (E4.1-16)

10049 (E4.1-16)

10049 (E4.1-16)

10049 (E4.1-16)

E5.1 10077 (E5.1-16)

10067(E5.1-16)

10065 (E5.1-16)

10065 (E5.1-16)

10065 (E5.1-16)

E6.1 10093 (E6.1-16)

10083 (E6.1-16)

10081 (E6.1-16)

10081 (E6.1-16)

10081 (E6.1-16)

E7.1 10109 (E7.1-16)

10099 (E7.1-16)

10097 (E7.1-16)

10097 (E7.1-16)

10097 (E7.1-16)

E8.1 10125 (E8.1-16)

10115 (E8.1-16)

10113 (E8.1-16)

10113 (E8.1-16)

10113 (E8.1-16)

E9.1 10131 (E9.1-16)

10129 (E9.1-16)

10129 (E9.1-16)

10129 (E9.1-16)

E10.1 10147 (E10.1-16)

10145 (E10.1-16)

10145 (E10.1-16)

10145 (E10.1-16)

E11.1 10163 (E11.1-16)

10161 (E11.1-16)

10161 (E11.1-16)

E12.1 10179 (E12.1-16)

10177 (E12.1-16)

10177 (E12.1-16)

E13.1 10195 (E13.1-16)

10193 (E13.1-16)

10193 (E13.1-16)

E14.1 10211 (E14.1-16)

10209 (E14.1-16)

10209 (E14.1-16)

E15.1 10227 (E15.1-16)

10225 (E15.1-16)

10225 (E15.1-16)

Page 16: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 15HPC

E16.1 10243(E16.1-16)

10241(E16.1-16)

10241(E16.1-16)

4.2 Mapping table of ED&A digital outputs to modbus numbers(using standard i/o configuration)

V5-2xx V5-3xx

V5-12xx V5-13xx V5-14xx

V5-22xx V5-23xx V5-24xx

V10-210 V10-22xV10-23x

V50-20x V50-22x

RCC30-20RCC30-40

A1.1 9001 (A1.1-16)

9001 (A1.1-8)

9001 (A1.1-8)

9001 (A1.1-8)

9001 (A1.1-16)

9001 (A1.1-8)

A2.1 9009 (A2.1-8)

9009 (A2.1-8)

9009 (A2.1-8)

9017 (A2.1-16)

9009 (A2.1-6)

A3.1 9017 (A3.1-8)

9017 (A3.1-8)

9017 (A3.1-8)

9033 (A3.1-16)

9015 (A3.1-8)

A4.1 9025 (A4.1-8)

9025 (A4.1-8)

9025 (A4.1-8)

9049 (A4.1-16)

9023 (A4.1-8)

A5.1 9033 (A5.1-8)

9033 (A5.1-8)

9033 (A5.1-8)

9065 (A5.1-16)

9031 (A5.1-8)

A6.1 9041 (A6.1-8)

9041 (A6.1-8)

9041 (A6.1-8)

9081 (A6.1-16)

9039 (A6.1-8)

A7.1 9049 (A7.1-8)

9049 (A7.1-8)

9049 (A7.1-8)

9097 (A7.1-16)

9047 (A7.1-8)

A8.1 9057 (A8.1-8)

9057 (A8.1-8)

9057 (A8.1-8)

9113 (A8.1-16)

9055 (A8.1-8)

A9.1 9065 (A9.1-8)

9065 (A9.1-8)

9129 (A9.1-16)

9063 (A9.1-8)

A10.1 9073 (A10.1-8)

9073 (A10.1-8)

9145 (A10.1-16)

9071 (A10.1-8)

A11.1 9081 (A11.1-8)

9081 (A11.1-8)

9161 (A11.1-16)

A12.1 9089 (A12.1-8)

9089 (A12.1-8)

9177 (A12.1-16)

A13.1 9097 (A13.1-8)

9097 (A13.1-8)

9193 (A13.1-16)

A14.1 9105 (A14.1-8)

9105 (A14.1-8)

9209 (A14.1-16)

A15.1 9113 (A15.1-8)

9113 (A15.1-8)

9225 (A15.1-16)

A16.1 9121 (A16.1-8)

9121 (A16.1-8)

9241 (A16.1-16)

Page 17: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 16HPC

4.3 Mapping table of ED&A analogue inputs to modbus numbers(using standard i/o configuration)

V5-2xx V5-3xx

V5-12xx V5-13xx V5-14xx

V5-22xx V5-23xx V5-24xx

V10-210 V10-22xV10-23x

V50-20x V50-22x

RCC30-20RCC30-40

AE1.1 30001 (AE1.1-4)

30001 (AE1.1-8)

30001 (AE1.1-8)

30001 (AE1.1-8)

30001 (AE1.1-8)

30001 (AE1.1-4)

AE2.1 30009 (AE2.1-4)

30009 (AE2.1-8)

30009 (AE2.1-8)

30009 (AE2.1-8)

30005 (AE2.1-4)

AE3.1 30013 (AE3.1-4)

30017 (AE3.1-8)

30017 (AE3.1-8)

30017 (AE3.1-8)

30009 (AE3.1-4)

AE4.1 30017 (AE4.1-4)

30025 (AE4.1-8)

30025 (AE4.1-8)

30025 (AE4.1-8)

30013 (AE4.1-4)

AE5.1 30021 (AE5.1-4)

30033 (AE5.1-8)

30033 (AE5.1-8)

30033 (AE5.1-8)

30017 (AE5.1-4)

AE6.1 30025 (AE6.1-4)

30041 (AE6.1-8)

30041 (AE6.1-8)

30041 (AE6.1-8)

30021 (AE6.1-4)

AE7.1 30029 (AE7.1-4)

30049 (AE7.1-8)

30049 (AE7.1-8)

30049 (AE7.1-8)

30025 (AE7.1-4)

AE8.1 30033 (AE8.1-4)

30057 (AE8.1-8)

30057 (AE8.1-8)

30057 (AE8.1-8)

30029 (AE8.1-4)

AE9.1 30065 (AE9.1-8)

AE10.1 30073 (AE10.1-8)

AE11.1 30081 (AE11.1-8)

AE12.1 30089 (AE12.1-8)

AE13.1 30097 (AE13.1-8)

AE14.1 30105 (AE14.1-8)

AE15.1 30113 (AE15.1-8)

AE16.1 30121 (AE16.1-8)

4.4 Mapping table of ED&A analogue outputs to modbus numbers(using standard i/o configuration)

V5-2xx V5-3xx

V5-12xx V5-13xx V5-14xx

V5-22xx V5-23xx V5-24xx

V10-21xV10-22xV10-23x

V50-20x V50-22x

RCC30-20RCC30-40

Page 18: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 17HPC

AA1.1 49001 (AA1.1)

49001 (AA1.1-4)

49001 (AA1.1-4)

49001 (AA1.1-2)

49001 (AA1.1-4)

49001 (AA1.1-2)

AA2.1 49005 (AA2.1-2)

49005 (AA2.1-2)

49003 (AA2.1-2)

49005 (AA2.1-4)

49003 (AA2.1-2)

AA3.1 49007 (AA3.1-2)

49007 (AA3.1-2)

49005 (AA3.1-2)

49009 (AA3.1-4)

49005 (AA3.1-2)

AA4.1 49009 (AA4.1-2)

49009 (AA4.1-2)

49007 (AA4.1-2)

49013 (AA4.1-4)

49007 (AA4.1-2)

AA5.1 49011 (AA5.1-2)

49011 (AA5.1-2)

49009 (AA5.1-2)

49017 (AA5.1-4)

49009 (AA5.1-2)

AA6.1 49013 (AA6.1-2)

49013 (AA6.1-2)

49011 (AA6.1-2)

49021 (AA6.1-4)

49011 (AA6.1-2)

AA7.1 49015 (AA7.1-2)

49015 (AA7.1-2)

49013 (AA7.1-2)

49025 (AA7.1-4)

49013 (AA7.1-2)

AA8.1 49017 (AA8.1-2)

49017 (AA8.1-2)

49015 (AA8.1-2)

49029 (AA8.1-4)

49015 (AA8.1-2)

AA9.1 49033 (AA9.1-4)

AA10.1 49037 (AA10.1-4)

AA11.1 49041 (AA11.1-4)

AA12.1 49045 (AA12.1-4)

AA13.1 49049 (AA13.1-4)

AA14.1 49053 (AA14.1-4)

AA15.1 49057 (AA15.1-4)

AA16.1 49061 (AA16.1-4)

Page 19: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 18HPC

5 Broadcasting

Broadcasting is a mechanism that makes it possible for a MODBUS master to write variables in allconnected slaves at once. This is achieved by sending a write function to slave number 0, which isaccepted by all slaves. The slaves won't send an answer back to the master to confirm the write request. The broadcast mechanism is useful if you need to send the same data to several slaves. Remember that abroadcast does not require an acknowledge and therefore is not repeated in case of line interference!Therefore it's best to broadcast data periodically to make sure it arrives in all slaves.Broadcasting a read request makes no sense because none of the slaves will answer.

Example:Once per second, broadcast the shorts S20..S29 to all slaves into registers 40010..40019. the return valueis placed in the variable S1:

if( ^/b1s){ L100 = 0; L101 = 40010; L102 = 10; L103 = 0; L104 = (long)&s20; m20 = 1;}S1 = ModbusWrite( &m20, 1, &L100);

Page 20: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 19HPC

6 Protocol details

This chapter is intended for those who need to know about the functions and addresses that are usedin the internals of the protocol!

The following list shows which functions are used in the MODBUS protocol to read and write variables:

Function number Function type Range (16-bit address field)

function 01 Read coils Read 00001 09999

function 02 Read inputs Read 10001 19999

function 03 Read holding registers Read 40001 49999

function 04 Read input registers Read 30001 39999

function 15 Write coils Write 00001 09999

function 16 Write holding registers Write 40001 49999

function 65 (E.D.&A. extension) Read character variables

function 66 (E.D.&A. extension) Write character variables

function 67 (E.D.&A. extension) Read long variables

function 68 (E.D.&A. extension) Write long variables

Which modbus function codes are generated by ModbusRead() and

ModbusWrite()The ModbusRead() and ModbusWrite() functions automatically generate a modbus function code from thevariable that has to be read or written. This table shows what function codes and addresses are generated:

Variable Function code in modbusquery message

Address in modbus querymessage

Read 00001 .. 09999 01 (Read coils) 0000 .. 9998

Read 10001 .. 19999 02 (Read inputs) 0000 .. 9998

Read 40001 .. 49999 03 (Read holding registers) 0000 .. 9998

Read 30001 .. 39999 04 (Read input registers) 0000 .. 9998

Write 00001 .. 09999 15 (Write coils) 0000 .. 9998

Write 40001 .. 49999 16 (Write holding registers) 0000 .. 9998

Function 03: Read holding registers:

The query message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><CRC-H><CRC-L>

ADR: the slave addressFUNC: function code 03OFFSET-H, OFFSET-L: data address, this is actually an offset, and therefore starts at zero for the firstvariable!LEN-H, LEN-L: the number of variables to readCRC-H, CRC-L: the CRC

Page 21: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 20HPC

The response message:

<ADR><FUNC><BYTECOUNT><DATA1-H><DATA1-L><DATA2-H><DATA2-L>...<CRC-H><CRC-L>

ADR: the slave address that sent the responseFUNC: echo of the function code 03BYTECOUNT: the number of data bytes that follow, equal to the number of requested variables *2DATAx-H, DATAx-L: high/low data bytes of each 16-bit word read, 2 bytes per wordCRC-H, CRC-L: the CRC

Example:

Read the 10 first 16-bit registers from slave 1 (S1..S10)

ADR: 01FUNC: 03OFFSET-H, OFFSET-L: 00 00LEN-H, LEN-L: 00 0ACRC-H, CRC-L: C5 CD

Query message:

0x01 0x03 0x00 0x00 0x00 0x0A 0xC5 0xCD

Possible response message:

0x01 0x03 0x14 0x00 0x01 0x00 0x02 0x00 0x03 0x00 0x04 0x00 0x05 0x00 0x06 0x00 0x07 0x000x08 0x00 0x09 0x00 0x0A 0x8F 0x16

meaning Reg0=0x0001, Reg1=0x0002, ...

Function 65(dec): Read character registers (8-bit): (ED&A extension)

The query message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><CRC-H><CRC-L>

ADR: the slave addressFUNC: function code 65 decimal or 41 hexOFFSET-H, OFFSET-L: data address, this is actually an offset, and therefore starts at zero for the firstvariable!LEN-H, LEN-L: the number of variables to be readCRC-H, CRC-L: the CRC

The response message:

<ADR><FUNC><BYTECOUNT><DATA1><DATA2><DATA3>...<CRC-H><CRC-L>

ADR: the slave address that sent the responseFUNC: echo of the function code 65BYTECOUNT: the number of data bytes that follow, equal to the number of requested variablesDATAx: data bytes of each character read, 1 byte per characterCRC-H, CRC-L: the CRC

Function 66(dec): Write character registers (8-bit): (ED&A extension)

Page 22: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 21HPC

The query message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><BYTECOUNT><DATA1><DATA2>...<CRC-H><CRC-L>

ADR: the slave addressFUNC: function code 66 decimal or 42 hexOFFSET-H, OFFSET-L: data address, this is actually an offset, and therefore starts at zero for the firstvariable!LEN-H, LEN-L: the number of variables to be writtenBYTECOUNT: the number of data bytes that follow, equal to the number of variables to be writtenDATAx: data byte for each character to be written, 1 byte per characterCRC-H, CRC-L: the CRC

The response message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><CRC-H><CRC-L>

ADR: the slave address that sent the responseFUNC: echo of the function code 66OFFSET-H, OFFSET-L: echo of value in the query messageLEN-H, LEN-L: echo of value in the query messageCRC-H, CRC-L: the CRC

Function 67(dec): Read long registers (32-bit): (ED&A extension)

The query message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><CRC-H><CRC-L>

ADR: the slave addressFUNC: function code 67 decimal or 43 hexOFFSET-H, OFFSET-L: data address, this is actually an offset, and therefore starts at zero for the firstvariable!LEN-H, LEN-L: the number of variables to be readCRC-H, CRC-L: the CRC

The response message:

<ADR><FUNC><BYTECOUNT><DATA1-H><DATA1-M><DATA1-N><DATA1-L><DATA2-H><DATA2-M><DATA2-N><DATA2-L>...<CRC-H><CRC-L>

ADR: the slave address that sent the responseFUNC: echo of the function code 67BYTECOUNT: the number of data bytes that follow, equal to the number of requested variables * 4DATA1-H, DATA1-M, DATA1-N, DATA1-L: data bytes of each character read, 4 bytes per long, big endianformatCRC-H, CRC-L: the CRC

Function 68(dec): Write long registers (32-bit): (ED&A extension)

The query message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><BYTECOUNT><DATA1-M><DATA1-N><DATA1-L><DATA2-H><DATA2-M><DATA2-N><DATA2-L>...<CRC-H><CRC-L>

Page 23: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 22HPC

ADR: the slave addressFUNC: function code 68 decimal or 44 hexOFFSET-H, OFFSET-L: data address, this is actually an offset, and therefore starts at zero for the firstvariable!LEN-H, LEN-L: the number of variables to be writtenBYTECOUNT: the number of data bytes that follow, equal to the number of variables to be written * 4DATAx-H, DATAx-M, DATAx-N, DATAx-L: data bytes of each long to write, 4 bytes per long, big endianformatCRC-H, CRC-L: the CRC

The response message:

<ADR><FUNC><OFFSET-H><OFFSET-L><LEN-H><LEN-L><CRC-H><CRC-L>

ADR: the slave address that sent the responseFUNC: echo of the function code 68OFFSET-H, OFFSET-L: echo of value in the query messageLEN-H, LEN-L: echo of value in the query messageCRC-H, CRC-L: the CRC

Exception responses:

A MODBUS slave, including the HPC MODBUS slave driver, can return exception codes instead of therequested data in case of an error. The ModbusRead() or ModbusWrite() functions will return a negativevalue if the slave returns an exception code.

Exception code Description Return value of ModbusRead( ) orModbusWrite( )

01 "Illegal function": if the request message has anillegal function number.

-111

02 "Invalid address": if the request message has aninvalid data address, invalid length, or if address+ length goes past the last variable.

-112

03 "Illegal data": if the master attempted to write aninvalid value into a variable.

-113

04 "Slave device failure": For some reason, theslave can not execute the command. Check thedocumentation of the slave.

-114

06 "Busy": the slave is busy, the request could notbe processed.

-116

07 "Nak-negative acknowledgement". For somereason, the slave can not execute thecommand. Check the documentation of theslave.

-117

10 "Gateway path unavailable": Can occur when themodbus communication passes through agateway. The gateway doesn't know how toforward the modbus command to the slave.

-120

11 "Gateway path unavailable": Can occur when themodbus communication passes through agateway. The gateway has forwarded themodbus command to the slave, but did not

-121

Page 24: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 23HPC

receive a response from that slave.

Page 25: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 24HPC

7 Troubleshooting guide

In case the communication between a MODBUS master and slave isn't working, what points can bechecked?

Summary:1. Is the MODBUS master sending requests?2. Does the slave receive the requests?3. Does the slave react to the requests?4. Do the answers of the slave get to the master?5. Are the answers good or do they contain an error code?6. Cable problems causing intermittent failures?

This guide is based on the status LED's that are available on all the serial ports of the E.D.&A. HPC's. Ifother equipment is being used it might be helpful to use an RS232 test tool with LED's.

First, make sure that the MODBUS master program sends packets on a regular base (each second e.g.). The second program example, modmas1.epl, can be used for this.

7.1 Does MODBUS master send messages?

Does the transmit data LED (TxD) of the MODBUS master flash once per second? This is a red LED on allE.D.&A HPC's. A flashing TxD LED indicates that the MODBUS master is sending data packets to the slave. The slave will not react as long as the master isn't transmitting data packets, so make sure the master'sTxD LED is flashing before proceeding to the next step!Verify the following items if the LED isn't flashing:

Check the external power supply of the used serial port if present.Corresponds the serial port where the cable is connected to with the serial port number that isused in the program? The serial port number must be used with the ComOpen() function and thewith the InstallModbusMas() function. The serial port of the V5 HPC and the serial port on theCPU boards of the different HPC's is always COM 0 (null)!Check the return value of the ComOpen() function. Negative values indicate an error if the portcould not be opened.Check the return value of the InstallModbusMas() function. Negative values indicate an error if thedriver could not be installed. The return value of InstallModbusMas() is described in chapter 2.2.The serial port must already be opened when the MODBUS driver is installed because it needs toknow the baud-rate to calculate its timing requirements. The install can also fail if the requiredoption lock is not available.Remove the serial cable and see whether the TxD LED flashes now. It might indicate a problemwith the cable if the LED flashes now. There could be a short-circuit inside the cable or the cableis a programmer cable which places the serial port in "programming" mode:The serial port is not in "communication" mode but in "programming" mode. The PP-80programmer cable, or any other cable that is used to send a program to the HPC, puts the serial

Page 26: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 25HPC

port automatically in "programmer" mode because there is a little extra wire inside the connector. These cables can therefore not be used to communicate with MODBUS on a serial port that alsoworks as a programmer port (= the serial port of the V5-xxx and the port on the CPU of the V10and V50).It's also possible to force the programmer port in "programmer" mode with a jumper or dip-switchon most HPC systems. The used cable has no influence then on the mode of the serial port: itwill always be in "programming" mode. The hardware manuals give a detailed description of thispossibility.The user program and MODBUS have no control over the serial port as long as it is in"programmer" mode, so make sure the port is not in "programmer" mode. The ModbusRead() andModbusWrite() functions give the return value –103 if the serial port is in "programmer" mode.

Is the HPC in RUN mode? A HPC that is not in RUN mode is not executing the user program, so theserial communication with MODBUS isn't working either.

7.2 Does the slave receive these messages?

If the TxD LED of the master works, check the receive data LED (RxD) of the MODBUS slave. This is ayellow or red LED on E.D.&A. HPC's. This RxD LED should flash with the same rate as the TxD LED onthe master. This LED indicates that the data from the MODBUS master arrives at the slave. This LED won't flash if the TxD LED of the master isn't flashing!Check the following points if the receive data LED (RxD) does not flash:

Check the external power supply of the used serial port if present.Most serial ports on E.D.&A. HPC's can work in a RS232 and a RS485 mode. The receive dataLED (RxD) will not lighten if the serial port is in the wrong mode. The hardware manuals describethe jumpers or dip-switches that set the mode of the serial port. The mode of the port of masterand slave must be the same.The cable can be wrong. The connections inside the cables are described in the hardwaremanuals.

It's still possible that the serial cable is wrong when the RxD LED flashes! Especially if: the "signal ground line" and one of the "signal lines" are reversed in an RS232 cable the two wires of a RS485 cable are reversed

The RxD LED might flash while the HPC receives garbage data in these cases.

7.3 Does the slave react to the messages?

If the slave's receive data LED (RxD) flashes, it receives the request from the MODBUS master. Now theslave should send an answer back. The answer is visible on the red transmit data LED (TxD) of the slave.This LED should lighten shortly after the RxD LED.

The TxD LED will only lighten if the RxD LED is working!There are some reasons why this might not be happening:

Corresponds the serial port where the cable is connected to with the serial port number that isused in the program? The serial port number must be used with the ComOpen() function and thewith the InstallModbusMas() function. The serial port of the V5 HPC and the serial port on theCPU boards of the different HPC's is always COM 0 (null)!Check the return value of the ComOpen() function. Negative values indicate an error if the portcould not be opened.Check the return value of the InstallModbus() function. Negative values indicate an error if thedriver could not be installed. The return value of InstallModbus() is described in chapter 2.3. Theserial port must already be opened when the MODBUS driver is installed because it needs to

Page 27: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 26HPC

know the baud-rate to calculate its timing requirements. The install can also fail if the requiredoption lock is not available.The slave number of the MODBUS slave is not correct: the master sends his request to a specificslave number, so the slave must be set to that slave number. This can be done with dip-switcheson the MODBUS slave or with the InstallModbus() function. A detailed description of the dip-switches is available in the hardware reference manual of each HPC.The initialization parameters of the 2 serial ports (master and slave) are not identical: baud-rate,parity and number of stop bits must be the same. The number of data bits must be 8 forMODBUS.The serial port is not in "communication" mode but in "programming" mode. The user programhas no control over the serial port as long as it is in "programmer" mode, so don't use aprogrammer cable on a serial port that is a programmer port also. Is the HPC in RUN mode? A HPC that is not in RUN mode is not executing the user program, sothe serial communication with MODBUS isn't working either.The MODBUS slave reacts only to valid MODBUS requests. These are the packets of data thatthe MODBUS master normally sends. An arbitrary print statement to the serial port of the masterwill result in an active TxD LED on the master and an active RxD LED on the slave but theMODBUS slave doesn't recognize this arbitrary data, so it doesn't answer.The E.D.&A. HPC's only process binary or RTU mode requests. Requests in ASCII mode areignored and will therefore not be answered.The MODBUS master is sending broadcasting packets. A MODBUS slave doesn't answer when itreceives broadcasting packets, so the TxD LED won't lighten.

7.4 Does the answers go from the slave to the master?

If the slave's transmit data LED TxD flashes, it responds to the request of the MODBUS master. Thereceive data LED (RxD) of the master should also flash. This indicates that the answers from the slavearrive at the MODBUS master.This can be wrong if the RxD LED of the master isn't working:

The serial port of the MODBUS master can be in a wrong mode. Most serial ports on E.D.&A.HPC's can work in a RS232 and a RS485 mode. The transmit data LED (TxD) of the master willwork normally, but the receive data LED (RxD) remains out in such a case. Check the jumpers ordip-switches that set the mode of the serial port. The mode of the port of master and slave mustbe the same.The receive data line in the cable might be broken or short-circuited.Again, if the "signal ground line" and one of the "signal lines" are reversed (for RS232) or when thetwo wires are reversed (RS485), the RxD LED might lighten while the HPC gets garbage data.

7.5 Are the answers OK or do they contain error codes?

If the RxD LED of the master flashes, the master's program should now indicate that the MODBUS slavereacted to the request of the master. It's still possible that the slave responds with an error code instead ofthe requested data, but that is a software or configuration problem. In such a case the master might reporta 'protocol error' message. If the MODBUS master is an E.D.&A. HPC, its read or write functions will showan error in their return and status values: –110 … -116.

If possible, check the request function number, especially if the MODBUS master is not anE.D.&A. apparatus. Return value –111 indicates an unsupported function code in a request.The data address can be too large or the MODBUS master might ask too much data at once. Thisresults in the return value –112.The master might be trying to write an illegal value into a variable. Some slaves accept only alimited range of values. Writing illegal values results in the return value –113. For example,E.D.&A. HPC's programmed in the ABC language accept numerical values in the range from 0 to9999 only.

Page 28: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 27HPC

7.6 Do cable problems cause interruptions?

Cabling problems may result in permanent or intermittent failures.

In an RS485 network, there might be a termination or topology problem. In theory, a communications lineshould be terminated at both ends and no terminations should appear in-between. In practice, using thincables may result in other problems. Topology problems may exist if a multipoint network is not built as a bus-topology. Bus topology meansone cable hopping from each node to the next, whiteout shunts in between. Star-topology requires the useof repeaters.

A communication error because of poor line quality is also possible. This might result in CRC errors ortimeouts. One solution is reducing the speed of the MODBUS master and slave to 1200 or 300 baud andsee if the communication works then. A better, shielded cable can also solve the problem. Increasing the number of tries of the master mayimprove things as well.

It's much easier to find a problem with the serial line if the communication speed is temporaryreduced to 1200 or even 300 baud on the master and the slave system. It will take much more timenow for a packet to travel over the serial line, so the transmit and receive LED's will be active longer.It's possible to simulate a MODBUS master with a simple test program on an EPL HPC. Thisprogram regularly sends out a row of characters that look just like a request of a real MODBUSmaster, so the slave will react on it and send an answer back. Pay attention, this program sends a request to SLAVE NUMBER 1. Serial port 1 simulates theMODBUS master; the MODBUS slave is installed on serial port 2. The program is attached asprogram example 'modsla1.epl'.The easiest way to test this is by connecting the transmit data line (TxD) of serial port 1, (pin 4 onDB9 connectors, pin 2 on DB25 connectors), to the receive data line (RxD) of serial port 2, (pin 2 onDB9 connectors, pin 3 on DB25 connectors). The ground connection is made inside the HPC if thetwo serial ports are in the same system. The two serial ports must be RS232 ports. It's also possibleto use the program if the serial ports are from two different HPC's. A serial communication cablebetween the two systems is needed then. It's possible to use the same program for the two HPCsystems.The LED indicators of the slave serial port show what happens:A flashing receive data LED (RxD), yellow or red: serial port 2 receives the requestA flashing transmit data LED (TxD), always red: serial port 2 transmits the answer.So, the MODBUS slave is working properly if its TxD LED flashes. It answers to the requests of themaster.

Page 29: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 28HPC

8 Examples

The example programs can be used on any E.D.&A. EPL HPC. Modmas4.epl and Modsla4.epl arespecifically written for the V5-2xx HPC: they use the character display of this HPC.

Modsla1.epl Emulation of a MODBUS master on COM 1A MODBUS slave on COM 2.

Sends a test message once per second.

Modmas1.epl Simple program with one ModbusRead(). Reads data from one slave, once persecond.

Modmas2.epl Program with two MODBUS masters and one MODBUS slave.

Modmas3.epl Program with one MODBUS master that reads data from 3 different slaves.This program reads the data at the highest available speed.

Modmas4.epl andModsla4.epl

Demo program for connecting two V5 HPC's. Uses arrays of longs to store thetransfer blocks.

8.1 Modsla1.epl

/* modsla1.epl** Example program installing 1 modbus slave driver on COM2,* and transmitting test requests on COM1.* To test the modbus slave on COM2 in RS232 mode, connect a* test wire from COM1 TxD to COM2 RxD. The modbus slave now* receives the requests and will respond. This is indicated* by flashing of the COM2 TxD LED.*/

#include <stdcfg.h>#include <kernel.h>

main(){ if( ^/b1s) comprintnc( 1, "\0x01\0x03\0x00\0x00\0x00\0x0A\0xC5\0xCD", 8);}

init(){ S1 = comopen( 1, "9600,n,8,1");

S2 = comopen( 2, "9600,n,8,1"); S3 = installmodbus( 2, 1, 0);}

Page 30: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 29HPC

8.2 Modmas1.epl

/* modmas1.epl** Example program using 1 MODBUS master driver.* From slave 1: read registers 257..260 and store in s23..s26.*/

#include <stdcfg.h>#include <kernel.h>

main(){ /* Each second: set the start flag */

if( ^/b1s) m20 = 1;

/* ModbusRead arguments: start, comport, block */

s20 = ModbusRead( &m20, 1, &L100);

if( s20==2) /* count success */ s21++; if( s20 < 0) /* count fails */ s22++;

a1.1 = m20; /* put startflag on output */}

init(){ /* Use s1 as indicator to verify all open's and install's */ s1 = 0;

/* Open com port 1, transmit and receive buffer * are 100 bytes. */

if( comopenq( 1, "9600,n,8,1", 100, 100) < 0) s1 = s1 + 1;

/* Install MODBUS master driver on com1, with * 100 bytes buffer, * 1000 millisec timeout, * 3 attempts. */

if( InstallModbusMas( 1, 100, 1000, 3) < 0) s1 = s1 + 2;

/* Init MODBUS block: Slave, Variable, Count, Status, Data */

L100 = 1; L101 = 40257;

Page 31: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 30HPC

L102 = 4; L103 = 0; L104 = (long)&s23;}

8.3 Modmas2.epl

/* modmas2.epl* Example program using 2 MODBUS master drivers, and 1 slave driver. * Master drivers on com1 and com5, slave driver on com6.** From slave 1 on com1, read registers 10..13 and store in s23..s26.* To slave 1 on com5, write registers 10..13 with data from s23..s26.* From slave 1 on com5, read coils 201..250, store in m201..m250.** The slave driver can be used to connect to another MODBUS master* system, such as an operator terminal or supervisory computer.*/

#include <stdcfg.h>#include <kernel.h>

main(){ /* Set the start flags periodically: each 500ms */

if( ^/b5t) { m20 = 1; m21 = 1; } if( ^/b1s) m22 = 1; /* Read on com 1 from slave 1 */

s20 = ModbusRead( &m20, 1, &L100);

/* Write on com 5 to slave 1 */

s30 = ModbusWrite( &m21, 5, &L110);

/* Read on com 5 from slave 1 */

s40 = ModbusRead( &m22, 5, &L120);

}

init(){ /* Use s1 as indicator to verify all open's and install's */ s1 = 0;

Page 32: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 31HPC

/* Open com ports, transmit and receive buffers are 100 bytes */

if( comopenq( 1, "2400,n,8,1", 100, 100) < 0) s1 = s1 + 1;

if( comopenq( 5, "2400,n,8,1", 100, 100) < 0) s1 = s1 + 2;

if( comopenq( 6, "9600,n,8,1", 100, 100) < 0) s1 = s1 + 4;

/* Install MODBUS master drivers, with * 100 byte buffers, * 500 millisec timeout, * 3 attempts. */

if( InstallModbusMas( 1, 100, 500, 3) < 0) s1 = s1 + 8;

if( InstallModbusMas( 5, 100, 500, 3) < 0) s1 = s1 + 16;

/* Install MODBUS slave driver, * on com6, * as slave number 10. */

if( InstallModbus( 6, 10, 0) < 0) s1 = s1 + 32;

/* Init MODBUS blocks: Slave, Variable, Count, Status, Data */

L100 = 1; L101 = 40010; L102 = 4; L103 = 0; L104 = (long)&s23;

L110 = 1; L111 = 40010; L112 = 4; L113 = 0; L114 = (long)&s23;

L120 = 1; L121 = 201; L122 = 50; L123 = 0; L124 = (long)&m201;}

Page 33: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 32HPC

8.4 Modmas3.epl

/* modmas3.epl** Example program requesting data from 3 slaves connected to com1.* From slave 10, read first 10 registers and store in s50..s59.* From slave 11, read first 10 registers and store in s60..s69.* From slave 12, read first 10 registers and store in s70..s79.*/

#include <stdcfg.h>#include <kernel.h>

main(){ /* To get the highest speed, we keep the startflags set */

m20 = 1; m21 = 1; m22 = 1;

/* ModbusRead arguments: start, com port, block */

s20 = ModbusRead( &m20, 1, &L100); s21 = ModbusRead( &m21, 1, &L105); s22 = ModbusRead( &m22, 1, &L110);}

init(){ /* Use s1 to verify all open's and install's */ s1 = 0;

/* Open com port 1, buffers are 100 bytes */

if( comopenq( 1, "9600,n,8,1", 100, 100) < 0) s1 = s1 + 1;

/* Install MODBUS master driver on com1, with * 100 bytes buffer, * 300 millisec timeout, * 3 attempts. */

if( InstallModbusMas( 1, 100, 300, 3) < 0) s1 = s1 + 2;

/* Init MODBUS block: Slave, Var, number of variables, Stat, Data */

L100 = 10; L101 = 40001; L102 = 10; L103 = 0; L104 = (long)&s50;

Page 34: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 33HPC

L105 = 11; L106 = 40001; L107 = 10; L108 = 0; L109 = (long)&s60;

L110 = 12; L111 = 40001; L112 = 10; L113 = 0; L114 = (long)&s70;}

8.5 Modmas4.epl

/* modmas4.epl and modsla4.epl** 2 example programs for V5 HPC's where one is master and the * other is slave.* The master HPC reads E1.25 and E1.26 from the slave and * puts this on its outputs A1.1 and A1.2.* The state of E1.25 and E1.26 from the master HPC are written* to the slave on A1.1 and A1.2.*/

#include <stdcfg.h>#include <kernel.h>#include <display.h>

bool comok;bool Doread;bool DoWrite;

long WriteVar[5];long ReadVar[5];

main(){ short status;

if(comok==FALSE) DispPrints(2,1,"Uit bedrijf"); else DispPrints(2,1,"In bedrijf ");

DoRead=1; DoWrite=1;

status=ModbusRead(&DoRead,0,ReadVar);

if(status==2) comok=TRUE;

Page 35: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 34HPC

if(status<0) comok=FALSE;

status=ModbusWrite(&DoWrite,0,WriteVar);

if(status==2) comok=TRUE; if(status<0) comok=FALSE;}

init(){ DispClear();

DispPrints(1,1,"Master HPC");

S1 = ComOpen(0,"9600,n,8,1"); S2 = InstallModbusMas(0,200,100,3);

ReadVar[0]=1; /* In Slave 1 */ ReadVar[1]=10025; /* Read E1.25 */ ReadVar[2]=2; /* and E1.26 */ ReadVar[3]=0; ReadVar[4]=(long)&a1.1; /* Store at a1.1 */

WriteVar[0]=1; /* In Slave 1 */ WriteVar[1]=9001; /* Write A1.1 */ WriteVar[2]=2; /* and A1.2 */ WriteVar[3]=0; WriteVar[4]=(long)&e1.25;/* Read e1.25 */}

8.6 Modsla4.epl

/* modsla4.epl** slave program to use with modmas4.epl */

#include <stdcfg.h>#include <kernel.h>#include <display.h>

main(){

}

init(){ DispClear();

Page 36: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 35HPC

DispPrints(1,1,"Slave HPC");

S1 = ComOpen(0,"9600,n,8,1"); S2 = InstallModbus(0,1,0);}

Page 37: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driverBook E26 Version 2.12

© 2014 E.D.&A. 36HPC

9 Versions book 26

Version number Date Modifications

2.08

2.09 28/10/2003 New option value added. InstallModbus function.

2.10 14/07/2004 Complete check manual

2.11 11/08/2006 Added detailed descriptions of some modbus query and responsemessages

2.12 21/5/2007 Added description of more modbus exception codes and errorcodes of the Modbusread() and modbuswrite() functions.Added the chapter "Which modbus function codes are generatedby ModbusRead() and ModbusWrite()"

7

Page 38: MODBUS RTU compatible master and slave driver · HPC 1 Version Table of contents ... The hardware reference manuals of each HPC system describe the installation and use of the option

MODBUS RTU compatible master and slave driver E26 2.12Versie

© 2014 E.D.&A. 37HPC

Index- D -DEINSTALLMODBUS 9

- I -INSTALLMODBUS 7

INSTALLMODBUSMAS 5

- M -MODBUSREAD 10

MODBUSWRITE 10


Recommended