COD Modbus Instruction
COD Modbus Instruction
COD Sensor
1 / 18
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 WINMORE’s Optical COD Sensor........................................................ 5
1.4 MODBUS RTU Function Code for WINMORE’s Optical COD Sensor............................... 5
1.5 Data formats in optical COD sensor.................................................................................7
2 MODBUS RTU Commands for Optical COD Sensor.......................................................... 10
2.1 Overview........................................................................................................................ 10
2.2 Command Description................................................................................................... 10
3 Procedure to get COD value............................................................................................. 17
2 / 18
Yantai Winmore Trade Co., Ltd.
1.1 Scope
This document is about MODBUS of optical COD probes with hardware Rev1.0 and software
Rev1.5 or later. This document is intended for software programmers with detailed information
about MODBUS RTU protocols.
3 / 18
Yantai Winmore Trade Co., Ltd.
When devices communicate on a MODBUS using RTU (remote terminal unit) mode, each 8-bit
byte message 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
1 stop 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 Code Data CRC
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.
4 / 18
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 Winmore’s MODBUS
5 / 18
Yantai Winmore Trade Co., Ltd.
Request Frame:
Function code 1 Byte 0x03
Start address 2 Bytes 0x0000….0xfffff
Number of registers 2 Bytes 1…125
Figure 8: Request frame for read registers
Response Frame:
Function code 1 byte 0x03
Number of byte 1 byte N×2
Register data N×2 bytes
N = number of registers
Figure 9: Response frame for read registers
Below is an example of Request and Response frames (Read register 108-110. Register 108 is read
only with 2-byte value of 0X022B. Registers 109-110 have values of 0X0000 and 0X0064).
Request Frame Response Frame
Data format Hexadecimal Data Format Hexadecimal
Function code 0x03 Function code 0x03
Start address(high bits) 0x00 Number of bytes 0x06
Start address (low bits) 0x6B Register value (high bits, 108) 0x02
Number of registers (high bits) 0x00 Register value (low bits, 108) 0x2B
Number of registers (low bits) 0x03 Register value (high bits, 109) 0x00
Register value (low bits, 109) 0x00
Register value (high bits, 110) 0x00
Register value (low bits, 110) 0x64
Figure 10: Example of request frame and response frame for read operation
1.4.2 MODBUS Function Code 0x10: Write Registers
This function code is to write a block of continuous registers at a remote device. Request frame
contains register data. Each register data have two character bytes. Response frame contains
function code, start address, and number of registers that completed write operation.
Request Frame:
Function code 1 byte 0x10
Start address 2 bytes 0x0000….0xffff
Number of registers 2 bytes 0x0001….0x0078
Number of bytes 1 byte N×2
Register data N×2 bytes value
N = number of registers
Figure 11: Request frame for write operation
Response Frame:
Function Code 1 byte 0x10
Start address 2 bytes 0x0000….0xffff
Number of registers 2 bytes 1…123(0x7B)
N = number of registers
Figure 12: Response frame for write operation
Below is an example of Request Frame and Response frame (write 0x000A and 0x0102 to two
6 / 18
Yantai Winmore Trade Co., Ltd.
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
8 / 18
Yantai Winmore Trade Co., Ltd.
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
= 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.
9 / 18
Yantai Winmore Trade Co., Ltd.
2.1 Overview
In order to communicate with optical COD probe 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.
10 / 18
Yantai Winmore Trade Co., Ltd.
Below is an example of request and response frames for sending a Start Measurement command
to a device with slave address 0x01.
Definition Device Function Start address Number of CRC
address code registers
Byte 0 1 2 3 4 5 6 7
Value 0x01 0x03 0x25 0x00 0x00 0x01 0x8F 0x06
Figure 19: Request frame to Start Measurement Comment
Definition Device Function Number of Register value CRC
address code bytes
Byte 0 1 2 3~4 5 6
Value 0x01 0x03 0x00 meaningless
Figure 20: Response frame for Start Measurement Command
2.2.3 Get Temperature and COD values
Purpose : Get temperature and COD/TOC measurement results. Temperature unit is Celsius
degree ( C), COD/TOC unit is mg/L. User calibration process is automatically applied to
COD/TOC value.
Get Temperature :
SEND:
Definition Device Function Start address Number of registers CRC
address code
Byte 0 1 2 3 4 5 6 7
Value 0X01 0X03 0X26 0X00 0X00 0X02 0XC7 0X43
RECIEVE:
Definition Device Function Number TEMPERATURE○
1 CRC
address code of bytes
Byte 0 1 2 3~4 5~6 7 8
Value 0X01 0X03 0X04 0X00 0X00 0X00 0X00 0XFA 0X5F
Get COD:
SEND:
Definition Device Function Start address Number of registers CRC
address code
Byte 0 1 2 3 4 5 6 7
Value 0X01 0X03 0X26 0X02 0X00 0X02 0X 6E 0X83
RECIEVE:
Definition Device Function Number COD○
1 CRC
address code of bytes
Byte 0 1 2 3~4 5~6 7 8
Value 0X01 0X03 0X04 0X00 0X00 0X00 0X00 0XFA 0X5F
Get TOC:
SEND:
Definition Device Function Start address Number of registers CRC
11 / 18
Yantai Winmore Trade Co., Ltd.
address code
Byte 0 1 2 3 4 5 6 7
Value 0X01 0X03 0X26 0X06 0X00 0X02 0X C7 0XE8
RECEIVE:
Definition Device Function Number TOC○
1 CRC
address code of bytes
Byte 0 1 2 3~4 5~6 7 8
Value 0X01 0X03 0X04 0X00 0X00 0X00 0X00 0XFA 0X5F
1 :
○ Floating point numbers are stored in little endian mode
12 / 18
Yantai Winmore Trade Co., Ltd.
K(3-6) B(7-10)
0x00 0x00 0x80 0x3F 0x00 0x00 0x00 0x00
Figure 30: Registers for two coefficients K and B.
2.2.7 Set Customer Calibration Coefficients
Purpose: Set two calibration coefficients K and B.
Customer coefficients (K and B) can be set at 4 continuous MODBUS registers starting from
address 0x1100.
Start address Number of Register 1-2 Register 3-4 MODBUS function
registers code
0x1100 0x04 K B 0x10
Figure 31: Register definition of Set Customer Calibration Command
Below is an example of request and response frames for setting customer calibration coefficients,
13 / 18
Yantai Winmore Trade Co., Ltd.
14 / 18
Yantai Winmore Trade Co., Ltd.
15 / 18
Yantai Winmore Trade Co., Ltd.
16 / 18
Yantai Winmore Trade Co., Ltd.
01 00
Figure52: The definition of brush work mode
2.2.14 Get Brush work mode
Purpose:Get brush work mode.
The mode can be read from 1 MODBUS registers starting from address 0x1b00.
Start address Number of registers Register 1 MODBUS function code
0x1b00 0x01 brush work mode 0x03
Figure53: Register definition of Get brush work mode Command
Power On
Delay>=2s
More than 10 N
Delay 2S
times?
Start Measurement – sensor probe start emitting LED light, and perform COD
measurement with automatic calibration.
Get temperature and COD values – Get measurement results including temperature ( C)
and COD (mg/L) after 2 seconds of measurements.
Note: Please pay attention to highlighted portion (red) in the flow chart above. It’s highly
17 / 18
Yantai Winmore Trade Co., Ltd.
18 / 18