gpt4 book ai didi

java - 我想在java中使用RxTx通过串口检测设备

转载 作者:行者123 更新时间:2023-11-29 09:30:51 24 4
gpt4 key购买 nike

我想在 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){}

}

最佳答案

 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 分钟的谷歌搜索可能会告诉你同样的事情。

关于java - 我想在java中使用RxTx通过串口检测设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com