×

INDI Library v2.0.7 is Released (01 Apr 2024)

Bi-monthly release with minor bug fixes and improvements

MoonLite focuser driver won't work with virtual serial ports

  • Posts: 29
  • Thank you received: 1
For some unknown reasons, the MoonLite focuser driver in INDI doesn't send the :SNYYYY# command to set the position of the focuser when it is connected to a virtual serial port created with socat in Ubuntu (therefore the focuser does not move). I need it to create a virtual serial port that receives data over the internet from a remote RPi, which forwards every command from the serial port to the INDI driver and vice versa. All the other commands do work, like temperature and position query, half/full step mode and :FG# (command that makes the focuser move according to what the driver said with :SNYYYY#). I wrote a simple basic Java class that uses jSSC as an external library and invokes socat with ProcessBuilder. All you have to do to test it is set the serial port, run it and connect MoonLite to the virtual serial port printed to stdout by the Java class.
Does anyone know what's wrong?
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
public class Test {
 
    public static void main(String[] args) throws IOException, InterruptedException {
        SerialPortImpl sp = new SerialPortImpl("/dev/ttyUSB0");
 
        ProcessBuilder processBuilder = new ProcessBuilder("socat", "-d", "-d", "pty,raw,echo=0", "pty,raw,echo=0");
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String port1 = null, port2 = null, line;
        while ((line = in.readLine()) != null) {
            if (line.contains("] ")) {
                line = line.substring(line.indexOf("] ") + 2);
                if (line.startsWith("N PTY is ")) {
                    line = line.replace("N PTY is ", "");
                    if (port1 == null) {
                        port1 = line;
 
                    } else {
                        port2 = line;
                        break;
                    }
                }
            }
        }
 
        SerialPortImpl mockedSerialPort = new SerialPortImpl(port1);
        mockedSerialPort.addListener(new Forwarder(sp));
        sp.addListener(new Forwarder(mockedSerialPort));
 
        //System.out.println(port1);
        System.out.println(port2);
        process.waitFor();
        in.close();
    }
 
    protected static class Forwarder {
 
        private SerialPortImpl forwardTo;
 
        Forwarder(SerialPortImpl forwardTo) {
            this.forwardTo = forwardTo;
        }
 
        void onPortMessage(String msg) {
            System.out.println("FORWARDING     " + msg);
            forwardTo.print(msg);
        }
    }
 
    public static class SerialPortImpl implements SerialPortEventListener {
 
        ArrayList<Forwarder> listeners = new ArrayList<>();
        SerialPort serialPort;
 
        SerialPortImpl(String port) {
            connect(port);
        }
 
        boolean isConnected() {
            return (serialPort != null) && serialPort.isOpened();
        }
 
        void addListener(Forwarder listener) {
            if (listeners.contains(listener)) {
                System.out.println("Listener already in the list!");
 
            } else {
                listeners.add(listener);
            }
        }
 
        void connect(String port) {
            serialPort = new SerialPort(port);
            try {
                serialPort.openPort();
                serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                serialPort.setEventsMask(SerialPort.MASK_RXCHAR);
                serialPort.addEventListener(this);
 
            } catch (SerialPortException e) {
                e.printStackTrace();
            }
        }
 
        void print(String message) {
            if (isConnected()) {
                new Thread(() -> {
                    try {
                        if (!serialPort.writeBytes(message.getBytes())) {
                            System.out.println("error writing");
                        }
 
                    } catch (SerialPortException e) {
                        e.printStackTrace();
                    }
                }, "Serial data sender").start();
 
            } else {
                System.out.println("not connected");
            }
        }
 
        @Override
        public void serialEvent(SerialPortEvent portEvent) {
            try {
                String msg = serialPort.readString();
                if ((msg != null) && (!msg.equals(""))) {
                    for (Forwarder l : listeners) {
                        l.onPortMessage(msg);
                    }
                }
 
            } catch (SerialPortException e) {
                e.printStackTrace();
            }
        }
    }
}
4 years 7 months ago #43330

Please Log in or Create an account to join the conversation.

Time to create page: 0.292 seconds