【问题标题】:serial port twoway communication java串口两路通讯java
【发布时间】:2009-02-02 10:07:49
【问题描述】:

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

感谢回复

【问题讨论】:

    标签: java core


    【解决方案1】:

    你也必须得到一个 InputStream,你不能从 OutputStream 中读取。还是我错过了什么?

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

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

    【讨论】:

      【解决方案2】:

      试试下面的示例代码

      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;}
         }
      }
      

      }

      调用 init() 方法并使用 com 端口名称(如 COM15、COM11、COM12 等)检查您连接到的设备的 com 端口。

      【讨论】:

        猜你喜欢
        • 2011-07-01
        • 2013-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多