~ 32 ~
UBZ-304
NOVATEK-ELECTRO
Request
Address Function Sub-function HB
Sub-function LB
Data HB
Data LB CRC LB
CRC HB
01h
08h
00h
00h
A0h
3Ch
98h
1Ah
Response
Address Function Sub-function HB
Sub-function LB
Data HB
Data LB CRC LB
CRC HB
01h
08h
00h
00h
A0h
3Ch
98h
1Ah
Figure 5.13
–
Example of request and response for sub-function 00h
–
return of request data
Sub-function 01h
–
restart of communication options
During fulfillment of the command UBZ performs only change in baud rate. To change totally the communication
settings you should run the command "UBZ RESTART" ("RESTART").
Example of request and response for MODBUS
RTU
mode is shown in Fig.5.14.
Request
Address Function Sub-function HB
Sub-function LB
Data HB
Data LB CRC LB
CRC HB
01h
08h
00h
01h
00h
00h
B1h
CBh
Response is not returned
Figure 5.14
–
Example of request and response for sub-function 01h
–
restart of communication options
5.4.5 Control of correct transmission of data packet
5.4.5.1 Control of correct transmission of data packet in
RTU
mode
To check the correctness of data transmission in
RTU
mode the CRC Cyclic Redundant Check
–
the code for
cyclic control is used.
The Cyclic Redundant Check (CRC16) is a cyclic verification code based on the polynomial A001h. The
transmitting device forms the Cyclic Redundant Check for all bytes of the message transmitted. The receiving
device similarly generates the Cyclic Redundant Check for all bytes of the message received and compares it with
the Cyclic Redundant Check received from the transmitting device. In case of mismatching the generated and
received Cyclic Redundant Check the error message will be generated.
The field of Cyclic Redundant Check has two bytes. CRC in the message is transferred with low byte first.
CRC is formed by the following algorithm:
1) loading CRC register (16 bit) by units (FFFFh);
2) exclusive OR with the first 8 bits of the byte of the message and CRC register contents;
3) shift of the result one bit to the right;
4) if shifted bit = 1, exclusive OR of the contents of the register with value A001h;
5) if shifted bit = 0, repeat step 3;
6) repeat steps 3, 4, 5, until 8 shifts;
7) exclusive OR with the following 8 bits byte of the message and CRC register contents;
8) repeat steps 3 to 7 until all bytes of the message will be processed;
9) the final contents of the register will contain the CRC.
Example of Program CRC code generation in the language C. the Function has two arguments:
Unsigned char* data <- a pointer to the message buffer
Unsigned char length <- the quantity of bytes in the message buffer
The function returns the CRC value as a type of unsigned integer.
Unsigned int crc_chk(unsigned char* data, unsigned char length)
{int j;
unsigned int reg_crc=0xFFFF;
while(length--)
{
reg_crc ^= *data++;
for(j=0;j<8;j++)
{
if(reg_crc & 0х01) reg_crc=(reg_crc>>1) ^ 0xA0
01; // LSB(b0)=1
else reg_crc=reg_crc>>1;
}
}
return reg_crc;
}
5.4.5.2
Control of correct transmission of data packet in ASCII mode
To check the correctness of data transfer in
ASCII
mode LRC Redundant Check
–
longitudinal redundancy
check. CRC is 8
–
bit number transmitted as two ASCII characters. The CRC is formed by inverse transformation of
all ASCII characters in eight-bit binary numbers, the addition of these numbers without accounting for the transfer,