【问题标题】:Java/Arduino - Read data from the Serial PortJava/Arduino - 从串口读取数据
【发布时间】:2013-04-14 06:19:50
【问题描述】:

我有一个 Java 程序,我必须在其中读取 Arduino 发送的信息。 我从here 获取了Java 代码。现在,我真的不明白它是如何工作的,但我尝试修改它并得到了这个:

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 Serial implements SerialPortEventListener {
    SerialPort serialPort;

    private static final String PORT_NAMES[] = {
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM3", // Windows
    };

    private BufferedReader input;
    private static OutputStream output;
    private static final int TIME_OUT = 2000;
    private static final int DATA_RATE = 115200;

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

        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 {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);

            serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,    SerialPort.PARITY_NONE);

            input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

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

    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());
            }
        }
    }

    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }

    public Serial(String ncom){
        if(Integer.parseInt(ncom)>=3 && Integer.parseInt(ncom)<=9)
            PORT_NAMES[2] = "COM" + ncom;
        initialize();
        Thread t=new Thread() {
            public void run() {
                try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
            }
        };
        t.start();
        System.out.println("Serial Comms Started");
    }

    public synchronized void send(int b){
        try{
            output.write(b);
        }
        catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    public synchronized int read(){
        int b = 0;

        try{
            b = (int)input.read();
        }
        catch (Exception e) {
            System.err.println(e.toString());
        }
        return b;
    }
}

我在主程序中使用我需要的COM port 创建对象Serial,然后在需要时使用Serial.readSerial.write

Serial.write 效果很好,Arduino 获取数据并将其显示在 LCD 显示器中。问题是Serial.read。当程序运行时,它会从串口读取数据(大约每 40 毫秒),但这并不意味着 Arduino 发送了一些东西。 Arduino 仅在按下按钮时发送一个字节。因此,当 Java 代码运行时,它会在读取内容之前抛出“n”异常,这会造成很大的延迟。

我知道我需要Serial.available() 之类的东西,我试过input.available(),但它不起作用。我不知道如何解决这个问题。

如果你有一个有效的代码,如果你能把它给我,我会非常感激。我只需要两种方法,读和写,我不在乎代码是如何工作的:D

编辑:

我更改了 Serial 类,现在它又像 apremalal 所说的那样具有这种方法

public synchronized void serialEvent(SerialPortEvent oEvent) {

        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {

                String inputLine=null;
                if (input.ready()) {
                    inputLine = input.readLine();
                    panel.read(inputLine);
                }

            } catch (Exception e) {
                System.err.println(e.toString());
            }
        }       
    }

在另一个班级(在这种情况下为面板)我有这个:

public void read(String data){
    System.out.println(data);
    System.out.println(data == "255");
    if(data == "255")
        //code here 
}

它正确打印值,但 data == "255" 总是错误的,即使我真的得到 255 ....我尝试做Integer.parseInt 但没有任何改变。为什么?

编辑2:好的解决了:\

public void read(String data){

    serialRead = Integer.parseInt(data);

    if(serialRead == 255)
        //code here 
}

现在它的工作..不知道为什么我必须这样做...嗯,随便 :)

【问题讨论】:

    标签: java serial-port arduino


    【解决方案1】:

    您不想专门编写示例代码中已经存在的读取函数。正如 TheMerovingian 指出的那样,您可以在阅读之前检查输入缓冲区。这是我在一个项目中使用的工作代码。

    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
            "COM35", // Windows
    };
    private BufferedReader input;
    private OutputStream output;
    private static final int TIME_OUT = 2000;
    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 {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);
            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();
    
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    
    
    public synchronized void close() {
        if (serialPort != null) {
            serialPort.removeEventListener();
            serialPort.close();
        }
    }
    
    public synchronized void serialEvent(SerialPortEvent oEvent) {
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
            try {
                String inputLine=null;
                if (input.ready()) {
                    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 1000    seconds,
                //waiting for events to occur and responding to them    (printing incoming messages to console).
                try {Thread.sleep(1000000);} catch (InterruptedException    ie) {}
            }
        };
        t.start();
        System.out.println("Started");
    }
    }
    

    编辑:serialEvent 函数负责读取缓冲区。

    public synchronized void serialEvent(SerialPortEvent oEvent) {
     if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=null;
            if (input.ready()) {
                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.
    }
    

    【讨论】:

    • 我已经编辑了serialEvent方法以避免你提到的问题
    • 是的,但此代码在控制台上打印数据。我需要的是,从另一个类(如我的 Main.java)中获取数据并使用它。比如“if(Serial.read()=255){ dosomething()}”
    • 尝试在 serialEvent 方法中实现您需要的功能,而不是使用不同的类。
    • 我就是这么想的。我会尝试将 SerialTest 中的数据发送到我的另一个线程。
    • @apremalal 我正在关注您的代码。我有一个 gsm 调制解调器,端口是 com11。我没有进入 serialEvent 方法。我没有收到任何错误。stackoverflow.com/questions/25882157/…
    【解决方案2】:

    BufferedReader 类有一个ready() 方法,如果“缓冲区不为空,或者基础字符流已准备好”,则该方法返回TrueFalse 否则。因此,您可以在read() 方法中添加一个检查,以确保在尝试读取之前有要读取的数据。

    public synchronized int read(){
    
        int b = 0;  
    
        try{
            if (input.ready()) {
                b = (int)input.read();
            }
        }catch (Exception e) {
            System.err.println(e.toString());
        }
        return b;
    }
    

    看起来代码有一个try-catch 可以在这些事情失败时处理,这可能是导致您滞后的原因,因为try-catch 非常昂贵。所以input.ready() 检查应该会导致更少的异常。

    【讨论】:

    • 我添加了 input.ready(),现在我得到 java.io.IOException: 当我按下按钮时,底层输入流返回零字节。从 Arduino 串行监视器我看到我正在发送的数据,所以我知道它可以工作,但是在 Java 中,当我按下按钮时,程序卡住了,然后当我释放它时,它抛出异常,然后继续正常运行
    • 异常到底说明了什么?
    • java.io.IOException: 底层输入流返回零字节
    • 哦,对了,我虽然这是你下一句话的开始。从BufferedReaderInputStreamReader 文档来看,ready() 方法似乎应该只在缓冲区中有数据的情况下返回True,所以老实说我不知道​​它为什么不起作用。您最好使用SerialEvent 代码并将所有数据存储到您自己的数组中,您可以使用该数组来读取最后收到的 X 个字节。这样您就可以在 arduino 发送数据时接收数据,但您不必尝试手动检索它。
    • 嗯,好的,我会尽快这样尝试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多