DO Modbus Instruction
DO Modbus Instruction
Sensor
MODBUS RTU
Programmer Manuel
1 / 17
Table of Contents
1 MODBUS RTU Overview ...................................................................................................... 3
2 / 17
1 MODBUS RTU Overview
1.1 Scope
This document is about MODBUS of optical dissolved oxygen probes with hardware Rev2.0 and
software Rev6.2 or later. This document is intended for software programmers with detailed
information about MODBUS RTU protocols.
3 / 17
1.2.2 MODBUS RTU Transmission Mode
When devices communicate on a MODBUS using RTU (remote terminal unit) mode, each 8-bit
byte contains two 4-bit hexadecimal characters. The main advantage of the RTU mode is that it
has higher character density, which enables better throughput compare to ASCII mode at same
baud rate. Each RTU message must be transmitted in a continuous string of characters.
RTU mode format for each byte (11 bits):
Encoding system 8 bit binary
Each 8-bit packet contains 4-bit hexadecimal characters (0-9, A-F)
Bit per byte: 1 start bit
8 data bits, least significant bit first
No parity check
1stop bits
Baud rate: 9600bps
Serial transmission of characters:
Every character or byte is sent under this sequence (left to right):
Least Significant Bit (LSB)……Most Significant Bit(MSB)
Start 1 2 3 4 5 6 7 8 Stop
Figure 3: RTU Mode Bit Sequence
CRC Field Structure:
Redundancy check (CRC16)
Frame Structure:
Slave address Function Data CRC
Code
2 bytes
1 byte 1 byte 0…252 bytes
CRC Low CRC High
Figure 4: RTU Message Frame Structure
Maximum size of MODBUS frame is 256 bytes.
1.2.3 MODBUS RTU Message Frame
In RTU mode, message frames need to be separated by an idle interval of at least 3.5 character
lengths. In rest of this document, this idle interval is called t3.5.
Based on standard MODBUS definition, message frame starts with t3.5 idle interval, and similarly,
ends with t3.5 idle interval. Device address and Function code are both 8-bit byte. Data character
string has n*8 bits, it contains information about register start/end address and number of
registers for read/write operation. CRC field is 16 bit in length.
Start Device Function Data CRC End
address code
Value Idle for 3.5 1-247 Comply Comply CRC CRC Idle for 3.5
character with with Low High character
length MODBUS MODBUS length
function data
code format format
Length 3.5 1 1 n 1 1 3.5
(byte)
Figure 7: Message frame structure for MODBUS
Sample code:
1. If your compiler has similar library functions, it can be called directly. For example if C
language is used, we can directly call memcpy() function in C library to convert floating point
number. Sample code:
float floatdata;//floating point data to be converted
void* outdata;
memcpy(outdata,&floatdata,4);
If floatdata=17.625,
In little-endian storage mode after the function is called:
Value at address of outdata is 0x00
Value at address of (outdata+1) is 0x00
Value at address of (outdata+2) is 0x8D
Value at address of (outdata+3) is 0x41
In big-endian storage mode after the function is called:
Value at address of outdata is 0x41
Value at address of (outdata+1) is 0x8D
Value at address of (outdata+2) is 0x00
Value at address of (outdata+3) is 0x00
2. If your complier doesn’t have the conversion function, then the following function can be
used:
void memcpy(void *dest,void *src,int n)
{
char *pd = (char *)dest;
char *ps = (char *)src;
for(int i=0;i<n;i++) *pd++ = *ps++;
}
Then you can get same result by calling this function memcpy(outdata,&floatdata,4);
Example: Convert binary floating point number 0100 0010 0111 1011 0110 0110 0110 0110B to a
decimal number
Step 1: Separate this binary number 0100 0010 0111 1011 0110 0110 0110 0110B and get values
of Sign , exponent and fraction.
0 10000100 11110110110011001100110B
1 Sign bit 8-bit exponent 23-bit fraction
Sign bit(s): 0
Exponent(E):10000100B=1×27+0×26+0×25+0×24+0×23+1×22+0×21+0×20
=128+0+0+0+0+4+0+0=132
Fraction(M):11110110110011001100110B =8087142
Step 2: Calculate decimal value
8 / 17
D = (-1)S×(1.0+M/223)×2E-127
= (-1)0×(1.0+8087142/223)×2132-127
= 1×1.964062452316284×32
= 62.85
Reference code:
float floatTOdecimal(long int byte0, long int byte1, long int byte2, long int byte3)
{
long int realbyte0,realbyte1,realbyte2,realbyte3;
char S;
long int E,M;
float D;
realbyte0 = byte3;
realbyte1 = byte2;
realbyte2 = byte1;
realbyte3 = byte0;
if((realbyte0&0x80)==0)
{
S = 0; //Positive
}
else
{
S = 1; //Negative
}
E = ((realbyte0<<1)|(realbyte1&0x80)>>7)-127;
M = ((realbyte1&0x7f) << 16) | (realbyte2<< 8)| realbyte3;
D = pow(-1,S)*(1.0 + M/pow(2,23))* pow(2,E);
return D;
}
Note:
Function parameters byte0, byte1, byte2 and byte3 represent the 4 sections of a binary
floating number.
Return value is value of decimal number after conversion
For example when a command is sent to a sensor to get temperature value, response frame from
the sensor will have measured temperature. If the values are 4 byte floating point number
0x00,0x00,0x8d,0x41, then the following function can be used to get temperature in decimal
value:
float temperature = floatTOdecimal( 0x00, 0x00, 0x8d, 0x41);
and temperature = 17.625.
1.5.2 Characters
Definition: Character is shown by ASCII code.
Example: String “YL” could be shown by corresponding ASCII codes (refer to ASCII character chart)
9 / 17
“Y” is 0x59
“L” is 0x4C
2.1 Overview
In order to communicate with optical dissolved oxygen via MODBUS RTU, master terminal
software will be needed. MODBUS RTU is an open standard. There are free commercial software
tools available. For applications described in this document, MODBUS register address starts from
1. However, slave address in MODBUS protocol starts from 0, and usually master software
compiles addresses. For example, register address 2090 will be compiled by master software as
address 2089.
2.2.2 Get SN
Purpose: Get sensor probe’s serial number (SN). Each sensor probe has a unique SN.
Serial Number can be read from 7 continuous MODBUS registers starting from address 0x0900
10 / 17
Start Address Number of registers Register 1-7 MODBUS Function code
0x0900 0x07 SN 0x03
Figure 18: Register definition for Get SN command
Below is an example of request and response frames to get SN “YL0114010022” from a slave
device (address 0x01)
Definition Address Function Starting address Number of CRC
code registers
Byte 0 1 2 3 4 5 6 7
Value 0x01 0x03 0x09 0x00 0x00 0x07 0x07 0x94
Figure 19: Request frame to get SN
Definition Address Function Number of Register value CRC
code byte
Byte 0 1 2 4-15 3 16 17 18
Value 0x01 0x03 0x0E 0x00 “YL0114010022” 0x00 0x19 0x66
Figure 20: Response frame sample for Get SN command
Note: SN value is in ASCII code as below:
Byte 4 5 6 7 8 9 10 11 12 13 14 15
Value 0x59 0x4C 0x30 0x31 0x31 0x34 0x30 0x31 0x30 0x30 0x32 0x32
Figure 21: Sensor probe’s SN
12 / 17
Byte 0 1 2 3-4 5-6 7 8
Value 0x01 0x03 0x04 0x02 0x00 0x05 0x07 0xb9 0x19
Figure 31: Response frame of Get Hardware and Software Rev Command
Below is an example of request and response frames for setting sensor cap coefficients to a
device with slave address 0x01.
14 / 17
Definition Device Function Start Number of Number Register CRC
address code address registers of bytes value
Byte 0 1 2 3 4 5 6 7-38 39 40
Value 0x01 0x10 0x27 0x00 0x00 0x10 0x20 K0-K7 XX XX
Figure 44: Request frame of set sensor cap coefficient command
Note: Ki(i=0-7) sensor cap coefficients, floating point number in little-endian storage mode
Definition Device Function Start address Number of CRC
address code registers
Byte 0 1 2 3 4 5 6 7
Value 0x01 0x10 0x27 0x00 0x00 0x10 0xcb 0x71
Figure 45: Response frame of set sensor cap coefficient command
15 / 17
3 Procedure to get DO value
Procedures Command
Power On
Delay>=4s
Y
Get Temperature and DO Get temperature
and DO values
Y
Calculate DO(mg/L)
Get temperature and DO values – Get measurement results including temperature (C)
and DO (%) after 1 second of measurements.
Note: Please pay attention to highlighted portion (red) in the flow chart above. It’s highly
recommended that customers get an average DO results (%) after 10 consecutive
measurements, then convert the average DO percentage to a DO value in mg/L unit.
Calculate DO(mg/L), converting the average DO percentage to a DO value in mg/L unit.
According to the formula: DO(mg/L)=DO(%)*X1*X2*1.4276;
1 ml/L = 1.4276 mg/L;
lnX1= Al + A2 100/T + A3 ln T/100 + A4 T/100 + S* [B1 + B2 T/100 + B3 (T/100)^2];
Al = -173.4292, B1 = - 0.033096,
A2 = 249.6339, B2 = 0.014259,
A3 = 143.3483, B3 = - 0.001700;
A4 = -21.8492;
T =273.15 + t ,T represent for Kelvin temperature and t represent for Celsius
temperature;
S is salinity, S=0 in pure water;
X2= (Phmg - u) / (760 - u);
16 / 17
Phmg= pressure * 760 / 101.325, pressure is the barometer in kPa unit;
Logu= 8.10765 - (1750.286/ (235+t)), t represent for Celsius temperature.
Reference code:
#include <math.h>
Phmg = pressure*760/101.325;
X2 = ((Phmg - u)/(760 - u));
DO(mg/L)= DO(%)*X1*X2*1.4276;
17 / 17