APN0020 Rev 1.1 Writing To Modbus Devices
APN0020 Rev 1.1 Writing To Modbus Devices
This application note discusses how to perform Modbus writes from within a script. It is assumed that the user has
Admin privileges and scripting rights for the device being worked on. To request scripting rights, contact
[email protected].
2. References
The following documents were used in compiling this Application Note.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 2 of 10
3. Background
Modbus is an industrial protocol standard that was created by Modicon, now Schneider Electric, in the late 1970’s
for communication among programmable logic controllers (PLCs). Modbus remains the most widely available
protocol for connecting industrial devices. The Modbus protocol specification is openly published, and use of the
protocol is royalty-free.
The Modbus protocol is defined as a master/slave protocol, meaning a device operating as a master will poll one or
more devices operating as slaves. A slave device cannot volunteer information; it must wait to be asked for it. The
master will write data to a slave device’s registers and read data from a slave device’s registers. A register address is
always in the context of the slave’s registers. Senquip devices are always the master.
The most used form of Modbus protocol is RTU over RS-485. Data is transmitted in 8-bit bytes, one bit at a time, at
baud rates ranging from 1200 bits per second (baud) to 115200 bits per second.
A master's query consists of a slave address, a function code defining the requested action, any required data, and
an error checking field. A slave's response consists of fields confirming the action taken, any data to be returned,
and an error checking field.
An example of a Modbus unsigned holding register read from register 5 of a slave device with address 1, and the
response, is shown below. Two byte fields are always sent Most Significant Byte (MSB) first and Least Significant Byte
(LSB) second.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 3 of 10
MOD_RSsim includes a window that allows you to see the incoming messages and responses from the simulator.
Modbus RTU mode includes an error–checking field which is based on a Cyclical Redundancy Check (CRC) performed
on the message contents. The CRC field checks the contents of the entire message. It is applied regardless of any
parity checking method used for the individual characters of the message. The CRC field contains a 16–bit value
implemented as two 8–bit bytes. The CRC field is appended to the message as the last field in the message. When
this is done, the low–order byte of the field is appended first, followed by the high–order byte. Senquip provides a
function to calculate the CRC that is documented in the Senquip Scripting Guide.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 4 of 10
XNNNN
Where “X” is one of the following Modbus function codes. For example, if you see a Modbus register number 41221,
this is really register number 1221 and should be accessed using the Modbus function 04 “Read unsigned Input”.
To make things worse, this is not always the case, sometimes register 1 should be addressed at address 0 and
sometimes at address 1.
The serial port is configured for RS232 with a baud rate of 19200, 8 data bits, no parity, and 1 stop bit.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 5 of 10
Modbus reads are set to timeout if no response is received after 0.5 seconds. A minimum delay of 15 milliseconds is
set between a response arriving and the next Modbus read. Although the Modbus standard sets the minimum as 3.5
character periods, we find that many Modbus sensors need a longer time between reads.
Modbus 1 was configured to read an unsigned input from slave address 1, register 5.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 6 of 10
MOD_RSsim is configured to have address 1 and register 5 is given the value 5. Serial port settings are set to match
the Senquip serial peripheral.
It is confirmed that the Senquip Portal is reading a value of 5 on the Modbus 1 peripheral.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 7 of 10
The standard structure of a Modbus message is given in Figure 2. A Modus message to write to a 16-bit register will
have a function code of 6, and will specify the register to be written to, and the data to be written. Specifically, to
write the value 55 (37 hex) to register 7, the required message is shown in Figure 11.
A function, sendVal, which writes an unsigned 16-bit number to a holding register is created. SendVal takes as input,
an object containing the address and register of the Modbus slave device and the data to be written. It creates a
Modbus write message, and initiates a serial write to execute the register update.
The function converts the decimal slave address that has been passed to it, to a string representation of a 1 byte
hexadecimal number using the encode function. The encode function convers a number into a string representation
using the number type specified. Similarly, the slave register address and data to be sent are encoded into strings
representing 2 byte hexadecimal numbers.
The first part of the Modbus write message is assembled by adding the address, function code 6, the register to be
written, and the value to write. A checksum for this data is calculated using the Senquip CRC function and is
appended as a 16-bit string representation. Note how the byte order in the CRC is swapped by placing a minus in
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 8 of 10
front of the number type in the encode function. The swapping of the bytes is required because the SQ.crc function
returns the data LSB first, and the Modbus standard requires the data to be sent MSB first. The CRC is appended to
complete the Modbus message.
The sendVal function is called from the data handler. The data handler is called on every base interval after
measurement collection is complete. In this example, the same write will repeat every time the data handler is
called. In a real example, the sendVal would only be called when Modbus data needs to be updated.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 9 of 10
The function could be enhanced to facilitate writing to 8-bit and 16-bit registers. Range checking and data validation
should be implemented.
7. Conclusion
Reading from and writing to Modbus registers with a Senquip device is simple using available settings and a simple
script.
Writing to Modbus registers can be useful in configuring sensors, and in the remote control of machines. In the
figure below, the pump is remotely controlled by writing to an engine controller using a Modbus write command.
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.
Document Number Revision Prepared By Approved By
APN0020 1.1 NGB NB
Title Page
Writing to Modbus Devices 10 of 10
function sendVal(sendObj){
let s = SQ.encode(sendObj.sadr,SQ.U8); // encode dec address into hex
let r = SQ.encode(sendObj.radr,SQ.U16); // encode dec register number into hex
let v = SQ.encode(sendObj.val,SQ.U16); // encode dec data into hex
let a = s+'\x06'+r+v; // 6 is the MODBUS write unsigned 16 function code
let c = SQ.crc(a); // use the Senquip CRC function to calculate the Modbus CRC
c = SQ.encode(c, -SQ.U16); // encode the CRC function in hex + flip byte order
let t = a+c; // create the final Modbus write message
SERIAL.write(1,t,t.length); // send the message to serial port 1
}
SQ.set_data_handler(function(data) {
}, null);
Copyright © 2022 Senquip Pty Ltd. Senquip Pty Ltd (“the Company”) is the owner of the copyright and all confidential information in this docu ment. The
document and its text, images, diagrams, data and information it contains must not be copied or reproduced in whole or in part, in any form or by any means,
without the prior written consent of the Company.