串行端口未关闭。 我想释放COM端口

发布于 2024-07-23 14:20:12 字数 3763 浏览 6 评论 0原文

串行端口未关闭。 我想释放COM端口...

下面是我的代码...

import java.io.*;
import java.util.*;
import gnu.io.*;

public class ReadCommPort implements SerialPortEventListener {

    static CommPortIdentifier   portId;
    static Enumeration            portList;
    InputStream                    inputStream;
    OutputStream                outputStream;
    public SerialPort            serialPort;
    List byteList               = new ArrayList();
    public static Message message   = null;

    public void readData() {
        boolean    portFound   = false;
        String    defaultPort = "COM1";
        portList            = CommPortIdentifier.getPortIdentifiers();

        while ( portList.hasMoreElements() ) {
            portId = ( CommPortIdentifier )portList.nextElement();
            if ( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
                if ( portId.getName().equals( defaultPort ) ) {
                    System.out.println( "Found port: " + defaultPort );
                    portFound = true;
                    buildSerialPort();
                }
            }
        }
        if ( ! portFound ) {
            System.out.println( "port " + defaultPort + " not found." );
        }
    }

    public void buildSerialPort() {
        try {
            serialPort  = (SerialPort) portId.open( "ReadCommPort", 1 );
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            serialPort.addEventListener( this );
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams( 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1,

                    SerialPort.PARITY_NONE );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public void serialEvent( SerialPortEvent event ) {

        switch ( event.getEventType() ) {
        case SerialPortEvent.BI:
            System.out.println( "BI");
            break;

        case SerialPortEvent.OE:
            System.out.println( "OE");
            break;

        case SerialPortEvent.FE:
            System.out.println( "FE");
            break;

        case SerialPortEvent.PE:
            System.out.println( "PE");
            break;

        case SerialPortEvent.CD:
            System.out.println( "CD");
            break;

        case SerialPortEvent.CTS:
            System.out.println( "CTS");
            break;

        case SerialPortEvent.DSR:
            System.out.println( "DSR");
            break;

        case SerialPortEvent.RI:
            System.out.println( "RI");
            break;

        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println( "OUTPUT_BUFFER_EMPTY");
            break;

        case SerialPortEvent.DATA_AVAILABLE :
            try {
                int len = inputStream.available();
                byte[] readBuffer = new byte[ len ];
                // processing data code..
                // close the port
                // release all resources...
                serialPort.removeEventListener();
                try
                {
                    serialPort.addEventListener( null );
                }
                catch (TooManyListenersException e)
                {
                    e.printStackTrace();
                }
                inputStream.close();
                outputStream.close();
                serialPort.close();
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
            break;
        }
    }

    public static void main(String[] args) {
        new ReadCommPort().readData();
    }
}

Serial Port Not getting closed. I want to release the COM port ...

Below is my code....

import java.io.*;
import java.util.*;
import gnu.io.*;

public class ReadCommPort implements SerialPortEventListener {

    static CommPortIdentifier   portId;
    static Enumeration            portList;
    InputStream                    inputStream;
    OutputStream                outputStream;
    public SerialPort            serialPort;
    List byteList               = new ArrayList();
    public static Message message   = null;

    public void readData() {
        boolean    portFound   = false;
        String    defaultPort = "COM1";
        portList            = CommPortIdentifier.getPortIdentifiers();

        while ( portList.hasMoreElements() ) {
            portId = ( CommPortIdentifier )portList.nextElement();
            if ( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
                if ( portId.getName().equals( defaultPort ) ) {
                    System.out.println( "Found port: " + defaultPort );
                    portFound = true;
                    buildSerialPort();
                }
            }
        }
        if ( ! portFound ) {
            System.out.println( "port " + defaultPort + " not found." );
        }
    }

    public void buildSerialPort() {
        try {
            serialPort  = (SerialPort) portId.open( "ReadCommPort", 1 );
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            serialPort.addEventListener( this );
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams( 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1,

                    SerialPort.PARITY_NONE );
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public void serialEvent( SerialPortEvent event ) {

        switch ( event.getEventType() ) {
        case SerialPortEvent.BI:
            System.out.println( "BI");
            break;

        case SerialPortEvent.OE:
            System.out.println( "OE");
            break;

        case SerialPortEvent.FE:
            System.out.println( "FE");
            break;

        case SerialPortEvent.PE:
            System.out.println( "PE");
            break;

        case SerialPortEvent.CD:
            System.out.println( "CD");
            break;

        case SerialPortEvent.CTS:
            System.out.println( "CTS");
            break;

        case SerialPortEvent.DSR:
            System.out.println( "DSR");
            break;

        case SerialPortEvent.RI:
            System.out.println( "RI");
            break;

        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            System.out.println( "OUTPUT_BUFFER_EMPTY");
            break;

        case SerialPortEvent.DATA_AVAILABLE :
            try {
                int len = inputStream.available();
                byte[] readBuffer = new byte[ len ];
                // processing data code..
                // close the port
                // release all resources...
                serialPort.removeEventListener();
                try
                {
                    serialPort.addEventListener( null );
                }
                catch (TooManyListenersException e)
                {
                    e.printStackTrace();
                }
                inputStream.close();
                outputStream.close();
                serialPort.close();
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
            break;
        }
    }

    public static void main(String[] args) {
        new ReadCommPort().readData();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-07-30 14:20:12

如果您使用的是RXTX-2.1版本,那么通过调用removeEventListener(); 程序应该挂起。 这是由于库中的本机代码造成的。 这可以通过运行这样的代码来避免,但就我个人而言,这种方式不太喜欢:

new Thread(){
    @Override
    public void run(){
        serialPort.removeEventListener();
        serialPort.close();
    }
}.start();

如果你基本上使用RXTX,那么使用2.2版本。 以及尝试组件 jSSC 的提示 - http://code.google.com /p/java-simple-serial-connector/

If you are using a version of RXTX-2.1, then by calling removeEventListener (); program should hang. This is due to native code in the library. This can be avoided by running such a code, but to me personally this way do not really like:

new Thread(){
    @Override
    public void run(){
        serialPort.removeEventListener();
        serialPort.close();
    }
}.start();

If you basically use RXTX, then use version 2.2. As well as tips to try component jSSC - http://code.google.com/p/java-simple-serial-connector/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文