Connecting Serial Devices (Compact)

With the RS485 connection you can integrate serial devices such as a sensor into your system at low cost.

The data lines of the RS485 socket are marked P (positive) and N (negative). On other devices, these lines are often referred to as D+ and D- or A and B.

Wiring of the RS485 terminal:

For the actual data transmission only the lines N and P are required. We recommend that you use a twisted pair for longer line lengths or higher bit rates.

If a reference potential should be necessary, you can use the electrical circuit ground at terminal “┴” for this purpose. However, you should only make this connection if it is really necessary. It is best to connect the cable shielding to the FE terminal.

Cables with a length >30m or cables leaving the building must always be shielded. To improve the EMC properties of the shield, connect the shield to the grounded mounting plate over a large area using a conductive cable clamp. Alternatively, you can use a conductive EMC cable gland to lead the cable through the control cabinet wall.

From Linux the interface is addressed via the character device “/dev/ttyRS485”. You can configure bit rates up to 3,000,000. However, occasional reception errors may occur at more than 230,400 Bit/s. This is because the UART of the Raspberry Pi to which the interface is connected has only a 16 byte FIFO and does not support DMA. The higher the bit rate, the more often it happens that the FIFO is not read out fast enough and received data is lost. For example, at 460,800 Bit/s there are 1-2 errors per 50 MByte received, at 921,600 Bit/s there are about 10 errors. If your RevPi Compact mainly sends data and only rarely receives it, you can even use higher bit rates without hesitation, but otherwise we recommend that you do not set more than 230,400 Bit/s.

The interface has an integrated terminating resistor with 120 Ohm. This termination is disabled after booting. This is the safest way, because if several devices on the RS485 bus switch on their termination, no communication is possible at all. Via short cables, communication is also possible without termination. For longer distances you should switch it on if your RevPi Compact is at the end of the bus.

Switch the termination on and off as follows:

  1. Open the character device “/dev/ttyRS485” with open().
  2. Read out the current RS485 configuration with a TIOCGRS485 ioctl()
  3. Set or delete the SER_RS485_TERMINATE_BUS flag in the “struct serial_rs485”.
  4. Activate the changed RS485 configuration with a TIOCSRS485 ioctl().
  5. Close the character device with close().

You have to repeat these steps after each boot if you want to enable termination. We have prepared a C-listing with these steps that you only need to compile:

#include <errnoh>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/serial.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
	struct serial_rs485 rs485conf;
	int fd, ret;

	if (argc != 2) {
		printf("Usage: %s <device>\n", argv[0]);
		return 0;
	}

	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		printf("Cannot open: %s\n", strerror(errno));
		return errno;
	}

	ret = ioctl(fd, TIOCGRS485, &rs485conf);
	if (ret < 0) {
		printf("Cannot get rs485 config: %s\n", strerror(errno));
		return errno;
	}

	rs485conf.flags ^= SER_RS485_TERMINATE_BUS;

	ret = ioctl(fd, TIOCSRS485, &rs485conf);
	if (ret < 0) {
		printf("Cannot set rs485 config: %s\n", strerror(errno));
		return errno;
	}

	ret = close(fd);
	if (ret < 0) {
		printf("Cannot close: %s\n", strerror(errno));
		return errno;
	}
}