Wednesday, April 17, 2024

Java Serial Class that works with Numato Labs USB I/O device.

This product is interesting because it is relatively cheap and does not require special software or drivers.  If you have an old PC laying around, you can install Linux Mint on it for free and use this instead of spending the cash on a Raspberry Pi.  I did just that to solve a minor problem at our manufacturing facility.  We have a warning light that goes off in our clean room which indicates that the particle count is out of specification.  The problem was, the employees did not have any sort of timer telling them how long it has been active.  After wiring this in parallel to the warning light, I created a Java application that pops up a warning label and a timer.  After 3 minutes they must immediately shut down.  An alternative to Java would be Python and the tkinter library, which I initially tried.  I find it much easier to make nicer graphics in Java.  By the time you buy a Raspberry PI 5 and all the required accessories, you might as well find yourself a bare bones mini PC.





package alarm_monitor;

import com.fazecast.jSerialComm.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class mySerialPort {

    private SerialPort port;

    public mySerialPort() {
        connect();
    }
    
    public boolean connect() {
        SerialPort[] ports = SerialPort.getCommPorts();

        if (ports.length > 0) {
            for (SerialPort port1 : ports) {
                if (port1.getDescriptivePortName().equals("Numato Lab 8 Channel USB GPIO Module")) {
                    port = port1;
                }
            }
            if (port != null) {
                port.setBaudRate(19200);
                port.setNumDataBits(8);
                port.setNumStopBits(SerialPort.ONE_STOP_BIT);
                port.setParity(SerialPort.NO_PARITY);
                port.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
                port.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING, 250, 250);
                return true;
            }
        }
        return false;
    }

    public void close() {
        if (port != null) {
            port.closePort();
        }
    }

    public String getPortInfo() {
        StringBuilder sb = new StringBuilder();
        for (SerialPort myPorts : SerialPort.getCommPorts()) {
            sb.append(port.getSystemPortPath()).append(": ").append(myPorts.getDescriptivePortName()).append("\n");
        }
        return sb.toString();
    }

    public boolean isOpen() {
        if (port != null) {
            return port.isOpen();
        } else {
            return false;
        }
    }

    public int getAlarmStatus() {

        String dataToWrite;

        try {
            OutputStream outputStream;
            int[] intData;
            try (InputStream inputStream = port.getInputStream()) {
                outputStream = port.getOutputStream();
                port.openPort();
                dataToWrite = "gpio set 0\r";
                outputStream.write(dataToWrite.getBytes());
                outputStream.flush();
                Thread.sleep(200);
                byte[] inputBuffer = new byte[inputStream.available()];
                inputStream.read(inputBuffer);
                dataToWrite = "gpio read 1\r";
                outputStream.write(dataToWrite.getBytes());
                outputStream.flush();
                Thread.sleep(200);
                inputBuffer = new byte[inputStream.available()];
                int bytesRead = inputStream.read(inputBuffer);
                intData = new int[bytesRead];
                for (int i = 0; i < bytesRead; i++) {
                    intData[i] = inputBuffer[i] & 0xFF;
                }
            }
            outputStream.close();
            port.closePort();

            return intData[13] - 48;

        } catch (Exception ex) {
            Logger.getLogger(mySerialPort.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }