Hi,

I have been able to open a connection and to get my device to respond. Unfortunately, when I send data through with out.write(string.getBytes()), I need to press "enter" twice. The data does not go through until the second time around.

For example:

device>
I type "hello".
device>
I type "enter".
device>hello

I don't understand why there is this delay. I look forward to your help. Thanks.

N29



import gnu.io.*; //use this instead of javax.com if you are using RXTX
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;

public class CiscoTest2 implements SerialPortEventListener {
static CiscoTest2 myFrame;
//we are making this into a frame so we can track the mouse
SerialPort mySerialPort =null;
InputStream in;
OutputStream out;
private byte[] buffer = new byte[2048];
CommPort thePort;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

static public void main(String[] args) {
// Print out the list of serial ports,
//in case you don't know the name"
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName() + " " +portId.getCurrentOwner());
}
}


// Change the port name below as needed on the Mac:
myFrame= new CiscoTest2("COM1",9600);
}

public CiscoTest2 (String whichPort, int whichSpeed) {
//which port you want to use and the baud come in as parameters
CommPortIdentifier portId = null;

try {
//find the port
portId = (CommPortIdentifier.getPortIdentifier(whichPort));
portId.addPortOwnershipListener(new Resolver());
thePort = portId.open("SerialExample" + whichPort, 2000);
mySerialPort = (SerialPort) thePort;
//portId = CommPortIdentifier.getPortIdentifier(whichPort);
//open the port
//mySerialPort = (SerialPort)portId.open("SerialExample" + whichPort, 2000);
//configure the port
try {
mySerialPort.setSerialPortParams(whichSpeed,
mySerialPort.DATABITS_8,
mySerialPort.STOPBITS_1,
mySerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e){System.out.println("Probably an unsupported Speed");}
//establish streams for reading and writing to the port
try {
in = mySerialPort.getInputStream();
out = mySerialPort.getOutputStream();
} catch (IOException e) { System.out.println("couldn't get streams");}
try {
mySerialPort.addEventListener(this);
mySerialPort.notifyOnDataAvailable(true);
} catch (TooManyListenersException e) {System.out.println("couldn't add listener");}

String enter = "\r\n";
out.write(enter.getBytes());
out.flush();
}
catch (Exception e) { System.out.println("Port in Use");}
}

class Resolver implements CommPortOwnershipListener {
protected boolean owned = false;
public void ownershipChange(int whaHoppen) {
switch (whaHoppen) {
case PORT_OWNED:
System.out.println("An open succeeded.");
owned = true;
break;
case PORT_UNOWNED:
System.out.println("A close succeeded.");
owned = false;
break;
case PORT_OWNERSHIP_REQUESTED:
if (owned) {
if (JOptionPane.showConfirmDialog(null, "Give up port?", "Port Conflict", JOptionPane.OK_CANCEL_OPTION) == 0)
thePort.close();
} else {
System.out.println("Somebody else has the port");
}
}
}
}
public void serialEvent(SerialPortEvent event) {
if (event.getEventType()== SerialPortEvent.DATA_AVAILABLE) {
try {
out.flush();
while (in.available() > 0) {
int bytesread = in.read(buffer);
for(int i=0; i<bytesread; i++) {
System.out.print((char) (buffer[i]));
}
}
} catch (IOException e) {}

userInput();
}

}

public void userInput() {
String userString = null;
try {
userString = br.readLine();
userString = userString + "\n";
out.write(userString.getBytes());
out.flush();
} catch (IOException ioe) {
System.out.println("IO error cannot get user input!");
System.exit(1);
}
}
}