当前位置: 首页 > 面试题库 >

Java / Arduino-从串行端口读取数据

文国发
2023-03-14
问题内容

我有一个Java程序,必须读取Arduino发送的信息。我从这里获取了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口,我需要在主程序,然后我使用Serial.readSerial.write当我需要它。

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

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

如果您有能正常工作的代码,那么如果您能给我,我将非常感激。我只需要两种方法,读写,我不在乎代码如何工作:D

编辑:

我更改了Serial类,现在它又如前文所述具有此方法

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 
}

现在它正在工作..不知道为什么我必须这样做…无论如何:)


问题答案:

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

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.
}


 类似资料:
  • 我试图做一些非常简单的东西,按钮计数器,有一个按钮在我的HTML-php当我点击它我发送数据'按'到我的arduino和它使led闪烁,到目前为止,还不错,但然后我想发送信号,当我在arduino中按下按钮,我想让我的php读取字符串"点击次数: N"的串行端口,N是从arduio的推送计数。 这里的问题是,在arduino串行我可以看到他字符串,一切正常,但在PHP我做

  • 问题内容: 我想知道是否有一种方法可以通过PHP读取我的串行端口-可行:-) 在练习Arduino技能时,我开发了一个简单的LED ON / OFF草图。通过在串行监视器中 打开 或 关闭 它可以工作。 下一步,我整理了一个网页,充当GUI界面,以单击链接并执行上面的打开和关闭功能。该基于Web的GUI可通过PHP使用。我正在使用PHP SERIAL 类与Arduino使用的串行端口进行交互。 问

  • 我想知道是否有一种方法可以通过PHP来完成对串行端口的读取——有效:—— 在练习Arduino技能时,我设计了一个简单的LED开关示意图。它通过在串行监视器中输入on或off来工作。 下一步,我把一个网页放在一起,作为一个GUI界面,点击一个链接并执行上面的开关功能。这个基于网络的GUI通过PHP工作。我使用PHP串行类与Arduino使用的串行端口进行交互。 问题是我需要找到一种从串口获取反馈的

  • 我做了一个python程序,从串行端口读取gps数据。GPS冰球流NMEA数据语句连续插入USB时。我的程序打开端口,然后尝试读取数据,解析它,然后将其与从Arduino提取的其他数据一起写入文本文件。 我遇到的问题是,当我第一次运行程序时,有时它无法读取数据。我放入了一些Try/Exception捕获,发现以某种方式无法从GPS串行端口读取数据 如果我点击Cntrl-C几次,这似乎可以解决它遇到

  • 我有一个Arduino与2个DS18B20温度传感器连接。我对python非常(非常)陌生。我正在寻找一种读取串行输入并将其解析到sqlite数据库的方法,但这已经超出了我的能力。为什么在尝试将串行端口定义为变量时出错? 首先<代码>sys.version 我的当前,只是读取串行连接程序的输入。 我目前无法编译它。我发现这个错误的大多数结果告诉添加,但在这种情况下,它不起作用。 错误。 另外,如果

  • 我试图在一些python代码和arduino代码之间来回“乒乓”信息。我想定期向arduino代码发送两个设定点(例如,在分钟内),在arduino上读取它们 现在,我无法获得可靠的信息来来回跳转。我在搜索中没有找到任何与此类似的内容,我试图修改的所有内容都不起作用。最接近我的是这个(实际上它并没有在发送和接收之间来回切换): python 阿杜伊诺: 我最终得到的只是重复的相同值(不是实际发送的