Hi, i have edited this java class to send strinto via COM, using javaxconn.
I wanna that the servos run a sequence of movements,
For Example:#0 p1300 -Wait 1 second- #1 p 2000 etc. etc.
But my question is: there is a “wait command” ?
I share with you my java class, I hope it can be usefull.
import java.io.;
import java.util.;
import javax.comm.*;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
// Command sent by string message (char)13 is carrige return.
static String messageString = "#0 P1320 " +(char)13;
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
/**
* Method declaration
*
*
* @param args
*
* @see
*/
public static void main(String] args) {
System.out.println("*********************************************************");
System.out.println("ECCO LA LISTA DELLE PORTE DISPONIBILI SUL SISTEMA IN USO.");
System.out.println("*********************************************************");
new CommPortLister().list();
boolean portFound = false;
String defaultPort = "COM1";
if (args.length > 0) {
defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println();
System.out.println("Ok! Ho trovato la porta selezionata " + defaultPort);
portFound = true;
try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Porta in uso da un altro flusso dati.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Errore nel setting della notifica dell'evento");
System.out.println(e.toString());
System.exit(-1);
}
System.out.println(
"Sto scrivendo \""+messageString+"\" sulla porta "
+serialPort.getName());
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
serialPort.close();
System.exit(1);
}
}
}
if (!portFound) {
System.out.println("porta " + defaultPort + " non trovata.Controllare se presente nella lista.");
}
}
// // // // // // // // // // // // // // // // // // // // // // //
//STAMPO LA LISTA DELLE PORTE PRESENTI NEL SISTEMA//
protected void list() {
// Il metodo getCommPortIdentifier mi restituisce la lista delle porte.
Enumeration pList = CommPortIdentifier.getPortIdentifiers();
// Processo che lista le porte
while (pList.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
System.out.println();
System.out.print("Port " + cpi.getName() + " ");
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println();
System.out.println("è una PORTA SERIALE: " + cpi);
} else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println("è una PORTA PARALLELA: " + cpi);
}
else {
System.out.println("è una PORTA SCONOSCIUTA: " + cpi);
}
}
}
// // // // // // // // // // // // // // // // // // // // // // // // //
}
If you wanna download the class and javaxconn pakage, go to my blog
Angelo Rega