RS-485 Protocol Manual Revision: 1.0 January 11, 2000
RS-485 Protocol Guidelines and Description Page i Table of Contents 1.0 COMMUNICATIONS BUS OVERVIEW... 1 2.0 DESIGN GUIDELINES... 1 2.1 Hardware Design Guidelines... 1 2.1.1 S+ and S-:... 1 2.1.2 LTC1487:... 1 2.1.3 Surge Protection:... 1 2.1.4 IC Socket Mounting:... 1 2.1.5 Transmit LED... 2 2.1.6 Termination Resistors:... 2 2.1.7 RS-485 Bias:... 2 2.1.8 Switch Layout:... 2 2.2 Software Design Guidelines... 3 2.2.1 Data Transmission Sequence:.... 3 2.2.2 Intercharacter Delay:... 3 2.2.3 Message Framing and Baud Rate:... 3 2.2.4 Bit Packing:... 3 3.0 USING RS-485 WITH MICROCONTROLLERS (PIC & 8051 SERIES)... 4 4.0 MESSAGE FRAME FORMAT... 5 4.1 Query (From Master)... 5 5.0 FUNCTION CODE DESCRIPTIONS... 5 5.1 Example Code 01: Read Valve Status... 5 5.2 Example Code 02: Synchronize Slave Clocks with Master Clock... 7 5.3 Example Code 03: Close Valve Command... 8 5.4 Example Code 04: General Purpose Data Poll (10 bytes)... 9 6.0 CRC GENERATION... 10 7.0 CRC PROGRAM CODE EXAMPLE (WRITTEN IN C)... 11
RS-485 Protocol Guidelines and Description Page 1 1.0 Communications Bus Overview The Eagle Systems RS-485 communications network has the following features: 2-wire Balanced (Differential) lines for improved noise immunity. Multi-dropped communications that will allow up to 32 nodes on the network (assuming 1 unit load per node the LTC1487 transceiver has.125 unit loads) The communications protocol is designed around the framework of the industry standard Modbus binary protocol. Protocol support for addresses 1-89 and broadcast addresses 90-99. 2.0 Design Guidelines To maintain compatibility with existing nodes on the RS-485 network, the following guidelines must be used. Any variation from the listed guidelines may cause unreliable or intermittent bus operation, compatibility problems with existing devices, or bus contention (more than one node transmitter is on at any given time).. Note: The words bus and network refer to the RS-485 multi-dropped communications link. The words CRC, CRC-16, and checksum refer to the 16 bit (2 bytes) Cyclic Redundancy Check field appended to each message frame. The words host and bus master refer to the RS-485 bus master. The words frame, message, host poll/slave response, and packet refer to the message frames supported by the Modbus Protocol. Hexadecimal numbers will always be shown with a 0x appended to the left side of the number. For example, the hex number 3f will be shown as 0x3f. 2.1 Hardware Design Guidelines 2.1.1 S+ and S-: The communication wires will be labeled as S+ and S- in all connection diagrams. The S+ wire is the non-inverting input to the RS-485 transceiver (usually labeled as A pin 6), and the S- wire is the inverting input to the RS-485 transceiver (usually labeled as B pin 7). There is an exception to this rule: Refer to the diagram 3.0 Using RS-485 with MicroControllers (PIC & 8051 Series for an example. During bus quiet times, the S- wire will be more positive than the S+ wire when referenced to the circuit board ground. 2.1.2 LTC1487: The Linear Technologies LTC1487 RS-485 transceiver should be selected for the node interface. The Linear Technologies transceivers have proven to be the most reliable among the available manufacturers. The LTC1487 has a high input impedance and will impose only.125 unit load on the network. 2.1.3 Surge Protection: There are a variety of sources for voltage transients that can be imposed on the communication network (lightning strikes near/on the building, close proximity to high current motor control leads, etc.). To provide a measure of protection from possibly high voltage transients, some type of surge protection should be designed in the transceiver circuit of each node. (An 18 volt MOV will work very well). The MOV s should be connected between the two wires, and from each wire to circuit board ground. 2.1.4 IC Socket Mounting: The RS-485 transceiver should be the thru-hole 8 pin dip package, plugged into a socket of the node circuit board. The socket will allow for a quick field repair in the event of failure of the
RS-485 Protocol Guidelines and Description Page 2 RS-485 transceiver. Experience has shown that the transceivers are the highest risk component in circuit board design. 2.1.5 Transmit LED All node devices must have a transmit LED to allow visual confirmation of bus activity. Typically, all the nodes are using a red led for the transmit indication. 2.1.6 Termination Resistors: Termination or Bias Resistors will not be needed on the node devices acting as slaves to the host. If provisions are made for termination or bias resistors in the circuit board design, the components should shipped from the factory in the disabled or not connected position. 2.1.7 RS-485 Bias: The bus master on the RS-485 data bus will impose a bias. The bias will keep the S- side more positive than the S+ side of the communications link (when referenced to circuit board ground). The bias is required to prevent false triggering of the node receivers during bus quiet times. 2.1.8 Switch Layout: The Switch positions are assigned binary weights to each switch position. For example, a 4 position switch will allow selection of addresses 1-15. If your base address assignment begins at 32, a programmed offset can be added in your software to the address switch value (which will allow a small address switch to be used, or just a few positions). For example, if the switch value is 5, and your programmed offset is 32, the slave node address will be 32+5=37. Refer to the diagram below for switch layout recommendations. The address switch can be a small array of shorting block jumpers, a stand-alone dip switch, or a few switch positions of a large dip switch. The binary weighting must be right to left, with the right most position as the least significant bit. Switch Position Example Binary Weights 8 4 2 1 ON Example : 8 + 0 + 0 + 1 = 9 OFF Note: Always use the ON position of the dip switch to select the address. ON = 1 OFF = 0 OFF = 0 ON = 8
RS-485 Protocol Guidelines and Description Page 3 2.2 Software Design Guidelines 2.2.1 Data Transmission Sequence:. To eliminate the possibility of bus contention (more than one transmitter enabled at a time on the bus), the following communications sequence should be used: Enable Driver Delay 3 Character Times Send Entire Message Frame Delay 1 Character Time Disable Driver 2.2.2 Intercharacter Delay: All response data should be sent in one continuous block (without any intercharacter delays during transmission to the host). An intercharacter delay of greater than 1 character time may be interpreted by the host as a message fragment (causing the host to flush the receive buffer and attempt a message retry). 2.2.3 Message Framing and Baud Rate: The Eagle Systems RS-485 data bus is operating at 9600 baud, 8 data bits, no parity bit, and 1 stop bit. 2.2.4 Bit Packing: Always use bit packing, if applicable, in all responses to a host poll. The binary version of the Modbus Protocol will allow for byte values of 0x00 to 0xff. For example, to report the status of 10 switch inputs to the host system, 2 bytes (16 bits) would be required. Since only 10 bits are needed, there will be 6 bits listed as spare. Data Transmission BYTE 1 BYTE 2 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 Spare Switch 3-7 Switch 1 status Switch 2 status Switch 8 status Switch 9 status Switch 10 status
RS-485 Protocol Guidelines and Description Page 4 3.0 Using RS-485 with MicroControllers (PIC & 8051 series) Refer to the diagram below: RS-485 transceivers normally require an inverter between the CPU RECV pin and the RECV pin of the transceiver. The RECV pin of the CPU is expecting a Marking condition on the receiver to represent bus quiet times. The RS-485 transceiver provides a Spacing condition during bus quiet times. Reversing the S+ and S- signal connections from the field RS-485 bus can eliminate the inverter. The RS-485 transceiver is a differential input/output device and does not know (or care) which direction the differential signal is supposed to swing. By reversing the inputs, the inverter is eliminated but, the designer must take care to label the S+ and S- pins properly on the card edge (where the field wires connect). Refer to the circuit example below:
RS-485 Protocol Guidelines and Description Page 5 4.0 Message Frame Format The host will always initiate the data transfer (Master/Slave arrangement). The host message frame has a fixed length of 4 bytes when individual slave nodes are addressed. If a broadcast address is used, the host message frame will vary in length. 4.1 Query (From Master) 0 1 2 3 Slave Code CRC1 CRC2 (varies) (varies) (varies) (varies) Slave A unique number assigned to each slave node on the network. es >= 90 represent Broadcast address and will be received by all node groups configured for the broadcast address. Adr 0 Adr 1-89 Adr 90-99 Reserved for diagnostic use. Normal Device es. Reserved for broadcast addresses. Codes: In a host query, this field identifies the function the target node is to perform.: 01-255 Normal Device Codes CRC1, CRC2 16 bit Cyclic Redundancy Check Checksum used to detect errors in the received data Frame. 5.0 Code Descriptions The host will always initiate the data transfer. The host data packet has a fixed length of 4 bytes (refer to section 4.1 for a detailed description of the polling message). The polling message will always begin with the. Refer to the description of each function code in the following sections. 5.1 Example Code 01: Read Valve Status Assignment: Eagle Systems This function code will cause the Slave board to send the current status of valves 1-14. Query 0 1 2 3 Code CRC1 CRC2 (varies) 01 (varies) (varies) Response 0 1 2 3 4 5 Code Status of Valves 1-8 Status of Valves 9-14 CRC1 CRC2 (varies) 01 (varies) (varies) (varies) (varies)
RS-485 Protocol Guidelines and Description Page 6 : Echo of the node slave address (host will verify the responding address matches the query address). Code: 01 Read Valve Status Valves 1-14 of the slave node. Status of Valve1-8: Byte 2 Bit 0: Valve Status on = Valve open. Bit 1: Valve Status on = Valve open. Bit 2: Valve3 Status on = Valve open. Bit 3: Valve4 Status on = Valve open. Bit 4: Valve5 Status on = Valve open. Bit 5: Valve6 Status on = Valve open. Bit 6: Valve7 Status on = Valve open. Bit 7: Valve8 Status on = Valve open. Status of Valve9-14: Byte 3 Bit 0: Valve9 Status on = Valve open. Bit 1: Valve10 Status on = Valve open. Bit 2: Valve11 Status on = Valve open. Bit 3: Valve12 Status on = Valve open. Bit 4: Valve13 Status on = Valve open. Bit 5: Valve14 Status on = Valve open. Bit 6: (not used) Bit 7: (not used) CRC1, CRC2 16 bit Cyclic Redundancy Check Checksum used to detect errors in the received data Frame.
RS-485 Protocol Guidelines and Description Page 7 5.2 Example Code 02: Synchronize Slave Clocks with Master Clock Assignment: Eagle Systems This function code will cause the slave board(s) to reset their on-board clocks to the time sent from the Master. This will keep the same timebase across the entire system. Note that address 99 is used by the system as a broadcast address. Broadcast address messages will not require a response from any slave board. Query 0 1 2 3 4 Code Seconds Minutes Hours 99 02 (varies) (varies) (varies) 5 6 7 8 9 Day Month Year CRC1 CRC2 99 02 (varies) (varies) Response (No response to a broadcast address)
RS-485 Protocol Guidelines and Description Page 8 5.3 Example Code 03: Close Valve Command Assignment: Eagle Systems This function code will cause the Slave board to send out a command to CLOSE a valve. The valve number is located in the byte following the function code. Query 0 1 2 3 4 Code Valve Number CRC1 CRC2 (varies) 03 (varies) (varies) (varies) Response 0 1 2 3 4 Code Status Byte CRC1 CRC2 (varies) 03 (varies) (varies) (varies) : Echo of the node slave address (host will verify the responding address matches the query address). Code: 03 Send CLOSE command to Valve. Status Byte: 01 Command received and executed normally 02 Invalid Command 03 Error at device 04-255 Available for other status values
RS-485 Protocol Guidelines and Description Page 9 5.4 Example Code 04: General Purpose Data Poll (10 bytes) Assignment: Eagle Systems This function code will retrieve 10 bytes of data from the polled slave. This is a general-purpose poll used to retrieve larger data blocks from the slave device. The response from the slave is always 10 bytes of data, with unused bytes set to zero. Query 0 1 2 3 Code CRC1 CRC2 (varies) 04 (varies) (varies) Response 0 1 2 3 4 Code Data 0 Data 1 Data 2 (varies) 04 (varies) (varies) (varies) 5 6 7 8 9 Data 3 Data 4 Data 5 Data 6 Data 7 (varies) (varies) (varies) (varies) (varies) 10 11 12 13 Data 8 Data 9 CRC1 CRC2 (varies) (varies) (varies) (varies)
RS-485 Protocol Guidelines and Description Page 10 6.0 CRC Generation A multi-byte CRC error checking protocol is used on all data transmissions between the Eagle Systems slave devices and the host system. The multi-byte protocol provides a virtually error free communication packet because of the CRC-16 two byte checksum appended to each message. The sending system will calculate a CRC and append it to the message. The receiving system will calculate a new CRC based on the entire message including the appended CRC bytes. The resulting CRC should be 0x0000. If the CRC calculated by the receiving system is not equal to zero, then an error occurred in the transmission and all data should be ignored. CRC-16 16 bit CCITT polynomial used: χ16 + χ15 +χ2 +χ1 CRC is good at detecting all kinds of errors, especially those that occur in bursts over a long period of time. Refer to the chart below for the CRC-16 error checking accuracy 1. CRC-16 Error Checking Accuracy Single Bit Errors: 100 percent Double-Bit Errors: 100 percent Odd-Numbered Errors: 100 percent Burst Errors Shorter than 16 bits: 100 percent Burst Errors of exactly 17 bits: 99.9969 percent All other burst errors: 99.9984 percent 1 Error Checking Accuracy Table values taken from Tanenbaum, Andres, S., Computer Networks, Prentice-Hall, 1981.
RS-485 Protocol Guidelines and Description Page 11 7.0 CRC Program Code Example (Written in C) /*>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTION: crc_calc() <<<<<<<<<<<<<<<<<<<<<<<< */ /* Purpose: Generate the CRC checksum s used by the Modbus Protocol. */ /* Notes: This routine will simulate a reverse CRC Hardware circuit. */ /* (Used in the Modbus Protocol). */ /* This function uses the CRC-16 16 bit CCITT polynomial. */ /* CRC Polynomial Used: x16+x15+x2+x1 */ /* Entry: The calling routine must pass an unsigned char (byte) value */ /* to perform the CRC calculations on, and a pointer to an */ /* unsigned integer location for the storage of the generated */ /* CRC word (2 bytes). It is the calling functions */ /* responsibility to initialize the crc_accum location to 0xffff*/ /* prior to calling this routine for the first time. Only init */ /* the location after the sequence of characters used in the */ /* CRC generation are complete. */ /* Example: If the character string is: 06 03 0b b9 00 01 */ /* The calculated CRC will be: 7c 56 */ /* (Note: Swap bytes for serial transmission - the */ /* complete transmission sequence will be: */ /* 06 03 0b b9 00 01 56 7c */ /* <-------- +---+ */ /* Data Flow CRC */ /* Exit: Location crc_accum will have a 16 bit (two bytes) value of */ /* the new calculated CRC. */ /* Programmer: Rick Vaughn Eagle Systems, Santa Fe, TX 77510 */ /* Revisions: 8/26/94 (Orig) ---Rick Vaughn--- */ /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */ void crc_calc(uchar work_data) { code uint genpoly = 0xa001; /* Reversed polynomial */ uchar i; /* Convert the received byte to an integer */ crc_accum = crc_accum ^ (uint)work_data; for(i=8; i>0; i--) { /* Loop 8 times to test each bit of the new character */ } if ((crc_accum) & 0x0001) crc_accum = ((crc_accum) >> 1) ^ genpoly; else (crc_accum) >>= 1; } file: eagle485.doc