串口双向通信java

时间:2009-02-02 10:07:49

标签: java core

你好我在java中打算,在我的项目数据记录器连接到com端口我必须发送15个整数值到端口然后其他设备将发送回15作为响应,现在我写入输出流但我'我没有得到回应。如何解决这个问题请帮助我。(我正在使用javax.com包)

感谢回复

2 个答案:

答案 0 :(得分:3)

你也必须得到一个InputStream,你无法从OutputStream中读取。或者我错过了什么?

此外,请记住在输出后执行OutputStream.flush(),否则您的数据可能会被缓冲以便稍后发送 - 如果响应者正在等待您的数据,则很可能出现问题。

话虽如此:javax.comm包很老了。上次我使用它时,它几乎被Sun推荐,或者至少没有以任何方式维护。您可能希望查看其他解决方案(SerialIO)。

答案 1 :(得分:0)

尝试以下示例代码

public static void init(String port) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
System.out.println(portId.getName());
    if (portId.getName().equals(port)) {



try {sPort = (SerialPort) portId.open("PORT_NAME", 2000);
             reader = new sms();
             break;
            } 
        catch (Exception e) { System.out.println(e);continue;}
   }
}

}

使用com端口名称调用init()方法(如COM15,COM11,COM12等..)检查它所连接的设备com端口。

相关问题