使用RXTX运行程序时出错

时间:2015-01-08 04:56:52

标签: java api serial-port rxtx

我在我的应用程序中使用RXTX(gnu.io. *;)进行串口通信,我已经下载了库并添加了支持文件如下:

  1. rxtxParallel.dll
  2. 中复制rxtxSerial.dllC:\Program Files\Java\jdk1.8.0_25\jre\bin
  3. RXTXcomm.jar
  4. 中复制C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

    我正在尝试执行下面提到的程序:

    import gnu.io.*;
    import java.io.*;
    import java.util.*;
    
    public class SimpleWrite implements Runnable, SerialPortEventListener {
    
        public void run() {
        }
        static Enumeration portList;
        static CommPortIdentifier portId;
        static String messageString = "AAA";
        static char ch = '"';
        static String dest = ch + "XXXXXXXXXXX" + ch;  // 11 Digit Mobile Number.
        static InputStream inputStream;
        static SerialPort serialPort;
        static OutputStream outputStream;
    
    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:
                break;
            case SerialPortEvent.DATA_AVAILABLE: {
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                try {
    
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    System.err.println("Error while reading Port " + e);
                }
                break;
    
            }
        } //switch
    }
    
    public SimpleWrite(SerialPort serial) {
        try {
            inputStream = serial.getInputStream();
            try {
                serial.addEventListener(this);
            } catch (TooManyListenersException e) {
                System.out.println("Exception in Adding Listener" + e);
            }
            serial.notifyOnDataAvailable(true);
    
        } catch (Exception ex) {
            System.out.println("Exception in getting InputStream" + ex);
        }
    
    }
    
    public static void main(String[] args) {
        String line1 = "AT+CSMS=1\r\n";
        String line2 = "AT+CMGS=" + dest + "\r\n";
        String line3 = messageString + "\r\n";
    
        portList = CommPortIdentifier.getPortIdentifiers();
    
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM3")) {
                    System.out.println("SMS Sending....Port Found");
                    try {
                        serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                        SimpleWrite wr = new SimpleWrite(serialPort);
    
                    } catch (PortInUseException e) {
                        System.out.println("Port In Use " + e);
                    }
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {
                        System.out.println("Error writing to output stream " + e);
                    }
                    try {
                        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {
                    }
                    try {
                        outputStream.write(line1.getBytes());
                        outputStream.write(line1.getBytes());
                        outputStream.write(line2.getBytes());
                        outputStream.write(line3.getBytes());
                        outputStream.write(26);
                        outputStream.flush();
                    } catch (Exception e) {
                        System.out.println("Error writing message " + e);
                    }
                }
            }
        }
    }
    
    /**
     * show text in the text window
     *
     * @param Text text string to show on the display
     */
    public static void showText(String Text) {
        System.out.println(Text);
      }
    }
    

    但是我在运行我的应用程序时收到如下错误:

    C:\>javac SimpleWrite.java
    
    C:\>java SimpleWrite 
    java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while
    loading gnu.io.RXTXCommDriver
    Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: no rxtxSerial in
    java.library.path
            at java.lang.ClassLoader.loadLibrary(Unknown Source)
            at java.lang.Runtime.loadLibrary0(Unknown Source)
            at java.lang.System.loadLibrary(Unknown Source)
            at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
            at SerialConnection.openConnection(SerialConnection.java:71)
            at Sender.send(Sender.java:46)
            at SMSClient.run(SMSClient.java:39)
            at java.lang.Thread.run(Unknown Source)
    

    我已经检查过,但显示的所有解决方案都与IDE一致。怎么解决这个?请指导我,我是新手!

1 个答案:

答案 0 :(得分:1)

如果从IDE运行程序,请确保项目的java平台设置为您复制的库,即,C:\ Program Files \ Java \ jdk1.8.0_25 \

因为,IDE附带默认的java平台&amp;当您运行该项目时,它使用默认的Java平台。

您可以在项目的属性对话框中更改要用于项目的平台。