Java 串行连接和文件问题(Arduino 和 Java)

Java Serial connection and file problem (Arduino and Java)

我有一个 arduino uno 连接到我的电脑,我正在尝试使用一个 java 程序来创建一个 gui,所以我连接到串行端口并将所有数据存储在一个文件中将以今天的日期命名。

但是当我用它尝试连接到串行端口时,我遇到了很多错误。


import java.io.*;
import java.util.Date;
import java.util.Scanner;
import javax.swing.*;
import com.fazecast.jSerialComm.SerialPort;
import java.awt.*;
import java.awt.event.*;

public class FluidDynamicSerial implements ActionListener {

    //main frame
    JFrame window;

    //create class members for buttons/check-box and drop-down
    JComboBox<String> portList;
    JButton connectButton;
    JCheckBox multFilesDay;

    //variables for files
    boolean nameWithTime = false;
    String name;

    //warning message
    String msg = "This file already exists. By default all files are named with today's date, if you wish to fix this problem either rename the file in the current diretory or click the multiple files in one day chekbox";

    //serial port var
    SerialPort port;


    //file vars
    File file;
    FileWriter writeFile;
    BufferedWriter buffWriteFile;

    //Date var
    Date date;

    public static void main(String[] args) {
        new FluidDynamicSerial();
    }

    @SuppressWarnings("deprecation")
    public FluidDynamicSerial() {
        //create window
        window = new JFrame();
        window.setTitle("Save Sensor Readings");
        window.setSize(300, 150);
        window.setLayout(new BorderLayout());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create drop-down and button to connect to serial port
        portList = new JComboBox<String>();
        connectButton = new JButton("Connect");

        //create Top Panel to place drop-down & button
        JPanel topPanel = new JPanel();
        topPanel.add(portList);
        topPanel.add(connectButton);
        window.add(topPanel, BorderLayout.NORTH);

        //create middle panel with hekBox to have multiple files in one day
        multFilesDay = new JCheckBox("Multiple Files in one day");
        JPanel centerPanel = new JPanel();
        centerPanel.add(multFilesDay);
        window.add(centerPanel, BorderLayout.CENTER);

        //place serial port options in drop-down
        SerialPort[] portOptions = SerialPort.getCommPorts();
        for(int i = 0; i < portOptions.length; i++) {
            portList.addItem(portOptions[i].getSystemPortName());
        }

        //file code
        date = new Date();
        String month = new Integer(date.getMonth()+1).toString();
        String yr = new Integer(date.getYear()+1900).toString();

        if(nameWithTime == true) {
            name = month + "-" + date.getDate() + "-" + yr + "_" + date.getHours() + "_" + date.getMinutes() + ".txt";
        } else {
            name = month + "-" + date.getDate() + "-" + yr + ".txt";
        }

        file = new File(name);



        window.setVisible(true);

        connectButton.addActionListener(this);
        multFilesDay.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event){
        Object control = event.getSource();

        if(control == connectButton) {
            if(connectButton.getText().equals("Connect")) {
                port = SerialPort.getCommPort(portList.getSelectedItem().toString());
                port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                if(port.openPort()) {
                    connectButton.setText("Disconnect");
                    portList.setEnabled(false);
                }
                Scanner scan = new Scanner(port.getInputStream());
                while(scan.hasNextLine()) {
                    if(file.exists()) {
                        JOptionPane.showMessageDialog(window,
                                msg,
                                "File Dupliation Error",
                                JOptionPane.ERROR_MESSAGE);
                        scan.close();
                    } else {
                        try {
                            writeFile = new FileWriter(file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        buffWriteFile = new BufferedWriter(writeFile);
                        String line = scan.nextLine();
                        try {
                            buffWriteFile.append(date.toString() + " " + line + " " + "\n");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                scan.close();
                try {
                    buffWriteFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                port.closePort();
                portList.setEnabled(true);
                connectButton.setText("Connect");
            }
        } else if(control == multFilesDay) {
            if(multFilesDay.isEnabled()) {
                nameWithTime = true;
            } else if(multFilesDay.isEnabled() == false) {
                nameWithTime = false;
            }
        }
    }
}

我希望程序创建一个文本文件,其名称将是今天的日期,并且其中包含来自串口的所有数据(电压读数)。

我重新整理了一些代码并对其进行了修改,现在可以使用了,感谢所有试图提供帮助的人