我想在java中使用RxTx通过串行端口检测设备,并且设备被编程为如果它从计算机接收到特定单词,它将回复“ok”,如果计算机接收到ok..它将停止发送单词并突出显示设备已连接。请帮我。还有一件事..我必须检查每个端口..请您编写一种自动循环通过端口直到检测到设备的方法。我的代码即使在无限循环中也只发送一次单词。代码:
private void cb1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
try{
l1.setText("Port: "+cb1.getSelectedItem().toString()+" is Selected");
selectedPort = cb1.getSelectedItem().toString();// TODO add your handling code here
rs.connect(selectedPort);
for(;;)
{
CommPortSender.send(new ProtocolImpl().getMessage("KITM"));//send message
if(pi.rmess().equalsIgnoreCase("OK"))//received message
{
l1.setText("The Device is attached to: "+selectedPort);
CommPortSender.send(new ProtocolImpl().getMessage("OK ACK"));//send message
break;
}
else
{
rs.disconnect(selectedPort);
continue;
}
}
}
catch(Exception e){}
}