Mini Maestro in Java Code Explanation?

Hello guys,

I am a beginner in programming Servo Controllers and just got my Mini Maestro 12 Channel. I need to programm it with Java and I already found a working example with the RXTX Library but i dont really get the Code. Could someone here explain it to me? Especially the part in the method SetChannelToPos().

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;
import java.io.OutputStream;
import java.util.Enumeration;

public class ServoController {
	
	public static void main(String[] args) {
		ServoController contrl = new ServoController();
		
		contrl.setChannelToPos(1, 4000);
		
	
	}
	
	private CommPortIdentifier serialPortId;
	@SuppressWarnings("rawtypes")
	private Enumeration enumComm;
	private SerialPort serialPort;
	private OutputStream outputStream;
	private int baudrate = 9600;
	private String portName = "COM";

	public ServoController() {
		if (openSerialPort(portName) == false) {
			System.out.println("Connection failed !");
		}
	}

	public boolean openSerialPort(String portName) {
		boolean foundPort = false;
		enumComm = CommPortIdentifier.getPortIdentifiers();
		while (enumComm.hasMoreElements()) {
			serialPortId = (CommPortIdentifier) enumComm.nextElement();
			if (portName.contentEquals(serialPortId.getName())) {
				foundPort = true;
				break;
			}
		}
		if (!foundPort)
			return false;
		try {
			serialPort = (SerialPort) serialPortId.open(this.getClass()
					.getName(), 2000);
			outputStream = serialPort.getOutputStream();
			serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public void closePort() {
		serialPort.close();
	}

public void setChannelToPos(int channel, int position) {
		String pos = Integer.toBinaryString(position);
		String buf = "";
		for (int i = 0; i < 14 - (pos.length()); i++)
			buf += "0";
		pos = buf + pos;
		try {
			outputStream.flush();
			outputStream.write((byte) 0x84);
			outputStream.write((byte) channel);
			outputStream.write((byte) Integer.parseInt(pos.substring(7, 14), 2));
			outputStream.write((byte) Integer.parseInt(pos.substring(0, 7), 2));
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

Thank you very much :slight_smile:

Best regards

Hi.

It looks like the code you posted was not written by us. Where did you get it from? You might try contacting the author directly to ask how the code works.

-Taylor

Hi sunilson,
Did you have any success with your java code?
I have just purchased a 6-Channel Maestro servo controller and I would also like to use a java program to control the servos.
So far I’ve had to luck. Any information you can pass on to me will be appreciated.
Thanks!

Hello.

We do not have any Java sample code for the Maestro. However, one of our forum members seems to have gotten it to work and posted some code you might take a look at in this forum thread.

-Derrill

Thanks Derrill,

I’ve tried that code, but I’m met with the following error message:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread “main” java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path

I’ll keep working on it. In the meantime if anyone has a suggestion, please let me know!

Hello.

It looks like you are missing the RXTX library. I do not know what IDE you are using, but if you are using Eclipse, you can open the project’s properties build path and add the RXTX library to the project.

- Amanda