Difference between revisions of "DIN Rail BUS Protocol"

From InCircuit
Jump to: navigation, search
(Added example)
(Railbus Protocol binary extension: fixed CHK<S>)
Line 134: Line 134:
 
!style="width: 2em"| SE
 
!style="width: 2em"| SE
 
!style="width: 10em"| DATA  
 
!style="width: 10em"| DATA  
!style="width: 4em"| CHKS
+
!style="width: 4em"| CHK
 
|}
 
|}
  
=== MW, ADDR and CHKS ===
+
=== MW, ADDR and CHK ===
  
the Magic Word and the destination address are encoded like in the railbus protocol.
+
the Magic Word, the destination address and the checksum are encoded like in the railbus protocol.
  
 
=== CM - Command ===
 
=== CM - Command ===
Line 171: Line 171:
  
 
the rest of the packet consists of unencoded databytes.
 
the rest of the packet consists of unencoded databytes.
 
  
 
== Examples ==
 
== Examples ==

Revision as of 15:13, 9 October 2013

Template:Under construction

Contents

Railbus Protocol

The Railbus Protocol is designed to be as flexible as possible while being as easy as possible.

All data is transmitted on the RS485 with a baudrate of 9600Baud; 8N1 as printable ASCII characters. Any data is converted into the corresponding hexadecimal notation.

Packet Layout

Every packet has a fixed length and is created as defined here:

MW ADDR CM SADR DATA CHKS CRNL


MW - Magic word

The Magic word is prepended to every package and always consists of the two characters 'R' and 'P'.

ADDR - Modul address

Each Modul on the bus has a unique address. If a Modul is in its delivery-state, it is set to address 0. On Modules which are equipped with a BCD-Display, the modules address is shown here.

In every packet the address of the requested board is transmitted. This means while in Request-packets the address of the destination, in acknowledge packets the address of the source is transmitted.

The module address is one byte long; it is transmitted as two characters.

CM - command

the command consists of one byte, either an 'R', 'W', 'K', 'I' or 'N'. The value is not converted to hexadecimal notation. Any possible character is not possible to show up in the rest of the package. This way, a definitive start of packet is generated.

The different Words define the mode of the package, as shown in the following table:


'R' Read Request. Asks the addressed device to send its register-content.
'W' Write Request. Asks the addressed device to write the data into its register.
'K' Read Acknowledge. Acknowledges a preceding Read Requests and transmits the corresponding data.
'I' Write Acknowledge. Acknowledges a preceding Write Request and the transferred data to be written.
'N' Not Acknowledge. Is sent, if a read or write-request to an unknown or forbidden register was made.


SADR - Sub address or register address

The Sub address is interpreted device specific. Only the Sub-address 255 is defined by the stack and used for identifying a device and set a Modul address.

Every address can be read and written. Not implemented addresses should respond by an Not Acknowledge.

SADR255 - Module identification

Layout :

31 - 8 7 - 0
ID ADDR R
============== W

Reading the register will transmit the devices ID and its current address. On writing the address the 24 higher bits are set and only the address will be set to a new value.

The IDs can be like "R66" for the Din Rail Relais 6I6S.

DATA - the register data

This field is only transmitted in the 'K' and 'W' modes. It consists of 32bit hexadecimal notated data and is transmitted in eight characters.

CHK - Checksum

The Checksum consists of one byte of data, transmitted as two hexadecimal notated characters. If the MSB is set, the checksumming is enabled, using CRC7 on all of the preceeding data. If the MSB is not set, checksumming is disabled.

Railbus Protocol binary extension

the binary extension to the railbus protocol is for automated transmissions of data only.

Packet Layout

Packets using the binary extension do have a variable length and have the following layout:

MW ADDR CM LENGTH SRCA SE DATA CHK

MW, ADDR and CHK

the Magic Word, the destination address and the checksum are encoded like in the railbus protocol.

CM - Command

The command for the binary extension always has the highest bit set to flag the variable packet length.

The defined commands are implementation specific. The command is transmitted as one character.

LENGTH - packet length

The length field specifies the number of character transmitted for the complete packet. It is a 16bit integer transmitted in 4 hex-coded characters.

SRCA - source address

The source address is defined as the address of the sender. Like the ADDR-field it is transmitted in two characters.

SE - sequence number

An ongoing sequence number. Every packet in an ongoing stream has this field incremented to provide a unique packet identification. The number of the first packet in a stream should be used on a random base. The sequence number is represented as a 8bit integer, transmitted unencoded in one character.

DATA - variable length data

the rest of the packet consists of unencoded databytes.

Examples

Request for current Status on a DIN Rail Relais 6I6S

For reading the current status on a Relais 6I6S, the first line of the following listing should be send; the board will answer with the second line.

 RPFFR0000
 RPFFK000015002A00
  1. Every packet starts with the magic characters R and P.
  2. The next two characters contain the hexadecimal encoded address of the board. In this case, the board listens at the address 0xFF (or 255 in decimal notation).
  3. Following the address, the command is transmitted
    • In case of the Read Request, an R is transmitted
    • In case of the Read Acknowledge - the answer of the board - an K is transmitted
  4. Next the SADR is put on the line. In this example the register 0 is accessed.
  5. Only in the Read Acknowledge, 8 characters forming 4 Bytes of data are transmitted. In this case the state of the inputs and outputs is transmitted.
    • In the second Byte of the data, representing bit 23-16 of the register, the state of the inputs is shown. Here DIGIN1, DIGIN3 and DIGIN5 have a voltage detected.
    • In the forth Byte, representing bit 7-0 of the register, the state of the outputs is shown. Here REL2, REL4 and REL6 are on.
  6. The last two characters contain the checksum. In this case, the dummy-checksum '00' is used. This means no validity checking is performed.

Adding a valid checksum

The checksum is calculated as CRC7, like used in communication in MMC or SD-Cards. The theory is described in Wikipedia:Cyclic redundancy check. This CRC7 is or'ed with a 0x80, to have the highest bit set and enable the checksum-checking.

To calculate the checksum, the function crc7_calc in the following code can be used:

const unsigned char crc7_table[16] = {
	0x00, 0x12, 0x24, 0x36, 0x48, 0x5a, 0x6c, 0x7e,
	0x90, 0x82, 0xb4, 0xa6, 0xd8, 0xca, 0xfc, 0xee
};

static unsigned char crc7_next(unsigned char crc, unsigned char dat)
{
	crc ^= dat;
	crc = ((crc << 4) & 0xff) ^ crc7_table[crc >> 4];
	crc = ((crc << 4) & 0xff) ^ crc7_table[crc >> 4];
	return crc;
}

unsigned char crc7_calc(const unsigned char *dat, unsigned short len)
{
	unsigned char crc = 0;

	if (len) do {
		crc = crc7_next(crc, *dat++);
	} while (--len);

	return (crc >> 1)|(1<<7);
}
Personal tools