[英]I want to detect the device through serial port using RxTx in java
我想使用Java中的RxTx通過串行端口檢測設備,並且對設備進行了編程,如果該設備從計算機接收到特定的單詞,它將回復“確定”,如果計算機收到確定的信息,它將停止發送該單詞並突出顯示該單詞。設備已連接。 請幫我。 還有一件事情..我必須檢查每個端口..請您編寫一種自動循環通過這些端口直到檢測到設備的方法。 我的代碼即使在無限循環中也只發送一次單詞。 碼:
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){}
}
static void listPorts()
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while ( portEnum.hasMoreElements() )
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );
}
}
static String getPortTypeName ( int portType )
{
switch ( portType )
{
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
5分鍾的谷歌搜索可以告訴您同樣的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.