Serial protocol

Communication protocol for all our devices consists of two main commands: read memory byte and write memory byte. It could contain also some special commands which are described separately for every device. Protocol is based on 5-byte binary packets exchange (See Table below). The device sends 5-byte answer for every 5-byte command, if device address field and XOR-sum in this command is correct.
Command structure is following:

Byte Description
1 6-bit device address. Should be equal to device address set on the dip switch inside the device or packet will be ignored otherwise. Two most significant bits are ignored.
2 Bit 7 – read/write bit. Bit 7 = 1 corresponds to write command and bit 7 = 0 – read command.
Bit 6 – special command flag. If bit 6 = 1, packet contains special command.
Bits 5÷0 – high bits of 14-bits address field.
3 Low byte of 14-bits address field.
4 Data byte.
5 XOR-sum of first 4 bytes. B5=B1^B2^B3^B4. If it is not correct XOR-sum, packet is ignored.

This protocol makes it possible to represent any serial device as a array of bytes with transparent access to process variables. Once implemented, such a protocol could be used with any device of this type. The only thing user should know about it is controller memory map, which is of course different for every device, and device address. Byte ordering is little endian, high byte has low address. This requires all word (16-bit integer) and float variables to be swapped in the IBM compatible PC, that use big endian byte order.

Communication parameters are always 8-bit with 1 stop bit, no parity, no flow control. Baudrate could be selected from 9.6, 19.2, 57.6 or 115.2 Kbit/s by dip switch inside the device (Table 2). Usually read or write access to the device memory using this protocol takes not more than 2ms for every byte at 115.2 Kbit/s. Data format in this protocol allows one to access a number of devices making use the same continuous address space with 3-byte address.

Examples:
1. Host computer wants to read byte at address 0x345 from the device with address 0x02. Device memory contains 0xAA at this address.

host request  : 0x02 0x03 0x45 0x00 0x44
device answer : 0x02 0x03 0x45 0xAA 0xEE

2. Host computer wants to write byte 0x55 at address 0x1543 to the device with address 0x08 (Note that read/write bit is cleared in the answer).

host request  : 0x08 0x95 0x43 0x55 0x8B
device answer : 0x08 0x15 0x43 0x55 0x0B

Communication speed could be selected from 9.6, 19.2, 57.6 or 115.2 Kbit/s by dip switch inside the device (see table below). Usually read or write access to the device memory using this protocol takes not more than 2ms for every byte at 115.2 Kbit/s. Memory map is of course different for every device and is specially described as well as additional commands. Byte ordering is little endian, high byte has low address.

Switch position Description
1-6 6-bit device address in binary format. Least significant bit is selected by switch position 1. Valid range is 1÷63.
7-8 Communication speed:
00 – 9600 bit/s
01 – 19200 bit/s
10 – 57600 bit/s
11 – 115200 bit/s

Most of serial controllers accepts some special commands. The first one is “read all memory” command. For this command byte 2 of the serial packet should be 0x41 (0x40 sets bit 6, 1 is the command number). Bytes 3 and 4 define high and low bytes of maximum address correspondingly. In response to this command device sends all memory contents from address 0 to maximum address specified in the command as a continuous flow of bytes without prefixes and checksum. This command is fastest way to get all values from the device. For example, reading of all SCPS parameters byte by byte takes about 130 ms while using 0x41 command reduces this time to 40 ms.

Other two commands apply to all devices on the RS-485 bus without addressing. These are “shut up” and “wake up” commands. They are necessary if communication software needs access to serial device with different protocol, for instance, ASCII-based. In this case some ASCII commands could be recognized by our serial device and it will occupy the bus with the answer. Thus, all our devices should keep silence during data exchange with other equipment. Normal commands handling could be then restored by “wake up” command.

Shut up command : 0x02 0x03 0x45 0x00 0x44
Wake up command : 0x02 0x03 0x45 0x00 0x44

Devices don’t response to these commands. They just enable or disable normal communication protocol.

Tags: