本文介绍了连接到 rfcomm0 的 Java rxtx 代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地将我的 arduino uno R3 与蓝牙 mate 模块连接起来,并且能够通过蓝牙将数据发送到我运行 Ubuntu 12.04 的笔记本电脑.(使用串口协议).在 rfcomm0 上接收数据.

I have successfully connected my arduino uno R3 with bluetooth mate module and am able to send data to my laptop running Ubuntu 12.04 via bluetooth. (using serial port protocol). The data is received on rfcomm0.

以下代码显示接收到的数据:sudo screen/dev/rfcomm0

The following code displays the received data : sudo screen /dev/rfcomm0

现在我在 java 程序中读取这些数据时遇到了问题.我引用了 http://playground.arduino.cc/Interface/Java 中的代码.这使用 rxtx 库来访问串行端口.

Now I am facing problems in reading this data in a java program. I have referred the code from http://playground.arduino.cc/Interfacing/Java. This uses the rxtx library to access the serial port.

代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;


public class SerialTest implements SerialPortEventListener {
    SerialPort serialPort;
    /** The port we're normally going to use. */
    private static final String PORT_NAMES[] = {
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM3", // Windows
//          "/dev/ttyACM0" // Ubuntu
            "/dev/rfcomm0" // Ubuntu Bluetooth
    };
    /**
    * A BufferedReader which will be fed by a InputStreamReader
    * converting the bytes into characters
    * making the displayed results codepage independent
    */
    private BufferedReader input;
    /** The output stream to the port */
    private OutputStream output;
    /** Milliseconds to block while waiting for port open */
    private static final int TIME_OUT = 2000;
    /** Default bits per second for COM port. */
    private static final int DATA_RATE = 9600;

    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        //First, Find an instance of serial port as set in PORT_NAMES.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
            System.out.println("Could not find COM port.");
            return;
        }

        try {
            // open serial port, and use class name for the appName.
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);

            // set port parameters
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            // open the streams
            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            // add event listeners
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    /**
     * This should be called when you stop using the port.
     * This will prevent port locking on platforms like Linux.
     */
    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    /**
     * Handle an event on the serial port. Read the data and print it.
     */
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=input.readLine();
                System.out.println(inputLine);
            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }
        // Ignore all the other eventTypes, but you should consider the other ones.
    }

    public static void main(String[] args) throws Exception {
        SerialTest main = new SerialTest();
        main.initialize();
        Thread t=new Thread() {
            public void run() {
                //the following line will keep this app alive for 10 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {Thread.sleep(10000);} catch (InterruptedException ie) {}
            }
        };
        System.out.println("Started");
        t.start();
        t.join();
        main.close();
        System.out.println("Stopped");
    }
}

此程序编译成功,但执行时显示:Could not find COM port."

This program compiles seccessfully but when executes displays : "Could not find COM port."

注意:当从 USB 端口/dev/ttyACM0 读取数据时,此代码完美运行.当我尝试从蓝牙端口/dev/rfcomm0 读取数据时出现问题.

Note : This code works perfectly when reading data from usb port /dev/ttyACM0. The problem arises when i try to read data from bluetooth port /dev/rfcomm0.

所以基本上我需要一个java程序来从rfcomm0端口读取.非常感谢任何帮助.

So basically I need a java program to read from rfcomm0 port. Any help is greatly appreciated.

推荐答案

感谢@angelatlarge 的建议.

Thanks to @angelatlarge for his suggestion.

问题是java程序没有权限访问/dev/rfcomm0.

The problem was that the java program did not have the permission to access /dev/rfcomm0.

提供权限的命令:sudo chmod a+rw/dev/rfcomm0

所以授予 /dev/rfcomm0 权限为我解决了这个问题.现在java程序可以从rfcomm0端口读取数据了.

So giving the permission to /dev/rfcomm0 solved the problem for me.Now the java program can read the data from the rfcomm0 port.

这篇关于连接到 rfcomm0 的 Java rxtx 代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 08:33