実際の COM5 または COM6 を使用している場合、ソフトウェアが送信したバイトを読み取らないという奇妙な問題が発生しましたが、RS232 tot USB ケーブル (Windows では COM12 として登場) を使用した場合は機能しました。COM ポートには、デバイスのドアが開いているかどうかを示す 2 本のワイヤが接続されています。
次のコードが COM12 (仮想 COM ポート) では機能するが、COM5 および COM6 (実際の COM ポート) では機能しないのはなぜでしょうか。パテを使用してドアが閉じているときに文字を入力すると、3 つすべてが入力された文字を表示します。パテを使用した3つのポートすべてで同じ動作ですが、ソフトウェアは、Serial2USBケーブルで構成される仮想COMポートに接続されている場合にのみ機能します...
この問題は、32 ビット バージョンの 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;
}
私はまた、明示的に設定している場所を見つけました
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
役立つかもしれませんが、何も変わりませんでした