Java class & Sequence

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

www.rega.135.it

Angelo Rega

I’m not familiar with that language, but on a PC, pausing is not a very good thing to do.
There are simple ways to pause an entire thread, but if you were to do that, everything on that thread would also pause.

The best solution is to create a multithread program so you can stick your program’s processing in a free thread and do with it as you like without harming others.
This’ll also allow your program to continue to to other processing during the pause.

An easier solution would be to just create an empty loop and have it iterate thousands of times.
But, again, that’s wasteful.

I think what you’re describing is a real-time multitasking operating system. I always wondered if there were “free” real-time os out there for various chipsets, like the atom bot board or the rabbit z2000.

If you’re interfacing the ssc-32 with a microcontroller-based system, you can do some search and see if there are any real-time os out there for that board. I did a similar thing to the Lego Mindstorm brick for a Real-time embedded system course. There were many real-time os for that brick, like brickos and esterel. I’m gonna be looking for a real-time os for the rabbit 2000 board (I think it’s z80-based chip).

Is here - it controls all servos simultaneously (which seems to be what you want).

Read the blurb here:

lynxmotion.net/phpbb/viewtopic.php?t=1142

The source is here:

sourceforge.net/projects/seetron-ssc

Enjoy!

Look! I have discovered you problem!

:wink:

Drop the binary DLL this API is using. They are ALL buggy. I’ve had the best results using J2ME of all things but if this isn’t an option try: web.media.mit.edu/~benres/simpleserial/

… and only write per byte and read per byte. Also, forget the ‘\n’ it doesn’t do anything… and remove spaces.