Hello! Please show live code that runs on JAVA … I used an example:
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class PololuSerialExample {
public PololuSerialExample() {
super();
}
static void listPorts() {
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println(portIdentifier.getName() + " - "
+ getPortTypeName(portIdentifier.getPortType()));
}
}
static String getPortTypeName(int portType) {
switch (portType) {
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
public static void main(String[] args) {
// We renamed /dev/ttyACM0 to /dev/ttyUSB0 because the rxtx.org library
// doesn't recognize devices prefixed by ttyACM.
String portName = "COM5";
try {
listPorts();
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open("Owner", 2000);
if (commPort instanceof SerialPort) {
// we create a output stream for serial port:
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream serialStream = serialPort.getOutputStream();
byte command = (byte) 0xFF;
int servo = 0;
int position = 992;
String message = String.format(
"Writing command=%d servo=%d, psition=%d",
command, servo, position);
System.out.println(message);
byte[] bytes = new byte[]{(byte) command,
(byte) servo, (byte) position};
serialStream.flush();
serialStream.write(bytes);
commPort.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
code is not legal competence. took it on this forum, it is connected properly, the Maestro Control Center all turns.
Hello from Russia