Java library for ODS sensors

DSE ODS Sensor Library

Java library for ODS sensors

We have recently made a Java library available for easy communication with our ODS line of non-contact optical displacement sensors. The library is tested on Windows, Mac OS-X, Linux (intel/amd processors and ARM processor on Raspberry Pi).

It is easy to use the library and we have provided short usage information and a small demonstration app.

/*
 * Copyright 2020 Danish Sensor Engineering ApS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dse;

import biz.nellemann.libsensor.SerialSensor;
import biz.nellemann.libsensor.TelegramListener;
import biz.nellemann.libsensor.TelegramResultEvent;
import biz.nellemann.libsensor.TelegramErrorEvent;
import biz.nellemann.libsensor.TelegramHandler16Bit;


public class DemoApp implements TelegramListener {

    final SerialSensor serialSensor;

    public static void main(final String args[]) {

        // Print available ports
        if(args.length < 1) {
            SerialSensor.printSerialPorts();
            return;
        }

        // Pass 1st argument (port) into our constructor
        new DemoApp(args[0]);
    }


    // Initialize with serial port name as argument
    public DemoApp(String portName) {

        serialSensor = new SerialSensor();
        serialSensor.setTelegramHandler(new TelegramHandler16Bit());
        serialSensor.addEventListener(this);
        serialSensor.doAverageOver = 15;

        // Setup serial port, start listener and add observer (this Observer class)
        serialSensor.openPort(portName, 38400);

        // Keep running for some seconds
        try {
            Thread.sleep(5000L);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }

        // Stop listener and close port
        serialSensor.removeEventListener(this);
        serialSensor.closePort();

    }


    @Override
    public void onTelegramResultEvent(TelegramResultEvent event) {
        System.out.println("Result: " + event.getMeasurement());
    }

    @Override
    public void onTelegramErrorEvent(TelegramErrorEvent event) {
        System.err.println("Error: " + event.toString());
    }

}

For more information you are always welcome to contact us.