One bundle project – Serial connection over RS-485 using Eclipse Kura

RS-485 standard (TIA/EIA-485) specifies a communication interface that uses differential signal lines to establish communication between devices. Majority of application consists of half-duplex transmission mode with a twisted pair copper wire as a physical medium. Each device turns its line driver only when transmitting. During remaining time the driver is kept turned off in order to allow other devices on network to operate by avoiding the data collision. It is the responsibility of the application-layer protocol to supervise data being exchanged.

In order to connect RS-485 to Raspberry Pi or BeagelBone Black we need to use appropriate HW converter. There are adapters that handle direction control automatically and ones that need external control. I’m using RS485 Board (3.3V version) from Wavesare.

Below is an example that that utilizes RS-485 Transceiver (like SP3485 or MAX3485). Converter that I’m using has RSE pin that control internal line drivers (it is connected to  DE and RE pins of SP3485). More information can be found in the datasheet.

Example code uses RSE line in order to turn on/off transmission driver. It is based on Eclipse Kura Serial Example and Device I/O example (from Kura Documentation and JavaDoc). I’m using Bndtools to develop OSGi bundles under Eclipse, that’s why the Bnd annotations are visible in the code.

Later, I will write post about how to use Bndtools to create bundles for Eclipse Kura.

Below is simple example that sets direction to transmit and sends some string to uart. You can implement reading using InputStream and direction pin.

package io.github.tobidi.obp.kura.comm.serialRS485;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jdk.dio.DeviceConfig;
import jdk.dio.DeviceManager;
import jdk.dio.gpio.GPIOPin;
import jdk.dio.gpio.GPIOPinConfig;

import org.eclipse.kura.comm.CommConnection;
import org.eclipse.kura.comm.CommURI;
import org.osgi.service.io.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import aQute.bnd.annotation.component.Activate;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Reference;

@Component
public class SerialRS485Example {

	private static final Logger log = LoggerFactory
			.getLogger(SerialRS485Example.class);

	ConnectionFactory connectionFactory;

	private CommConnection connOne;

	private InputStream isOne;

	private OutputStream osOne;

	@Reference
	public void setConnectionFactory(ConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}

	public void unsetConnectionFactory(ConnectionFactory connectionFactory) {
		this.connectionFactory = null;
	}

	@Activate
	public void activate() {

		GPIOPinConfig pinConfig = new GPIOPinConfig(DeviceConfig.DEFAULT, 22,
				GPIOPinConfig.DIR_OUTPUT_ONLY, -1, GPIOPinConfig.TRIGGER_NONE,
				true);

		GPIOPin directionPin = null;

		try {
			directionPin = (GPIOPin) DeviceManager.open(GPIOPin.class,
					pinConfig);

		} catch (IOException e1) {
			log.error(e1.getMessage(), e1);
			return;
		}

		if (connectionFactory != null) {

			try {
				String uri = new CommURI.Builder("/dev/ttyAMA0")
						.withBaudRate(19200).withDataBits(CommURI.DATABITS_8)
						.withStopBits(CommURI.STOPBITS_1)
						.withParity(CommURI.PARITY_NONE).withTimeout(2000)
						.withFlowControl(CommURI.FLOWCONTROL_NONE).build()
						.toString();

				directionPin.setValue(false);
				connOne = (CommConnection) connectionFactory.createConnection(
						uri, 1, false);
				directionPin.setValue(true);

				isOne = connOne.openInputStream();
				osOne = connOne.openOutputStream();

				byte[] array = "ping\n\r".getBytes();

				osOne.write(array);
				osOne.flush();

				isOne.close();
				osOne.close();
				isOne = null;
				osOne = null;

				connOne.close();
				connOne = null;

				directionPin.close();

			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		} else {
			log.warn("No ConnectionFactory");
		}

	}

}
Advertisements

One thought on “One bundle project – Serial connection over RS-485 using Eclipse Kura

  1. “Later, I will write post about how to use Bndtools to create bundles for Eclipse Kura.” – yes please! in lue of a blog, some tips would be useful!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s