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
Best regards