繁体   English   中英

无法从Java的COM端口读取串行数据

[英]Unable to read serial data from COM port in Java

我正在尝试读取我在Linux(Ubuntu 12.10)上开发的Java应用程序的串行数据。 这是我的代码:

try {
    portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
    SimpleRead reader = new SimpleRead();
} catch (Exception e) {
    TimeStamp=new Date().toString();
    System.out.println(TimeStamp+": ttyS0 "+portId);
    System.out.println(TimeStamp+": msg1 - "+e);
}

SimpleRead构造函数和其他所需的代码:

public SimpleRead() {
    try {
        TimeStamp=new Date().toString();
        serialPort = (SerialPort) portId.open("SimpleRead", 2000);
        System.out.println(TimeStamp + ": " + portId.getName() + " opened for scanner input");
    } catch (PortInUseException e) {
        System.out.println(e);
    }
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("Input Stream set");
    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {
        System.out.println(e);
    }
    System.out.println("Event listener added");
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        serialPort.setDTR(false);
        serialPort.setRTS(false);
    } catch (UnsupportedCommOperationException e) {
        System.out.println(e);
    }
    System.out.println("Parameters written");
    readThread = new Thread(this);
    readThread.start();
}

@Override
public void run() {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        System.out.println(e);
    }
}

@Override
public void serialEvent(SerialPortEvent event) {
    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:
            System.out.println("Hello");
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];
            System.out.println("Hello");
            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                    if (numBytes>=16)
                        break;
                }
                //System.out.print(new String(readBuffer));
                TimeStamp = new java.util.Date().toString();
                System.out.println(TimeStamp + ": scanned input received:" + new String(readBuffer));
                inputStream.close();
            } catch (IOException e) {
                System.out.println(e);
            }
            break;
    }
}

由于某些原因,由于没有在输出屏幕中打印serialEvent()方法主体中的“ Hello”,因此没有从端口读取串行数据。 请在这里帮助我!

编辑代码取自: http : //www.java-samples.com/showtutorial.php? tutorialid=11是的,在尝试从设备读取之前,已为设备配置了给定参数。

新编辑我正在使用适用于Linux x64平台的Java SE 8开发套件,以及从http://www.java2s.com/Code/Jar/c/Downloadcomm20jar.htm获得的Java Communications API。 我的IDE是Netbeans 8.1。

因此,事实证明,我必须使用Java 1.8的32位版本来访问串行端口。 我的Linux上的IDE即使被迫(通过更改netbeans.conf文件中的“ jdk_home”路径)强制也不接受64位计算机上的32位库,因此我在访问串行端口时遇到了问题。

我将操作系统切换到Windows 7,尽管我仍在64位系统上,因为Windows上的Netbeans IDE 8.1可以在具有各自源库的64位和32位模式下运行。 因此,我在参考64位Windows上的Javax.comm API使其运行后,为Windows安装了JDK 1.8(32位),然后在IDE中运行了我的代码。 现在,可以正确读取来自串行端口的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM