I've got this weird problem that my software doesn't read the byte it sent when using the real COM5 or COM6, but when using a RS232 tot USB cable (which came up in Windows as COM12) it did work. Connected to the COM-port are two wires that will tell whether the door of the device is opened or not.
How can it be that the following code works on COM12 (a virtual COM-port), but not on COM5 and COM6 (real COM-ports) and when using putty and entering characters when the door is closed all three show the entered characters. It's the same behavior for all 3 ports with putty, but the software only works when it's connected to the vrtual COM-port that's made up by the Serial2USB cable ...
The problem occurs at a computer with 32-bit version of Windows 7 Embedded.
public void continuouslyCheckConnection() {
new Thread() {
public void run() {
while(true) {
if (hasStarted) {
sendByte();
int newStatus = readWithPossibleDelay(100);
logger.debug("new doorreader status: " + newStatus);
if (latestStatus != newStatus) {
latestStatus = newStatus;
if (newStatus == 0)
mainController.getScreensController().doorOpened();
}
}
try {
Thread.sleep(100);
} catch(InterruptedException ie) {
logger.error("DoorReader; InterruptedException in continuouslyCheckConnection: " + ie.getMessage());
}
}
}
}.start();
}
public void sendByte() {
try {
serialPort.writeByte((byte)0x01);
} catch(SerialPortException spe) {
logger.error("DoorReader; SerialPortException in sendByte: " + spe.getMessage());
}
}
private synchronized int readWithPossibleDelay(int delay) {
Callable<Integer> readTask = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
byte[] byteArray = serialPort.readBytes(1);
int readInt = byteArray[0];
return readInt;
}
};
Future<Integer> future = executor.submit(readTask);
try {
return future.get(delay, TimeUnit.MILLISECONDS);
} catch (ExecutionException ee) {
logger.error("DoorReader; ExecutionException in readWithPossibleDelay: " + ee.getMessage());
} catch (InterruptedException ie) {
logger.error("DoorReader; InterruptedException in readWithPossibleDelay: " + ie.getMessage());
} catch (TimeoutException te) {
// ignore, this returns 0 which is okay
}
return 0;
}
I';ve also found somewhere that explicitly setting
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
might help, but it didnt change anything