Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - How can I retrieve the arudino sensor data from COM-PORT and save it in a textfile?

I have to use the jSerialComm-API to get my arduino sensor data through my COM-port.

This is my Sensor-Class that displays the sensor data on my eclipse-terminal. Now I want to use the method (extractDataFromCOM()) to provide the sensor-data to other classes.

import com.fazecast.jSerialComm.*;

public class Sensor {
    public void connect() {
        SerialPort comPort = SerialPort.getCommPorts()[0];
        comPort.openPort();
        comPort.addDataListener(new SerialPortDataListener() {
            public int getListeningEvents() {
                return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
            }

            public void serialEvent(SerialPortEvent event) {
                byte[] newData = event.getReceivedData();
                for (int i = 0; i < newData.length; ++i)
                    System.out.print((char)newData[i]); //Terminal output
            }
        });
    }

    public String extractDataFromCOM(String data) {
        //? ? ?
        return data;
    }
}

In the TextDatabase.java class I want to invoke the method (Sensor.extractDataFromCOM()) to retrieve the data and then store it in a textfile:

public class TextDatabase {
    public static void main(String[] args) throws IOException {
        Sensor s1 = new Sensor();
        //endless loop to get flow of sensor data?
        while (true) {
            String mydata = s1.extractDataFromCOM(); 
            
            //store it in a database or textfile: 
            FileWriter fw = new FileWriter("mydata.txt", true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(mydata);
            //(...)
        }
    }
}

Question: How do I access the class 'Sensor' from class 'TextDatabase' and retrieve the sensor data to store it in a textfile?

question from:https://stackoverflow.com/questions/65642021/how-can-i-retrieve-the-arudino-sensor-data-from-com-port-and-save-it-in-a-textfi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put the data from the SerialPortEvent to a class variable in the serialEvent method. Then pass that to external through the extractDataFromCOM

    public class Sensor {

    final Queue<byte[]> data = new PriorityQueue<byte[]>();
    
    public void connect() {
        SerialPort comPort = SerialPort.getCommPorts()[0];
        comPort.openPort();
        comPort.addDataListener(new SerialPortDataListener() {
            public int getListeningEvents() {
                return SerialPort.LISTENING_EVENT_DATA_RECEIVED;
            }

            public void serialEvent(SerialPortEvent event) {
                byte[] newData = event.getReceivedData();
                data.offer(newData);
            }
        });
    }

    public byte[] extractDataFromCOM() {
        //? ? ?
        //convert to String if required
        return data.poll();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...