Java library for ODS sensors

DSE ODS Sensor Library

Java library for ODS sensors

We have a Java library that facilitates communication with our ODS line of non-contact optical displacement sensors. This library, which can be found at https://github.com/Danish-Sensor-Engineering/libods-java/, has been thoroughly tested on Windows, MacOS, Linux (x86/amd64 processors and ARM processors 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.demo;

import dse.libods.SerialSensor;
import dse.libods.TelegramListener;
import dse.libods.TelegramResultEvent;
import dse.libods.TelegramErrorEvent;
import dse.libods.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);


        // 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.

ByDSE