Hi,
I have inherited some code, multithreaded, with a singleton that talks to LabView over a couple ports/sockets. I'm not sure (but do not believe) the multithreading matters in this case, but mention it in case you think there might be an issue. All communications to LabView go through the singleton LabView class (instance).
When we run the code on the same MS Windows XP box as the LabView then it works properly; meaning LabView receives the commands and the java receives the results. When we run it on a Windows XP box in another room with another subnet it works fine as well. When we run it on a Mac OS in the same room/network the reader.ready() is not true within the wait time. (see the reader.ready() line in the original code).
Does anyone know why it would work for Windows but not Mac OS?
One current "guess" of what is wrong on my part is that the code below (taken out of context, otherwise the post would be really long) needs some rethinking. Any help/comments/advice on the way this is coded is greatly appreciated.
Here is the existing code.
// some declarations.
long t0, t1;
long waittime = 5000;
BufferedReader reader;
public synchronized void connectCommPort() {
if (commportconnected != true) {
try {
sock = new Socket();
InetSocketAddress endpoint = new InetSocketAddress(IPaddress, commPort);
sock.connect(endpoint, 4000); // try to connect with a timeout of 4 seconds.
reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
writer = new PrintWriter(sock.getOutputStream());
System.out.println("mec: LabV: SUCCESS - network connection to Labview command port succeeded");
commportconnected = true;
errorstatus = false;
} catch (IOException ex) {
logwriter("LabV: WARNING - network connection to Labview command port failed."+ex.toString());
System.out.println("mec: LabV: WARNING - network connection to Labview command port failed."+ex.toString());
commportconnected = false;
errorstatus = true;
}
}
}
public synchronized float readpot(int potnumber) {
String smotor = Integer.toString(potnumber);
if (potnumber >= 0 && potnumber <= 5) {
String message = "pot " + smotor;
connectCommPort();
if (commportconnected) {
try {
writer.print(message + "\r\n");
writer.flush();
logwriter("LabV: [sent] " + message);
potvalue = potlistener();
} catch (Exception ex) {
System.out.println("mec: Exception while sending command "+ex.getLocalizedMessage());
}
disconnectCommPort();
}
} else {
if (potnumber == 7 || potnumber == 6) {
potvalue = 5.f;
logwriter("Faked pot reading for potnumber " + potnumber);
} else {
logwriter("WARNING: " + potnumber + " is not a valid pot number (must be 0 to 6)");
}
}
return potvalue;
}
public synchronized float potlistener() {
String message = null;
t0 = System.currentTimeMillis();
t1 = System.currentTimeMillis();
boolean donereading = false;
while ((t1 - t0) < (waittime) & donereading != true) {
t1 = System.currentTimeMillis();
try {
while (reader.ready()) {
message = reader.readLine();
logwriter("LabV: [received] " + message);
if (message.contains(".")) {
potvalue = Float.parseFloat(message);
}
donereading = true;
} // close reader-ready-while
} catch (Exception ex) {
System.err.println("Exeption in potlistener() "+ex.getLocalizedMessage());
}
} // close timeout while loop
return potvalue;
}
In particular, could I readLine() in a loop where I catch the exception if it is not read and just keep trying? What is the "best way" to do this?
Here is what I was thinking, something like:
while ( ((t1 - t0) < waittime) && !donereading ) {
t1 = System.currentTimeMillis();
try {
message = reader.readLine();
logwriter("LabV: [received] " + message);
if (message.contains(".")) {
potvalue = Float.parseFloat(message);
donereading = true;
}
} catch (Exception ex) {
System.err.println("Exeption in potlistener() "+ex.getLocalizedMessage());
}
} // close timeout while loop