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

编程入门 行业动态 更新时间:2024-10-25 12:29:08
本文介绍了我想在java中使用RxTx通过串口检测设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在java中使用RxTx通过串口检测设备,并且设备被编程为如果它从计算机收到特定字,它将回复ok并且如果计算机收到ok ...它将停止发送单词和突出显示设备已连接。请帮我。还有一件事......我必须检查每个端口..请您编写一个自动循环通过端口的方法,直到检测到设备。 我的代码即使在无限循环中也只发送一次这个词。 code:

I want to detect the device through serial port using RxTx in java and device is programmed that if it recieves a specific word from computer it will reply "ok" and if the computer receives ok.. it will stop sending the word and highlight that the device is attached. PLEASE HELP ME. and one more thing.. i have to check for every port.. please will you code a method that auto-cycle through the ports till the device is detected. My code sends the word only one time even being in a infinite loop. code:

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

5 minutes of googling could have told you the same thing.

更多推荐

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

本文发布于:2023-11-05 04:10:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我想   串口   检测设备   RxTx   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!