Self Cleaning NH4 N Modbus Instruction en
Self Cleaning NH4 N Modbus Instruction en
Self Cleaning NH4 N Modbus Instruction en
NH4-N Sensor
MODBUS RTU
Programmer Manuel
1 / 21
Yantai Winmore Trade Co., Ltd.
Table of Contents
1 MODBUS RTU Overview..................................................................................................... 3
1.1 Scope........................................................................................................................ 3
1.2 MODBUS Command Structure.................................................................................3
1.3 MODBUS RTU for NH4-N Sensor............................................................................. 5
1.4 MODBUS RTU Function Code for NH4-N Sensor..................................................... 5
1.5 Data formats in NH4-N Sensor.................................................................................7
2 MODBUS RTU Commands for NH4-N Sensor................................................................... 10
2.1 Overview................................................................................................................ 10
2.2 Command Description........................................................................................... 10
3 Procedure to get NH4-N value..........................................................................................21
2 / 21
Yantai Winmore Trade Co., Ltd.
1.1 Scope
This document is about MODBUS of Self-cleaning NH4-N probes with hardware Rev1.0 and
software Rev1.6 or later. This document is intended for software programmers with detailed
information about MODBUS RTU protocols.
3 / 21
Yantai Winmore Trade Co., Ltd.
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
6 / 21
Yantai Winmore Trade Co., Ltd.
Start address (high bits) 0x00 Start address (high bits) 0x00
Start address (low bits) 0x01 Start address (low bits) 0x01
Number of registers (high bits) 0x00 Number of registers (high bits) 0x00
Number of registers (low bits) 0x02 Number of registers (low bits) 0x02
Number of bytes 0x04
Register value (high bits) 0x00
Register value (low bits) 0x0A
Register value (high bits) 0x01
Register value (low bits) 0x02
Figure 13: Example of Request frame and response frame for write operation
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
D = (-1)S×(1.0+M/223)×2E-127
= (-1)0×(1.0+8087142/223)×2132-127
8 / 21
Yantai Winmore Trade Co., Ltd.
= 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)
“Y” is 0x59
9 / 21
Yantai Winmore Trade Co., Ltd.
“L” is 0x4C
2.1 Overview
In order to communicate with NH4-N sensor 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.
Start Address Number of registers Register 1-7 MODBUS Function code
0x0900 0x07 SN 0x03
Figure 18: Register definition of Get SN Command
Below is an example of request and response frames to get SN “YL1014010022” from a slave
device (address 0x01) .
10 / 21
Yantai Winmore Trade Co., Ltd.
12 / 21
Yantai Winmore Trade Co., Ltd.
13 / 21
Yantai Winmore Trade Co., Ltd.
Value 0x01 0x03 0x04 0x01 0x00 0x01 0x00 0xfa 0x5f
Figure 43: Response frame for Get Hardware and Software Rev Command
15 / 21
Yantai Winmore Trade Co., Ltd.
16 / 21
Yantai Winmore Trade Co., Ltd.
17 / 21
Yantai Winmore Trade Co., Ltd.
18 / 21
Yantai Winmore Trade Co., Ltd.
19 / 21
Yantai Winmore Trade Co., Ltd.
Byte 0 1 2 3 4 5 6 7 8 15 16
Value 0x01 0x10 0x32 0x00 0x00 0x01 0x02 0x0a 0x00 0xb3 0x33
Figure 83: Request frame to Set Brush Interval of time Command
Note: Time 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 0x32 0x00 0x00 0x01 0x0f 0x71
Figure 84: Response frame to Set Brush Interval of time Command
2.2.19 Get Brush Interval of time
Purpose:Get the interval of time for brush between each rotation, default time is 30 min.
The interval of time can be read from 1 MODBUS registers starting from address 0x3200.
Start address Number of registers Register 1 MODBUS function code
0x3200 0x01 Interval of time(min) 0x03
Figure 85: Register definition of Get Brush Interval of time Command
Below is an example of request and response frames for getting brush interval of time from a
device with slave address 0x01, assuming returned time is 30min.
Definition Device Function Start address Number of registers CRC
address code
Byte 0 1 2 3 4 5 6 7
Value 0x01 0x03 0x32 0x00 0x00 0x01 0x8a 0xb2
Figure 86: Request frame to Get Brush Interval of time Command
20 / 21
Yantai Winmore Trade Co., Ltd.
Byte 0 1 2 3 4 5 6
Value 0xFF 0x03 0x02 0x03 0x00(reserve) 0x91 0x60
Figure 90: Response frame of get slave device ID comment
Delay>=20s
More than 10 N
Delay 2S
times?
Start brush active – make the brush active once power on , then the brush will work at the
set interval time.
Get temperature and NH3-N values – Get measurement results including temperature (C)
and NH3-N (mg/L) after 20 seconds of brush active.
Note: Please pay attention to highlighted portion (red) in the flow chart above. It’s highly
recommended that customers get an average NH3-N results after 10 consecutive
measurements, then perform display or other process.
21 / 21