Java 串行端口参数值prob

Java 串行端口参数值prob,java,serial-port,javax.comm,Java,Serial Port,Javax.comm,我正在尝试从COM端口读取/写入一些数据。程序设置串行端口参数值如下: serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE). 然而,程序在上面的代码行被卡住了。在该行之前,它成功执行 完整代码如下: public SendMsg(String className) throws NoSuchPortException, IOExcep

我正在尝试从COM端口读取/写入一些数据。程序设置串行端口参数值如下:

serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE).
然而,程序在上面的代码行被卡住了。在该行之前,它成功执行

完整代码如下:

public SendMsg(String className) throws NoSuchPortException, IOException, TooManyListenersException, UnsupportedCommOperationException
{
    this.className=className;
    //portList =  CommPortIdentifier.getPortIdentifiers();
    ports = CommPortIdentifier.getPortIdentifiers();
    System.out.println("ports name"+ports);
    while(ports.hasMoreElements())
    {
        pID = (CommPortIdentifier)ports.nextElement();
        System.out.println("Port Name " + pID.getName());

        if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {
            System.out.println("Port Name 1 " + pID.getName());
            if (pID.getName().equals("COM1"))
            {
                try {
                    System.out.println("Port Name 2 " + pID.getName());
                    System.out.println("COM1 found");
                    serialPort=(SerialPort)pID.open(className, 1000);

                     outputStream=serialPort.getOutputStream();
                     inputStream=serialPort.getInputStream();
                     //f.serialEvent1(inputStream);
                     serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                     System.out.println("hai");

                     serialPort.addEventListener(this);

                     break;
                } catch (PortInUseException ex) {
                    Logger.getLogger(SendMsg.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

听起来好像代码就停在那一点上了?您能在调试器中运行它,然后中断它,并在它停止的地方获得堆栈跟踪吗?这将告诉您更多信息。它不会停止并保持在以下行中:serialPort.setSerialPortParams(9600,serialPort.DATABITS_8,serialPort.STOPBITS_1,serialPort.PARITY_NONE);看看[这个答案是否有用][1][1]: