Java 在com端口上同时读写

Java 在com端口上同时读写,java,serial-port,Java,Serial Port,我的问题是,难道不可能在一个com端口上同时读写吗?这只是为了演示目的,我只会在按下键盘上的F5键后读取端口,但为了演示目的,我尝试同时读取和写入,但无法执行。它表示,其他程序正在使用该端口 更新 package com_port; import java.io.*; import java.util.*; import javax.comm.*; public class simplerad implements Runnable, SerialPortEventListener {

我的问题是,难道不可能在一个com端口上同时读写吗?这只是为了演示目的,我只会在按下键盘上的
F5
键后读取端口,但为了演示目的,我尝试同时读取和写入,但无法执行。它表示,
其他程序正在使用该端口

更新

package com_port;

import java.io.*;
import java.util.*;
import javax.comm.*;

public class simplerad implements Runnable, SerialPortEventListener
{
    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public static void main(String[] args) throws Exception
    {
        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("SimpleRead Started.");
        while (portList.hasMoreElements())
            {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
                    {
                        System.out.println ("Found " + portId.getName());
                        if (portId.getName().equals("COM7"))
                            {
                                OutputStream outputStream;
                                SerialPort writePort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                                writePort.setSerialPortParams(9600,
                                                              SerialPort.DATABITS_8,
                                                              SerialPort.STOPBITS_1,
                                                              SerialPort.PARITY_NONE);
                                outputStream = writePort.getOutputStream();
                                outputStream.write("AT+CENG=2".getBytes());
                                System.out.println("write done");
                                writePort.close();
                                simplerad reader = new simplerad();
                            }
                    }
            }
    }

    public simplerad()
    {
        try
            {
                System.out.println("1");
                serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
            }
        catch (PortInUseException e)
            {
                e.printStackTrace();
            }
        try
            {
            System.out.println("2");    
            inputStream = serialPort.getInputStream();
            }
        catch (IOException e)
            {
                e.printStackTrace();
            }
        try
            {
            System.out.println("3");    
            serialPort.addEventListener(this);
            }
        catch (TooManyListenersException e)
            {
                e.printStackTrace();
            }
        serialPort.notifyOnDataAvailable(true);
        try
            {
            System.out.println("4");
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                               SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            }
        catch (UnsupportedCommOperationException e)
            {
                e.printStackTrace();
            }
        readThread = new Thread(this);
        readThread.start();
    }

    public void run()
    {
        System.out.println("5");
        try
            {
            System.out.println("6");
                Thread.sleep(200);
            }
        catch (InterruptedException e)
            {
                e.printStackTrace();
            }
    }

    public void serialEvent(SerialPortEvent event)
    {
        System.out.println("7");
        switch (event.getEventType())
            {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                byte[] readBuffer = new byte[20];

                try
                    {
                    System.out.println("8");
                        while (inputStream.available() > 0)
                            {
                                int numBytes = inputStream.read(readBuffer);
                                System.out.print(new String(readBuffer, 0, numBytes, "us-ascii"));
                            }
                        System.out.print(new String(readBuffer));
                    }
                catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                break;
            }
    }
}

为什么
serialEvent
不运行?

不,不能在同一端口上同时读写。 因为要避免死锁情况

我查了你的密码,发现。。。在
while
循环中

        while (portList.hasMoreElements())
            {
              } 

问题在于
while
循环,因为
端口列表
没有要读取的元素。

这里的问题是,您试图打开同一个COM端口两次,答案是否定的,这将不起作用-至少在这里是这样做的。原因是底层操作系统只允许每个端口有一个用户模式句柄

但是,是的,只要COM端口是双向的(大多数是双向的),您就可以打开一个COM端口并同时对其进行读/写-这就是全双工模式

要在应用程序中获得此行为,您需要打开一次“COM7”端口,然后让您的读线程访问此原始对象,而不是尝试第二次打开它。另一种方法是在打开后立即获取InputStream和OutputStream,然后在应用程序的其余部分使用这些对象

我不确定事件侦听器为什么没有启动,但是您的代码对于您在这里实际尝试执行的操作似乎有点复杂。打开/关闭可能会导致您错过事件通知

下面是一个在open上应该执行的操作示例,这应该是一个比在读写之间打开/关闭端口更有效的解决方案:

// Each of these could be object members so all your class methods have access
OutputStream outputStream;
InputStream inputStream;
SerialPort serialPort;

// Open the port one time, then init your settings
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

// Get the input/output streams for use in the application
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();

// Finally, add your event listener
serialPort.addEventListener(this);

那么我该怎么办?有什么建议吗?如果端口上连续提供数据,当我按下
F5
键时,它会读取该数据吗?唯一的方法是有两个端口。。一个用于写作,一个用于阅读。。或者您可以读取同一端口,关闭它,然后在写入模式下重新打开它。@AJ。如果端口上持续提供数据,当我按F5键时,你认为我能读取该数据吗?@AJ。很好的解释我没有理解你的意思@AJ。?我试着证明它不起作用。但正如我所说的,这只是一个演示,主要的是阅读。那么它会像我在评论1中所问的那样起作用吗