| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | MODBUS Protocol | |||||||||||||||||||||||||
2 | ||||||||||||||||||||||||||
3 | ||||||||||||||||||||||||||
4 | 1. Introduction | |||||||||||||||||||||||||
5 | MODBUS MCCB Protocol | |||||||||||||||||||||||||
6 | ||||||||||||||||||||||||||
7 | ||||||||||||||||||||||||||
8 | 2. Physical Layer | |||||||||||||||||||||||||
9 | ⊙ Communication port : RS485 | |||||||||||||||||||||||||
10 | ⊙ Asynchronous format : Each character is composed of 10 bit. | |||||||||||||||||||||||||
11 | (1 start bit + 8 data bits + 1 stop bit) | |||||||||||||||||||||||||
12 | ⊙ Baud rate : 19200 bps | |||||||||||||||||||||||||
13 | ⊙ Data Bits : 8 bits | |||||||||||||||||||||||||
14 | ⊙ Parity : No parity | |||||||||||||||||||||||||
15 | ⊙ Stop Bits : 1 bits | |||||||||||||||||||||||||
16 | ⊙ Master-Slave method: Only master is qualified for requesting, | |||||||||||||||||||||||||
17 | while slave delivers the requested data and response to the required performance according to inquiry. | |||||||||||||||||||||||||
18 | ||||||||||||||||||||||||||
19 | ||||||||||||||||||||||||||
20 | ||||||||||||||||||||||||||
21 | 3. Data Link Layer | |||||||||||||||||||||||||
22 | ⊙ if master delivers the request frame, slave does the response frame. | |||||||||||||||||||||||||
23 | Each frame is differenciated by dead time. | |||||||||||||||||||||||||
24 | General form to send and receive frames is as follows: | |||||||||||||||||||||||||
25 | ||||||||||||||||||||||||||
26 | DESCRIPTION | SIZE | ||||||||||||||||||||||||
27 | SLAVE ADDRESS | 1 byte | ||||||||||||||||||||||||
28 | FUNCTION CODE | 1 byte | ||||||||||||||||||||||||
29 | DATA | N byte | ||||||||||||||||||||||||
30 | CRC | 2 byte | ||||||||||||||||||||||||
31 | DEAD TIME | 3.5 bytes transmission time | ||||||||||||||||||||||||
32 | ||||||||||||||||||||||||||
33 | Master | Request | Response | Slave | ||||||||||||||||||||||
34 | Slave address | Device Address | Device Address | Self-address | ||||||||||||||||||||||
35 | Definition of slave action | Function Code | Function Code | Echo or MSB=1 | ||||||||||||||||||||||
36 | Additional information to perform the requested action | Data | Data | Requested data or exception code | ||||||||||||||||||||||
37 | ||||||||||||||||||||||||||
38 | ||||||||||||||||||||||||||
39 | CRC | Error check | Error check | CRC | ||||||||||||||||||||||
40 | ||||||||||||||||||||||||||
41 | ⊙ SLAVE ADDRESS | |||||||||||||||||||||||||
42 | Valid slave device address range : 0~247 decimal | |||||||||||||||||||||||||
43 | Slave device address range in practice : 1~32 decimal | |||||||||||||||||||||||||
44 | If the range of slave device address of the requested frame by master is 0, it means that | |||||||||||||||||||||||||
45 | master device bradcasts to all slaves. | |||||||||||||||||||||||||
46 | In case that master requests to slave, fill the relative slave address in address field and send it. | |||||||||||||||||||||||||
47 | If slave responses to master, fill its own adress in address field and send it. | |||||||||||||||||||||||||
48 | ||||||||||||||||||||||||||
49 | ⊙ FUNCTION CODE | |||||||||||||||||||||||||
50 | Valid range : 1~255 | |||||||||||||||||||||||||
51 | normal : 1~127, error : 129 ~ 255(normal + 0x80) | |||||||||||||||||||||||||
52 | It defines slave's requested action by master. | |||||||||||||||||||||||||
53 | Slave enters the following. | |||||||||||||||||||||||||
54 | In case of normal response: Echo the function code requested, as it stands. | |||||||||||||||||||||||||
55 | In case of exceptjonal response: Set to 1 as MSB of the function code requested and fill it in. | |||||||||||||||||||||||||
56 | ||||||||||||||||||||||||||
57 | ⊙ DATA | |||||||||||||||||||||||||
58 | Register address | |||||||||||||||||||||||||
59 | Amount of items to be handled | |||||||||||||||||||||||||
60 | The number of byte of actual data | |||||||||||||||||||||||||
61 | ||||||||||||||||||||||||||
62 | ⊙ CRC | |||||||||||||||||||||||||
63 | Error checking method. | |||||||||||||||||||||||||
64 | CRC-16 | |||||||||||||||||||||||||
65 | CRC Generation Function | |||||||||||||||||||||||||
66 | ||||||||||||||||||||||||||
67 | unsigned short CRC16(puchMsg, usDataLen) | |||||||||||||||||||||||||
68 | unsigned char *puchMsg ; /* message to calculate CRC upon */ | |||||||||||||||||||||||||
69 | unsigned short usDataLen ; /* quantity of bytes in message */ | |||||||||||||||||||||||||
70 | { | |||||||||||||||||||||||||
71 | unsigned char uchCRCHi = 0xFF ; /* high byte of CRC initialized */ | |||||||||||||||||||||||||
72 | unsigned char uchCRCLo = 0xFF ; /* low byte of CRC initialized */ | |||||||||||||||||||||||||
73 | unsigned uIndex ; /* will index into CRC lookup table */ | |||||||||||||||||||||||||
74 | ||||||||||||||||||||||||||
75 | while (usDataLen––) /* pass through message buffer */ | |||||||||||||||||||||||||
76 | { | |||||||||||||||||||||||||
77 | uIndex = uchCRCHi ^ *puchMsgg++ ; /* calculate the CRC */ | |||||||||||||||||||||||||
78 | uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex} ; | |||||||||||||||||||||||||
79 | uchCRCLo = auchCRCLo[uIndex] ; | |||||||||||||||||||||||||
80 | } | |||||||||||||||||||||||||
81 | ||||||||||||||||||||||||||
82 | return (uchCRCHi << 8 | uchCRCLo) ; | |||||||||||||||||||||||||
83 | } | |||||||||||||||||||||||||
84 | ||||||||||||||||||||||||||
85 | ||||||||||||||||||||||||||
86 | ⊙ DEAD TIME | |||||||||||||||||||||||||
87 | Frame is completed only when it has silent interval more than 3.5 character time after receiving the last character. | |||||||||||||||||||||||||
88 | 1 character time is approximately 0.5ms since BPS is 19200 | |||||||||||||||||||||||||
89 | (700ms to1000ms is recommended for the interval of master's request.) | |||||||||||||||||||||||||
90 | ||||||||||||||||||||||||||
91 | 4. MODBUS Exception Codes | |||||||||||||||||||||||||
92 | ||||||||||||||||||||||||||
93 | code | Name | ||||||||||||||||||||||||
94 | 0x01 | ILLEGAL FUNCTION | ||||||||||||||||||||||||
95 | 0x02 | ILLEGAL DATA ADDRESS | ||||||||||||||||||||||||
96 | 0x03 | ILLEGAL DATA VALUE | ||||||||||||||||||||||||
97 | 0x04 | SLAVE DEVICE FAILURE | ||||||||||||||||||||||||
98 | 0x10 | No data for event/fault record | ||||||||||||||||||||||||
99 | 0x11 | SBO TIME OUT | ||||||||||||||||||||||||
100 | 0x12 | ILLEGAL ADU LENGTH | ||||||||||||||||||||||||